Skip to content

Commit

Permalink
Remove 'subset' argument in barplot()
Browse files Browse the repository at this point in the history
  • Loading branch information
nfrerebeau committed Oct 22, 2024
1 parent 0276979 commit 4dacca6
Show file tree
Hide file tree
Showing 4 changed files with 17 additions and 28 deletions.
3 changes: 1 addition & 2 deletions R/AllGenerics.R
Original file line number Diff line number Diff line change
Expand Up @@ -1038,7 +1038,6 @@ NULL
#'
#' Displays a compositional bar chart.
#' @param height A [`CompositionMatrix-class`] object.
#' @param select A vector of column indices.
#' @param by A `vector` of grouping elements, as long as the variables in
#' `height`.
#' @param order_columns A [`logical`] scalar: should should columns be reorderd?
Expand All @@ -1057,7 +1056,7 @@ NULL
#' @param border The color to draw the borders.
#' @param axes A [`logical`] scalar: should axes be drawn on the plot?
#' @param legend A [`logical`] scalar: should the legend be displayed?
#' @param ... Further parameters to be passed to [graphics::barplot()].
#' @param ... Further graphical parameters.
#' @return
#' `barplot()` is called for its side-effects: is results in a graphic being
#' displayed (invisibly return `height`).
Expand Down
31 changes: 11 additions & 20 deletions R/barplot.R
Original file line number Diff line number Diff line change
Expand Up @@ -5,28 +5,20 @@ NULL
# CompositionMatrix ============================================================
#' @export
#' @method barplot CompositionMatrix
barplot.CompositionMatrix <- function(height, ..., select = NULL,
barplot.CompositionMatrix <- function(height, ...,
by = groups(height),
order_columns = FALSE, order_rows = NULL,
decreasing = TRUE,
space = 0.2, offset = 0.025,
color = palette_color_discrete(),
border = NA, axes = TRUE, legend = TRUE) {
## Get data
if (is.null(select)) select <- seq_len(ncol(height))
z <- height[, select, drop = FALSE]
if (ncol(z) == 1) {
warning("Only one part is selected. Displaying all data.", call. = FALSE)
z <- height
}
n <- nrow(z)

## Prepare data
xy <- prepare_barplot(z, groups = by, order_columns = order_columns,
xy <- prepare_barplot(height, groups = by, order_columns = order_columns,
order_rows = order_rows, decreasing = decreasing,
offset = offset)
parts <- factor(xy$data$column, levels = colnames(height))
col <- color(parts)
n <- nrow(height)

## Graphical parameters
cex.axis <- list(...)$cex.axis %||% graphics::par("cex.axis")
Expand All @@ -37,7 +29,7 @@ barplot.CompositionMatrix <- function(height, ..., select = NULL,
mfrow <- graphics::par("mfrow")
mar <- graphics::par("mar")
mar[1] <- 3
mar[2] <- width2line(rownames(z), cex = cex.axis) + 0.1
mar[2] <- width2line(rownames(height), cex = cex.axis) + 0.1
mar[3] <- height2line("M", cex = cex.axis)
mar[4] <- height2line("M", cex = cex.axis)

Expand Down Expand Up @@ -107,12 +99,12 @@ prepare_barplot <- function(x, groups = NULL,
decreasing = TRUE, offset = 0.025) {
## Validation
stopifnot(methods::is(x, "CompositionMatrix"))
m <- nrow(x)
n <- nrow(x)

## Grouping
grp <- as_groups(groups, drop_na = FALSE)
if (nlevels(grp) == 0 || nlevels(grp) == m) grp <- rep("", m)
arkhe::assert_length(grp, m)
if (nlevels(grp) == 0 || nlevels(grp) == n) grp <- rep("", n)
arkhe::assert_length(grp, n)

## Row order
spl <- lapply(
Expand All @@ -139,20 +131,19 @@ prepare_barplot <- function(x, groups = NULL,
}

## Relative frequencies
freq <- z / rowSums(z)
xmax <- t(apply(X = freq, MARGIN = 1, FUN = cumsum))
xmin <- xmax - freq
z <- z / rowSums(z)

## Build a long table
row <- row(z, as.factor = TRUE)
col <- col(z, as.factor = TRUE)
data <- data.frame(
row = as.vector(row),
column = as.vector(col),
value = as.vector(freq)
value = as.vector(z)
)

n <- nrow(freq)
xmax <- t(apply(X = z, MARGIN = 1, FUN = cumsum))
xmin <- xmax - z
data$xmin <- as.vector(xmin)
data$xmax <- as.vector(xmax)
data$y <- as.vector(n + 1 - as.numeric(row)) / n # Reverse levels order
Expand Down
3 changes: 2 additions & 1 deletion inst/examples/ex-barplot.R
Original file line number Diff line number Diff line change
Expand Up @@ -16,4 +16,5 @@ coda <- as_composition(kommos, groups = 1) # Coerce to compositional data
barplot(coda, order_columns = TRUE)

## Display only minor elements
barplot(coda, select = is_element_minor(coda), order_columns = TRUE)
minor <- coda[, is_element_minor(coda)]
barplot(minor, order_columns = TRUE)
8 changes: 3 additions & 5 deletions man/barplot.Rd

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

0 comments on commit 4dacca6

Please sign in to comment.