From d9b12c17cf2ed86cc09a7528f1e59a22fd9f5325 Mon Sep 17 00:00:00 2001 From: Michael Chirico Date: Sat, 31 May 2025 11:52:30 -0700 Subject: [PATCH 1/6] refactor plyr::count to use dplyr --- NAMESPACE | 3 +++ R/gg-plots.R | 5 ++++- tests/testthat/test-zzz_ggpairs.R | 5 ++++- 3 files changed, 11 insertions(+), 2 deletions(-) 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..c36f066d7 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") 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))) + From 33a16626b24383f98608d3793d0adbf212bea1b7 Mon Sep 17 00:00:00 2001 From: Michael Chirico Date: Sat, 31 May 2025 11:54:24 -0700 Subject: [PATCH 2/6] more recent dplyr required for .by= --- DESCRIPTION | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) 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, From 20087c6bb5d8ec1a144da1c4ab7533f65b04efdd Mon Sep 17 00:00:00 2001 From: Michael Chirico Date: Mon, 2 Jun 2025 09:06:18 -0700 Subject: [PATCH 3/6] use dplyr approach to summarise() --- R/gg-plots.R | 19 ++++++++----------- 1 file changed, 8 insertions(+), 11 deletions(-) diff --git a/R/gg-plots.R b/R/gg-plots.R index c36f066d7..868bce067 100644 --- a/R/gg-plots.R +++ b/R/gg-plots.R @@ -1770,17 +1770,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(y, weight)) %>% + arrange(y) # keep colour if matching the discrete variable if (mapping_string(mapping$colour) == mapping_string(mapping$y)) { col <- as.name("y") From 172a1e6979413903795a6b817f56ac1581b0819e Mon Sep 17 00:00:00 2001 From: Michael Chirico Date: Mon, 2 Jun 2025 09:14:30 -0700 Subject: [PATCH 4/6] typo --- R/gg-plots.R | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/R/gg-plots.R b/R/gg-plots.R index 868bce067..91621cc4b 100644 --- a/R/gg-plots.R +++ b/R/gg-plots.R @@ -1776,7 +1776,7 @@ ggally_summarise_by <- function( weight = eval_data_col(data, mapping$weight) %||% 1, stringsAsFactors = FALSE ) %>% - summarise(label = text_fn(y, weight)) %>% + 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)) { From ff3154f2932fb9b35217446bf479fc400355bf47 Mon Sep 17 00:00:00 2001 From: Michael Chirico Date: Mon, 2 Jun 2025 09:39:32 -0700 Subject: [PATCH 5/6] explicit roxygen import comment --- R/gg-plots.R | 1 + 1 file changed, 1 insertion(+) diff --git a/R/gg-plots.R b/R/gg-plots.R index 91621cc4b..49bf9d4b1 100644 --- a/R/gg-plots.R +++ b/R/gg-plots.R @@ -1759,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, From 2c8793b50ec525e73cf352e4b8f9088cdbd4cab6 Mon Sep 17 00:00:00 2001 From: Michael Chirico Date: Mon, 2 Jun 2025 10:26:37 -0700 Subject: [PATCH 6/6] plyr::summarize->dplyr::summarise --- NAMESPACE | 1 - R/ggparcoord.R | 10 +++++----- 2 files changed, 5 insertions(+), 6 deletions(-) diff --git a/NAMESPACE b/NAMESPACE index d5b24f57d..c2cb46bfd 100644 --- a/NAMESPACE +++ b/NAMESPACE @@ -173,7 +173,6 @@ importFrom(lifecycle,deprecate_soft) importFrom(lifecycle,deprecated) importFrom(magrittr,"%>%") importFrom(plyr,ddply) -importFrom(plyr,summarize) importFrom(rlang,"%||%") importFrom(stats,anova) importFrom(stats,complete.cases) diff --git a/R/ggparcoord.R b/R/ggparcoord.R index ad6cf2ad2..a8e41bc44 100644 --- a/R/ggparcoord.R +++ b/R/ggparcoord.R @@ -77,7 +77,8 @@ if (getRversion() >= "2.15.1") { #' @param title character string denoting the title of the plot #' @author Jason Crowley, Barret Schloerke, Dianne Cook, Heike Hofmann, Hadley Wickham #' @return ggplot object that if called, will print -#' @importFrom plyr ddply summarize +#' @importFrom dplyr arrange summarise +#' @importFrom plyr ddply #' @importFrom stats complete.cases sd median mad lm spline #' @importFrom tidyr pivot_longer #' @export @@ -512,10 +513,9 @@ ggparcoord <- function( if (!is.null(shadeBox)) { # Fix so that if missing = "min10", the box only goes down to the true min - d.sum <- ddply(data.m, c("variable"), summarize, - min = min(value), - max = max(value) - ) + d.sum <- data.m %>% + summarise(min = min(value), max = max(value), .by = variable) %>% + arrange(variable) p <- p + geom_linerange( data = d.sum, linewidth = I(10), col = shadeBox, inherit.aes = FALSE,