diff --git a/R/bar.R b/R/bar.R index 93e8d29..90519c1 100644 --- a/R/bar.R +++ b/R/bar.R @@ -21,7 +21,6 @@ hbar <- function( #' @param y A quoted character column or coercible as a character column. #' @param group Some quoted grouping categorical column, e.g. administrative areas or population groups. #' @param facet Some quoted grouping categorical column, e.g. administrative areas or population groups. -#' @param order Should bars be ordered? "none" if no, "y" if yes based on y, "grouped" if yes based on y and group. #' @param x_rm_na Remove NAs in x? #' @param y_rm_na Remove NAs in y? #' @param group_rm_na Remove NAs in group? diff --git a/R/reorder_by.R b/R/reorder_by.R index cd6df24..ad0cf9a 100644 --- a/R/reorder_by.R +++ b/R/reorder_by.R @@ -48,6 +48,9 @@ reorder_by <- function(df, x, y, group = "", order = "y", dir_order = 1) { # dir_order is 1 or -1 (numeric scalar) checkmate::assert_subset(dir_order, c(1, -1)) + # Convert dir_order to decreasing logical flag + dir_order_lgl <- (dir_order == -1) + #------ Reorder # droplevels first @@ -58,29 +61,33 @@ reorder_by <- function(df, x, y, group = "", order = "y", dir_order = 1) { # reording options if (order == "y") { # Order by values of y - df <- df[order(df[[y]] * dir_order), ] + df <- df[order(df[[y]], decreasing = dir_order_lgl), ] df[[x]] <- forcats::fct_inorder(df[[x]]) } else if (order == "grouped_y" && group != "") { # Order by group first, then by values of y - df <- df[order(df[[group]], df[[y]] * dir_order), ] + df <- df[ + order(df[[group]], df[[y]], decreasing = c(FALSE, dir_order_lgl)), + ] df[[x]] <- forcats::fct_inorder(df[[x]]) } else if (order == "grouped_y" && group == "") { # Fallback to ordering by y if group is empty rlang::warn("Group is empty. Ordering by y only.") - df <- df[order(df[[y]] * dir_order), ] + df <- df[order(df[[y]], decreasing = dir_order_lgl), ] df[[x]] <- forcats::fct_inorder(df[[x]]) } else if (order == "x") { # Order alphabetically by x - df <- df[order(df[[x]] * dir_order), ] + df <- df[order(df[[x]], decreasing = dir_order_lgl), ] df[[x]] <- forcats::fct_inorder(df[[x]]) } else if (order == "grouped_x" && group != "") { # Order by group first, then alphabetically by x - df <- df[order(df[[group]], df[[x]] * dir_order), ] + df <- df[ + order(df[[group]], df[[x]], decreasing = c(FALSE, dir_order_lgl)), + ] df[[x]] <- forcats::fct_inorder(df[[x]]) } else if (order == "grouped_x" && group == "") { # Fallback to ordering by x if group is empty rlang::warn("Group is empty. Ordering by x only.") - df <- df[order(df[[x]] * dir_order), ] + df <- df[order(df[[x]], decreasing = dir_order_lgl), ] df[[x]] <- forcats::fct_inorder(df[[x]]) } diff --git a/man/bar.Rd b/man/bar.Rd index 2a04989..db8545a 100644 --- a/man/bar.Rd +++ b/man/bar.Rd @@ -68,7 +68,7 @@ bar( \item{facet}{Some quoted grouping categorical column, e.g. administrative areas or population groups.} -\item{order}{Should bars be ordered? "none" if no, "y" if yes based on y, "grouped" if yes based on y and group.} +\item{order}{A character scalar specifying the order type (one of "none", "y", "grouped"). See details.} \item{x_rm_na}{Remove NAs in x?}