From 6b39ac9216beea279f31aadc4b2355ec8189170d Mon Sep 17 00:00:00 2001 From: chris Date: Wed, 25 Jan 2023 19:38:30 -0500 Subject: [PATCH 1/3] Fix issue where grouped boxplots and violin plots were variable widths (closes #169) --- R/ggMarginal-MarginalPlot.R | 25 +-- ...oxplots-are-the-same-within-a-marginal.svg | 165 ++++++++++++++++++ ...oxplots-are-the-same-within-a-marginal.svg | 165 ++++++++++++++++++ tests/testthat/helper-funs.R | 3 + 4 files changed, 341 insertions(+), 17 deletions(-) create mode 100644 tests/figs/ggMarginal/ggplot2-3.3.0/widths-of-boxplots-are-the-same-within-a-marginal.svg create mode 100644 tests/figs/ggMarginal/ggplot2-3.4.0/widths-of-boxplots-are-the-same-within-a-marginal.svg diff --git a/R/ggMarginal-MarginalPlot.R b/R/ggMarginal-MarginalPlot.R index ac32b73..0573d7a 100644 --- a/R/ggMarginal-MarginalPlot.R +++ b/R/ggMarginal-MarginalPlot.R @@ -53,7 +53,11 @@ MarginalPlot <- R6::R6Class("MarginalPlot", scatDF$x <- scatDF$y } - scatDF$y <- scatDF$x + # We never want to actually use the y aesthetic when creating the marginal + # plots, but geom_violin requires it. Also note that, in order to make the + # widths of violin and boxplots the same in the marginal plots, we need a + # constant y. + scatDF$y <- 1 scatDF[, c("x", "y", "fill", "colour", "group")] }, @@ -246,15 +250,8 @@ MarginalPlot <- R6::R6Class("MarginalPlot", addLimits = function(margThemed) { limits <- private$getLimits() - # for plots with y aes we have to use scale_y_continuous instead of - # scale_x_continuous. - if (self$type %in% c("boxplot", "violin")) { - margThemed + - ggplot2::scale_y_continuous(limits = limits, oob = scales::squish) - } else { - margThemed + - ggplot2::scale_x_continuous(limits = limits, oob = scales::squish) - } + margThemed + + ggplot2::scale_x_continuous(limits = limits, oob = scales::squish) }, getPanelScale = function(marg) { @@ -275,13 +272,7 @@ MarginalPlot <- R6::R6Class("MarginalPlot", }, needsFlip = function() { - # If the marginal plot is: (for the x margin (top) and is a boxplot) or - # (for the y margin (right) and is not a boxplot), - # ... then have to flip - topAndBoxP <- self$marg == "x" && self$type %in% c("boxplot", "violin") - rightAndNonBoxP <- - self$marg == "y" && !(self$type %in% c("boxplot", "violin")) - topAndBoxP || rightAndNonBoxP + self$marg == "y" }, # Get the axis range of the x or y axis of the given ggplot build object diff --git a/tests/figs/ggMarginal/ggplot2-3.3.0/widths-of-boxplots-are-the-same-within-a-marginal.svg b/tests/figs/ggMarginal/ggplot2-3.3.0/widths-of-boxplots-are-the-same-within-a-marginal.svg new file mode 100644 index 0000000..6359ab5 --- /dev/null +++ b/tests/figs/ggMarginal/ggplot2-3.3.0/widths-of-boxplots-are-the-same-within-a-marginal.svg @@ -0,0 +1,165 @@ + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + +3.0 +3.5 +4.0 +4.5 +5.0 + + + + + + + + + +2 +3 +4 +5 +wt +drat + +factor(vs) + + + + +0 +1 + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + diff --git a/tests/figs/ggMarginal/ggplot2-3.4.0/widths-of-boxplots-are-the-same-within-a-marginal.svg b/tests/figs/ggMarginal/ggplot2-3.4.0/widths-of-boxplots-are-the-same-within-a-marginal.svg new file mode 100644 index 0000000..35f74c2 --- /dev/null +++ b/tests/figs/ggMarginal/ggplot2-3.4.0/widths-of-boxplots-are-the-same-within-a-marginal.svg @@ -0,0 +1,165 @@ + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + +3.0 +3.5 +4.0 +4.5 +5.0 + + + + + + + + + +2 +3 +4 +5 +wt +drat + +factor(vs) + + + + +0 +1 + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + diff --git a/tests/testthat/helper-funs.R b/tests/testthat/helper-funs.R index d74357d..668dadd 100644 --- a/tests/testthat/helper-funs.R +++ b/tests/testthat/helper-funs.R @@ -95,6 +95,9 @@ groupingFeature <- list( "groupFill doesn't impact hist heights - with fill" = function() ggMarginal( margMapP(), type = "histogram", xparams = list(binwidth = .3), groupFill = TRUE + ), + "widths of boxplots are the same within a marginal" = function() ggMarginal( + margMapP(), type = "boxplot", groupColour = TRUE ) ) transforms <- list( From 949cc14d7d71b54c115ed5e924279f7291d3c58b Mon Sep 17 00:00:00 2001 From: chris Date: Fri, 27 Jan 2023 22:35:04 -0500 Subject: [PATCH 2/3] Bumpt version and update news --- DESCRIPTION | 2 +- NEWS.md | 6 ++++++ 2 files changed, 7 insertions(+), 1 deletion(-) diff --git a/DESCRIPTION b/DESCRIPTION index a868efe..2776c88 100644 --- a/DESCRIPTION +++ b/DESCRIPTION @@ -1,6 +1,6 @@ Package: ggExtra Title: Add Marginal Histograms to 'ggplot2', and More 'ggplot2' Enhancements -Version: 0.10.0 +Version: 0.10.9000 Authors@R: c( person("Dean", "Attali", , "daattali@gmail.com", role = c("aut", "cre")), person("Christopher", "Baker", , "chriscrewbaker@gmail.com", role = "aut") diff --git a/NEWS.md b/NEWS.md index 8ddecf5..03145a6 100644 --- a/NEWS.md +++ b/NEWS.md @@ -1,3 +1,9 @@ +# ggExtra 0.10.9000 + +2023-01-27 + +- Fixed issue where grouped boxplots and violin plots were variable widths (#173) + # ggExtra 0.10.0 2022-03-22 From 7c61ec169af1c52ff779803311fa3b5f617f9bb0 Mon Sep 17 00:00:00 2001 From: Dean Attali Date: Sat, 28 Jan 2023 01:18:37 -0500 Subject: [PATCH 3/3] Update DESCRIPTION --- DESCRIPTION | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/DESCRIPTION b/DESCRIPTION index 2776c88..f5e5da6 100644 --- a/DESCRIPTION +++ b/DESCRIPTION @@ -1,6 +1,6 @@ Package: ggExtra Title: Add Marginal Histograms to 'ggplot2', and More 'ggplot2' Enhancements -Version: 0.10.9000 +Version: 0.10.0.9000 Authors@R: c( person("Dean", "Attali", , "daattali@gmail.com", role = c("aut", "cre")), person("Christopher", "Baker", , "chriscrewbaker@gmail.com", role = "aut")