Fix ordering logic in reorder_by to use decreasing argument

This commit is contained in:
gnoblet 2025-07-02 09:11:35 +02:00
parent a8ea3ea284
commit cc8fab9a19
3 changed files with 14 additions and 8 deletions

View file

@ -21,7 +21,6 @@ hbar <- function(
#' @param y A quoted character column or coercible as a character column. #' @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 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 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 x_rm_na Remove NAs in x?
#' @param y_rm_na Remove NAs in y? #' @param y_rm_na Remove NAs in y?
#' @param group_rm_na Remove NAs in group? #' @param group_rm_na Remove NAs in group?

View file

@ -48,6 +48,9 @@ reorder_by <- function(df, x, y, group = "", order = "y", dir_order = 1) {
# dir_order is 1 or -1 (numeric scalar) # dir_order is 1 or -1 (numeric scalar)
checkmate::assert_subset(dir_order, c(1, -1)) checkmate::assert_subset(dir_order, c(1, -1))
# Convert dir_order to decreasing logical flag
dir_order_lgl <- (dir_order == -1)
#------ Reorder #------ Reorder
# droplevels first # droplevels first
@ -58,29 +61,33 @@ reorder_by <- function(df, x, y, group = "", order = "y", dir_order = 1) {
# reording options # reording options
if (order == "y") { if (order == "y") {
# Order by values of 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]]) df[[x]] <- forcats::fct_inorder(df[[x]])
} else if (order == "grouped_y" && group != "") { } else if (order == "grouped_y" && group != "") {
# Order by group first, then by values of y # 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]]) df[[x]] <- forcats::fct_inorder(df[[x]])
} else if (order == "grouped_y" && group == "") { } else if (order == "grouped_y" && group == "") {
# Fallback to ordering by y if group is empty # Fallback to ordering by y if group is empty
rlang::warn("Group is empty. Ordering by y only.") 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]]) df[[x]] <- forcats::fct_inorder(df[[x]])
} else if (order == "x") { } else if (order == "x") {
# Order alphabetically by 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]]) df[[x]] <- forcats::fct_inorder(df[[x]])
} else if (order == "grouped_x" && group != "") { } else if (order == "grouped_x" && group != "") {
# Order by group first, then alphabetically by x # 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]]) df[[x]] <- forcats::fct_inorder(df[[x]])
} else if (order == "grouped_x" && group == "") { } else if (order == "grouped_x" && group == "") {
# Fallback to ordering by x if group is empty # Fallback to ordering by x if group is empty
rlang::warn("Group is empty. Ordering by x only.") 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]]) df[[x]] <- forcats::fct_inorder(df[[x]])
} }

View file

@ -68,7 +68,7 @@ bar(
\item{facet}{Some quoted grouping categorical column, e.g. administrative areas or population groups.} \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?} \item{x_rm_na}{Remove NAs in x?}