Skip to content

Commit

Permalink
Fix row ordering in barplot()
Browse files Browse the repository at this point in the history
  • Loading branch information
nfrerebeau committed Oct 23, 2024
1 parent 0c1c439 commit ad260eb
Showing 1 changed file with 4 additions and 3 deletions.
7 changes: 4 additions & 3 deletions R/barplot.R
Original file line number Diff line number Diff line change
Expand Up @@ -104,6 +104,9 @@ prepare_barplot <- function(x, groups = NULL,
decreasing = TRUE, offset = 0.025) {
## Validation
stopifnot(methods::is(x, "CompositionMatrix"))
grp <- as_groups(groups, drop_na = FALSE)
if (nlevels(grp) == 0 || nlevels(grp) == n) grp <- rep("", n)
arkhe::assert_length(grp, n)

## Relative frequencies
x <- x / rowSums(x)
Expand All @@ -114,6 +117,7 @@ prepare_barplot <- function(x, groups = NULL,
j <- x[, order_rows, drop = TRUE]
i <- order(j, decreasing = decreasing)
x <- x[i, , drop = FALSE]
grp <- grp[i]
}

## Columns order
Expand All @@ -125,9 +129,6 @@ prepare_barplot <- function(x, groups = NULL,
}

## Grouping
grp <- as_groups(groups, drop_na = FALSE)
if (nlevels(grp) == 0 || nlevels(grp) == n) grp <- rep("", n)
arkhe::assert_length(grp, n)
spl <- split(x = x, f = grp)
z <- do.call(rbind, spl)

Expand Down

0 comments on commit ad260eb

Please sign in to comment.