diff --git a/DESCRIPTION b/DESCRIPTION index 5b259721b..7935f5922 100644 --- a/DESCRIPTION +++ b/DESCRIPTION @@ -31,7 +31,7 @@ Depends: R (>= 3.1), ggplot2 (>= 3.4.4) Imports: - dplyr (>= 1.0.0), + dplyr (>= 1.1.0), tidyr (>= 1.3.0), grDevices, grid, diff --git a/NAMESPACE b/NAMESPACE index defe34671..d5b24f57d 100644 --- a/NAMESPACE +++ b/NAMESPACE @@ -136,7 +136,10 @@ import(RColorBrewer) import(ggplot2) import(utils) importFrom(dplyr,all_of) +importFrom(dplyr,arrange) importFrom(dplyr,group_by) +importFrom(dplyr,n) +importFrom(dplyr,pick) importFrom(dplyr,rename) importFrom(dplyr,summarise) importFrom(ggstats,StatCross) diff --git a/R/gg-plots.R b/R/gg-plots.R index 588722f29..49bf9d4b1 100644 --- a/R/gg-plots.R +++ b/R/gg-plots.R @@ -1319,6 +1319,7 @@ ggally_facetbar <- function(data, mapping, ...) { #' tips, ggplot2::aes(sex, day), #' floor = 20, ceiling = 50 #' ) + ggplot2::theme(aspect.ratio = 4 / 2)) +#' @importFrom dplyr all_of arrange n pick summarise ggally_ratio <- function( data, mapping = ggplot2::aes(!!!stats::setNames(lapply(colnames(data)[1:2], as.name), c("x", "y"))), @@ -1329,7 +1330,9 @@ ggally_ratio <- function( xName <- mapping_string(mapping$x) yName <- mapping_string(mapping$y) - countData <- plyr::count(data, vars = c(xName, yName)) + countData <- data %>% + summarise(freq = n(), .by = all_of(c(xName, yName))) %>% + arrange(pick(c(xName, yName))) # overwrite names so name clashes don't happen colnames(countData)[1:2] <- c("x", "y") @@ -1756,6 +1759,7 @@ ggally_autopointDiag <- function(data, mapping, ...) { #' } #' p_(ggally_summarise_by(tips, mapping = aes(x = total_bill, y = day), text_fn = weighted_sum)) #' } +#' @importFrom dplyr arrange summarise ggally_summarise_by <- function( data, mapping, @@ -1767,17 +1771,14 @@ ggally_summarise_by <- function( horizontal <- is_horizontal(data, mapping) if (horizontal) { - res <- ddply( - data.frame( - x = eval_data_col(data, mapping$x), - y = eval_data_col(data, mapping$y), - weight = eval_data_col(data, mapping$weight) %||% 1, - stringsAsFactors = FALSE - ), - c("y"), - plyr::here(summarize), - label = text_fn(x, weight) - ) + res <- data.frame( + x = eval_data_col(data, mapping$x), + y = eval_data_col(data, mapping$y), + weight = eval_data_col(data, mapping$weight) %||% 1, + stringsAsFactors = FALSE + ) %>% + summarise(label = text_fn(x, weight), .by = y) %>% + arrange(y) # keep colour if matching the discrete variable if (mapping_string(mapping$colour) == mapping_string(mapping$y)) { col <- as.name("y") diff --git a/tests/testthat/test-zzz_ggpairs.R b/tests/testthat/test-zzz_ggpairs.R index 8154ea536..2305e617b 100644 --- a/tests/testthat/test-zzz_ggpairs.R +++ b/tests/testthat/test-zzz_ggpairs.R @@ -609,7 +609,10 @@ test_that("strip-top and strip-right", { data(tips) double_strips <- function(data, mapping, ...) { - dt <- plyr::count(data, c(mapping_string(mapping$x), mapping_string(mapping$y))) + cols <- c(mapping_string(mapping$x), mapping_string(mapping$y)) + dt <- data %>% + summarise(freq = n(), .by = all_of(cols)) %>% + arrange(pick(cols)) ggplot(dt, aes(xmin = 0.25, xmax = 0.75, ymin = 1, ymax = freq)) + geom_rect() + ggplot2::facet_grid(paste0(mapping_string(mapping$y), " ~ ", mapping_string(mapping$x))) +