From 4dacca630ba12a9509f2855d4cfc06eaf816e333 Mon Sep 17 00:00:00 2001 From: nfrerebeau Date: Tue, 22 Oct 2024 18:45:15 +0200 Subject: [PATCH] Remove 'subset' argument in barplot() --- R/AllGenerics.R | 3 +-- R/barplot.R | 31 +++++++++++-------------------- inst/examples/ex-barplot.R | 3 ++- man/barplot.Rd | 8 +++----- 4 files changed, 17 insertions(+), 28 deletions(-) diff --git a/R/AllGenerics.R b/R/AllGenerics.R index 63e7b9a..7e1ab1b 100644 --- a/R/AllGenerics.R +++ b/R/AllGenerics.R @@ -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? @@ -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`). diff --git a/R/barplot.R b/R/barplot.R index 763dbef..4544e3d 100644 --- a/R/barplot.R +++ b/R/barplot.R @@ -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") @@ -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) @@ -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( @@ -139,9 +131,7 @@ 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) @@ -149,10 +139,11 @@ prepare_barplot <- function(x, groups = NULL, 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 diff --git a/inst/examples/ex-barplot.R b/inst/examples/ex-barplot.R index 23b4fe4..7b07211 100644 --- a/inst/examples/ex-barplot.R +++ b/inst/examples/ex-barplot.R @@ -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) diff --git a/man/barplot.Rd b/man/barplot.Rd index 8af1a72..805fd0d 100644 --- a/man/barplot.Rd +++ b/man/barplot.Rd @@ -9,7 +9,6 @@ \S4method{barplot}{CompositionMatrix}( height, ..., - select = NULL, by = groups(height), order_columns = FALSE, order_rows = NULL, @@ -25,9 +24,7 @@ \arguments{ \item{height}{A \code{\linkS4class{CompositionMatrix}} object.} -\item{...}{Further parameters to be passed to \code{\link[graphics:barplot]{graphics::barplot()}}.} - -\item{select}{A vector of column indices.} +\item{...}{Further graphical parameters.} \item{by}{A \code{vector} of grouping elements, as long as the variables in \code{height}.} @@ -83,7 +80,8 @@ 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) } \seealso{ Other plot methods: