From 071a524abfade561e95da1a4f6f4a7f99373c83a Mon Sep 17 00:00:00 2001 From: J Kyle Armstrong Date: Thu, 27 Jun 2024 22:54:57 -0400 Subject: [PATCH 1/3] suggestion to address `get_legend` issue #205 suggestion to address `get_legend` issue #205 - added `is_zeroGrob` function - modified `get_plot_component` to filter all nonZero Grobs. - added return_all option to `get_legend` function. A side effect is that now fails test "get legend" however, noted in comments the test is "more of a duct tape solution than a reflection of the intended behaviour". I think for almost all components it would be reasonable to return first nonZero Grubs by default, the option to return all also enables the user to grab all labels that match. --- DESCRIPTION | 2 +- NAMESPACE | 1 + R/get_legend.R | 4 +- R/get_plot_component.R | 27 +- man/get_plot_component.Rd | 3 + man/is_zeroGrob.Rd | 17 + .../draw_/bottom-right-half-width-height.svg | 56 +++ .../draw_/centered-full-width-height.svg | 56 +++ .../draw_/centered-half-width-height.svg | 56 +++ .../draw_/four-corners-aligned-scaled.svg | 59 +++ .../draw_/four-corners-centered-scaled.svg | 59 +++ .../label-color-specified-w-uk-spelling.svg | 24 ++ .../label-color-specified-w-us-spelling.svg | 24 ++ .../draw_/label-color-taken-from-theme.svg | 24 ++ .../draw_/top-left-half-width-height.svg | 56 +++ .../circle-key-glyph-color-used-as-fill.svg | 47 +++ ...rectangle-key-glyph-color-used-as-fill.svg | 47 +++ ...ligning-faceted-plots-w-unequal-widths.svg | 367 ++++++++++++++++++ .../basic-plot-arranging-labeling.svg | 87 +++++ ...basic-plot-arranging-with-missing-plot.svg | 83 ++++ .../_snaps/plot_grid/colwise-arranging.svg | 83 ++++ .../h-v-alignment-right-and-top-axes.svg | 237 +++++++++++ .../horizontal-alignment-bottom-axis.svg | 237 +++++++++++ .../horizontal-alignment-top-axis.svg | 237 +++++++++++ .../_snaps/plot_grid/horizontal-alignment.svg | 129 ++++++ .../horizontal-vertical-alignment.svg | 182 +++++++++ .../_snaps/plot_grid/scaling-plots.svg | 86 ++++ .../plot_grid/setting-heights-1-1-2.svg | 83 ++++ .../_snaps/plot_grid/setting-widths-1-1-2.svg | 83 ++++ .../vertical-alignment-left-axis.svg | 237 +++++++++++ .../vertical-alignment-right-axis.svg | 237 +++++++++++ .../_snaps/plot_grid/vertical-alignment.svg | 129 ++++++ .../_snaps/themes/theme-half-open-huge.svg | 103 +++++ .../_snaps/themes/theme-half-open-tiny.svg | 103 +++++ .../_snaps/themes/theme-half-open.svg | 103 +++++ .../_snaps/themes/theme-minimal-grid-huge.svg | 108 ++++++ .../_snaps/themes/theme-minimal-grid-tiny.svg | 108 ++++++ .../_snaps/themes/theme-minimal-grid.svg | 108 ++++++ .../themes/theme-minimal-hgrid-huge.svg | 106 +++++ .../themes/theme-minimal-hgrid-tiny.svg | 106 +++++ .../_snaps/themes/theme-minimal-hgrid.svg | 106 +++++ .../themes/theme-minimal-vgrid-huge.svg | 105 +++++ .../themes/theme-minimal-vgrid-tiny.svg | 105 +++++ .../_snaps/themes/theme-minimal-vgrid.svg | 105 +++++ 44 files changed, 4320 insertions(+), 5 deletions(-) create mode 100644 man/is_zeroGrob.Rd create mode 100644 tests/testthat/_snaps/draw_/bottom-right-half-width-height.svg create mode 100644 tests/testthat/_snaps/draw_/centered-full-width-height.svg create mode 100644 tests/testthat/_snaps/draw_/centered-half-width-height.svg create mode 100644 tests/testthat/_snaps/draw_/four-corners-aligned-scaled.svg create mode 100644 tests/testthat/_snaps/draw_/four-corners-centered-scaled.svg create mode 100644 tests/testthat/_snaps/draw_/label-color-specified-w-uk-spelling.svg create mode 100644 tests/testthat/_snaps/draw_/label-color-specified-w-us-spelling.svg create mode 100644 tests/testthat/_snaps/draw_/label-color-taken-from-theme.svg create mode 100644 tests/testthat/_snaps/draw_/top-left-half-width-height.svg create mode 100644 tests/testthat/_snaps/key_glyph/circle-key-glyph-color-used-as-fill.svg create mode 100644 tests/testthat/_snaps/key_glyph/rectangle-key-glyph-color-used-as-fill.svg create mode 100644 tests/testthat/_snaps/plot_grid/aligning-faceted-plots-w-unequal-widths.svg create mode 100644 tests/testthat/_snaps/plot_grid/basic-plot-arranging-labeling.svg create mode 100644 tests/testthat/_snaps/plot_grid/basic-plot-arranging-with-missing-plot.svg create mode 100644 tests/testthat/_snaps/plot_grid/colwise-arranging.svg create mode 100644 tests/testthat/_snaps/plot_grid/h-v-alignment-right-and-top-axes.svg create mode 100644 tests/testthat/_snaps/plot_grid/horizontal-alignment-bottom-axis.svg create mode 100644 tests/testthat/_snaps/plot_grid/horizontal-alignment-top-axis.svg create mode 100644 tests/testthat/_snaps/plot_grid/horizontal-alignment.svg create mode 100644 tests/testthat/_snaps/plot_grid/horizontal-vertical-alignment.svg create mode 100644 tests/testthat/_snaps/plot_grid/scaling-plots.svg create mode 100644 tests/testthat/_snaps/plot_grid/setting-heights-1-1-2.svg create mode 100644 tests/testthat/_snaps/plot_grid/setting-widths-1-1-2.svg create mode 100644 tests/testthat/_snaps/plot_grid/vertical-alignment-left-axis.svg create mode 100644 tests/testthat/_snaps/plot_grid/vertical-alignment-right-axis.svg create mode 100644 tests/testthat/_snaps/plot_grid/vertical-alignment.svg create mode 100644 tests/testthat/_snaps/themes/theme-half-open-huge.svg create mode 100644 tests/testthat/_snaps/themes/theme-half-open-tiny.svg create mode 100644 tests/testthat/_snaps/themes/theme-half-open.svg create mode 100644 tests/testthat/_snaps/themes/theme-minimal-grid-huge.svg create mode 100644 tests/testthat/_snaps/themes/theme-minimal-grid-tiny.svg create mode 100644 tests/testthat/_snaps/themes/theme-minimal-grid.svg create mode 100644 tests/testthat/_snaps/themes/theme-minimal-hgrid-huge.svg create mode 100644 tests/testthat/_snaps/themes/theme-minimal-hgrid-tiny.svg create mode 100644 tests/testthat/_snaps/themes/theme-minimal-hgrid.svg create mode 100644 tests/testthat/_snaps/themes/theme-minimal-vgrid-huge.svg create mode 100644 tests/testthat/_snaps/themes/theme-minimal-vgrid-tiny.svg create mode 100644 tests/testthat/_snaps/themes/theme-minimal-vgrid.svg diff --git a/DESCRIPTION b/DESCRIPTION index 235dd3e..ce71c51 100644 --- a/DESCRIPTION +++ b/DESCRIPTION @@ -71,6 +71,6 @@ Collate: 'stamp.R' 'themes.R' 'utils_ggplot2.R' -RoxygenNote: 7.2.3 +RoxygenNote: 7.3.1 Roxygen: list(markdown = TRUE) Encoding: UTF-8 diff --git a/NAMESPACE b/NAMESPACE index 64c65c5..86ed2f0 100644 --- a/NAMESPACE +++ b/NAMESPACE @@ -46,6 +46,7 @@ export(gtable_squash_cols) export(gtable_squash_rows) export(insert_xaxis_grob) export(insert_yaxis_grob) +export(is_zeroGrob) export(panel_border) export(pdf_null_device) export(plot_component_names) diff --git a/R/get_legend.R b/R/get_legend.R index 580a179..4992a78 100644 --- a/R/get_legend.R +++ b/R/get_legend.R @@ -20,6 +20,6 @@ #' plot_grid(NULL, legend, ncol=1), #' rel_widths=c(1, 0.2))) #' @export -get_legend <- function(plot) { - get_plot_component(plot, "guide-box") +get_legend <- function(plot, return_all = FALSE) { + get_plot_component(plot, "guide-box", return_all = return_all) } diff --git a/R/get_plot_component.R b/R/get_plot_component.R index 78530dd..d5b4d85 100644 --- a/R/get_plot_component.R +++ b/R/get_plot_component.R @@ -1,3 +1,15 @@ +#' is zero grob +#' +#' Function to check if a grob is a zero grob +#' +#' @param x A grob +#' @return A logical value +#' @export +is_zeroGrob <- function(x) { + "zeroGrob" %in% attr(x, "class") +} + + #' Get plot components #' #' Extract plot components from a ggplot or gtable. `get_plot_component()` @@ -18,6 +30,9 @@ #' p <- ggplot(mpg, aes(displ, cty)) + geom_point() #' ggdraw(get_plot_component(p, "ylab-l")) #' +#' plot.mpg <- ggplot(mpg, aes(x = cty, y = hwy, colour = factor(cyl))) + geom_point(size=2.5) + theme(legend.position='bottom') +#' ggdraw(get_plot_component(plot.mpg, "guide-box")) +#' #' @export get_plot_component <- function(plot, pattern, return_all = FALSE) { plot <- as_gtable(plot) @@ -30,8 +45,16 @@ get_plot_component <- function(plot, pattern, return_all = FALSE) { if (length(grobIndex) > 1 && !return_all) { # If there's more than one grob, return just the first one warning("Multiple components found; returning the first one. To return all, use `return_all = TRUE`.") - grobIndex <- grobIndex[1] - matched_grobs <- grobs[[grobIndex]] + #grobIndex <- grobIndex[1] + matched_grobs1 <- grobs[grobIndex] + # Remove zero grobs + matched_grobs_non_zero <- matched_grobs1[!sapply(matched_grobs1, is_zeroGrob)] + # now get the first grob + if(length(matched_grobs_non_zero) == 0) { + matched_grobs <- NULL + } else { + matched_grobs <- matched_grobs_non_zero[[1]] + } } else if (length(grobIndex) > 1 && return_all) { # If there's more than one grob, return all as a list matched_grobs <- grobs[grobIndex] diff --git a/man/get_plot_component.Rd b/man/get_plot_component.Rd index 3c33ce9..99402e1 100644 --- a/man/get_plot_component.Rd +++ b/man/get_plot_component.Rd @@ -36,4 +36,7 @@ library(ggplot2) p <- ggplot(mpg, aes(displ, cty)) + geom_point() ggdraw(get_plot_component(p, "ylab-l")) +plot.mpg <- ggplot(mpg, aes(x = cty, y = hwy, colour = factor(cyl))) + geom_point(size=2.5) + theme(legend.position='bottom') +ggdraw(get_plot_component(plot.mpg, "guide-box")) + } diff --git a/man/is_zeroGrob.Rd b/man/is_zeroGrob.Rd new file mode 100644 index 0000000..275e385 --- /dev/null +++ b/man/is_zeroGrob.Rd @@ -0,0 +1,17 @@ +% Generated by roxygen2: do not edit by hand +% Please edit documentation in R/get_plot_component.R +\name{is_zeroGrob} +\alias{is_zeroGrob} +\title{is zero grob} +\usage{ +is_zeroGrob(x) +} +\arguments{ +\item{x}{A grob} +} +\value{ +A logical value +} +\description{ +Function to check if a grob is a zero grob +} diff --git a/tests/testthat/_snaps/draw_/bottom-right-half-width-height.svg b/tests/testthat/_snaps/draw_/bottom-right-half-width-height.svg new file mode 100644 index 0000000..1c2afc6 --- /dev/null +++ b/tests/testthat/_snaps/draw_/bottom-right-half-width-height.svg @@ -0,0 +1,56 @@ + + + + + + + + + + + + + + + + + + + + + + + + + +0.00 +0.25 +0.50 +0.75 +1.00 + + + + + + + + + + +0.00 +0.25 +0.50 +0.75 +1.00 +Bottom right, half width/height + + diff --git a/tests/testthat/_snaps/draw_/centered-full-width-height.svg b/tests/testthat/_snaps/draw_/centered-full-width-height.svg new file mode 100644 index 0000000..5199b80 --- /dev/null +++ b/tests/testthat/_snaps/draw_/centered-full-width-height.svg @@ -0,0 +1,56 @@ + + + + + + + + + + + + + + + + + + + + + + + + + +0.00 +0.25 +0.50 +0.75 +1.00 + + + + + + + + + + +0.00 +0.25 +0.50 +0.75 +1.00 +Centered, full width/height + + diff --git a/tests/testthat/_snaps/draw_/centered-half-width-height.svg b/tests/testthat/_snaps/draw_/centered-half-width-height.svg new file mode 100644 index 0000000..e32ad3c --- /dev/null +++ b/tests/testthat/_snaps/draw_/centered-half-width-height.svg @@ -0,0 +1,56 @@ + + + + + + + + + + + + + + + + + + + + + + + + + +0.00 +0.25 +0.50 +0.75 +1.00 + + + + + + + + + + +0.00 +0.25 +0.50 +0.75 +1.00 +Centered, half width/height + + diff --git a/tests/testthat/_snaps/draw_/four-corners-aligned-scaled.svg b/tests/testthat/_snaps/draw_/four-corners-aligned-scaled.svg new file mode 100644 index 0000000..53fdd64 --- /dev/null +++ b/tests/testthat/_snaps/draw_/four-corners-aligned-scaled.svg @@ -0,0 +1,59 @@ + + + + + + + + + + + + + + + + + + + + + + + + + + + + +0.00 +0.25 +0.50 +0.75 +1.00 + + + + + + + + + + +0.00 +0.25 +0.50 +0.75 +1.00 +four corners, aligned, scaled + + diff --git a/tests/testthat/_snaps/draw_/four-corners-centered-scaled.svg b/tests/testthat/_snaps/draw_/four-corners-centered-scaled.svg new file mode 100644 index 0000000..7be2f67 --- /dev/null +++ b/tests/testthat/_snaps/draw_/four-corners-centered-scaled.svg @@ -0,0 +1,59 @@ + + + + + + + + + + + + + + + + + + + + + + + + + + + + +0.00 +0.25 +0.50 +0.75 +1.00 + + + + + + + + + + +0.00 +0.25 +0.50 +0.75 +1.00 +four corners, centered, scaled + + diff --git a/tests/testthat/_snaps/draw_/label-color-specified-w-uk-spelling.svg b/tests/testthat/_snaps/draw_/label-color-specified-w-uk-spelling.svg new file mode 100644 index 0000000..29a15ce --- /dev/null +++ b/tests/testthat/_snaps/draw_/label-color-specified-w-uk-spelling.svg @@ -0,0 +1,24 @@ + + + + + + + + + + + + +blue label +Label color specified w/ UK spelling + + diff --git a/tests/testthat/_snaps/draw_/label-color-specified-w-us-spelling.svg b/tests/testthat/_snaps/draw_/label-color-specified-w-us-spelling.svg new file mode 100644 index 0000000..7c66ff2 --- /dev/null +++ b/tests/testthat/_snaps/draw_/label-color-specified-w-us-spelling.svg @@ -0,0 +1,24 @@ + + + + + + + + + + + + +blue label +Label color specified w/ US spelling + + diff --git a/tests/testthat/_snaps/draw_/label-color-taken-from-theme.svg b/tests/testthat/_snaps/draw_/label-color-taken-from-theme.svg new file mode 100644 index 0000000..ae8dc77 --- /dev/null +++ b/tests/testthat/_snaps/draw_/label-color-taken-from-theme.svg @@ -0,0 +1,24 @@ + + + + + + + + + + + + +red label +Label color taken from theme + + diff --git a/tests/testthat/_snaps/draw_/top-left-half-width-height.svg b/tests/testthat/_snaps/draw_/top-left-half-width-height.svg new file mode 100644 index 0000000..582ef22 --- /dev/null +++ b/tests/testthat/_snaps/draw_/top-left-half-width-height.svg @@ -0,0 +1,56 @@ + + + + + + + + + + + + + + + + + + + + + + + + + +0.00 +0.25 +0.50 +0.75 +1.00 + + + + + + + + + + +0.00 +0.25 +0.50 +0.75 +1.00 +Top left, half width/height + + diff --git a/tests/testthat/_snaps/key_glyph/circle-key-glyph-color-used-as-fill.svg b/tests/testthat/_snaps/key_glyph/circle-key-glyph-color-used-as-fill.svg new file mode 100644 index 0000000..10b4646 --- /dev/null +++ b/tests/testthat/_snaps/key_glyph/circle-key-glyph-color-used-as-fill.svg @@ -0,0 +1,47 @@ + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + +x + + +a +b +circle key glyph, color used as fill + + diff --git a/tests/testthat/_snaps/key_glyph/rectangle-key-glyph-color-used-as-fill.svg b/tests/testthat/_snaps/key_glyph/rectangle-key-glyph-color-used-as-fill.svg new file mode 100644 index 0000000..b073f34 --- /dev/null +++ b/tests/testthat/_snaps/key_glyph/rectangle-key-glyph-color-used-as-fill.svg @@ -0,0 +1,47 @@ + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + +x + + +a +b +rectangle key glyph, color used as fill + + diff --git a/tests/testthat/_snaps/plot_grid/aligning-faceted-plots-w-unequal-widths.svg b/tests/testthat/_snaps/plot_grid/aligning-faceted-plots-w-unequal-widths.svg new file mode 100644 index 0000000..e4b9684 --- /dev/null +++ b/tests/testthat/_snaps/plot_grid/aligning-faceted-plots-w-unequal-widths.svg @@ -0,0 +1,367 @@ + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + +a + + + + + + + + + + +b + + + + + + + + + + +c + + + + + + + +1.00 +1.25 +1.50 +1.75 +2.00 + + + + + +1 +2 +3 +4 +5 + + + + + +1.0 +1.5 +2.0 +2.5 +3.0 +0.00 +0.25 +0.50 +0.75 +1.00 + + + + + +xstart +y + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + +a + + + + + + + + + + +b + + + + + + + + + + +c + + + + + + + +1.0 +1.5 +2.0 +2.5 +3.0 + + + + + +1.00 +1.25 +1.50 +1.75 +2.00 + + + + + +1 +2 +3 +4 +5 +0 +250 +500 +750 +1000 + + + + + +xstart +y +aligning faceted plots w/ unequal widths + + diff --git a/tests/testthat/_snaps/plot_grid/basic-plot-arranging-labeling.svg b/tests/testthat/_snaps/plot_grid/basic-plot-arranging-labeling.svg new file mode 100644 index 0000000..530dbb9 --- /dev/null +++ b/tests/testthat/_snaps/plot_grid/basic-plot-arranging-labeling.svg @@ -0,0 +1,87 @@ + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + +A +B + + + + + + + + + + + + + + + + + + + + +C + + + + + + + + + + + + + + + + + + + + +D +basic plot arranging, labeling + + diff --git a/tests/testthat/_snaps/plot_grid/basic-plot-arranging-with-missing-plot.svg b/tests/testthat/_snaps/plot_grid/basic-plot-arranging-with-missing-plot.svg new file mode 100644 index 0000000..dfe708c --- /dev/null +++ b/tests/testthat/_snaps/plot_grid/basic-plot-arranging-with-missing-plot.svg @@ -0,0 +1,83 @@ + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + +basic plot arranging, with missing plot + + diff --git a/tests/testthat/_snaps/plot_grid/colwise-arranging.svg b/tests/testthat/_snaps/plot_grid/colwise-arranging.svg new file mode 100644 index 0000000..cb709ad --- /dev/null +++ b/tests/testthat/_snaps/plot_grid/colwise-arranging.svg @@ -0,0 +1,83 @@ + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + +colwise arranging + + diff --git a/tests/testthat/_snaps/plot_grid/h-v-alignment-right-and-top-axes.svg b/tests/testthat/_snaps/plot_grid/h-v-alignment-right-and-top-axes.svg new file mode 100644 index 0000000..653dff1 --- /dev/null +++ b/tests/testthat/_snaps/plot_grid/h-v-alignment-right-and-top-axes.svg @@ -0,0 +1,237 @@ + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + +a +b +c + + + + + + + + +1.0 +1.5 +2.0 +2.5 +3.0 +x +short + +short + + + + + + +a +b +c + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + +aaaaaaaa + + + + + + + + + + +bbbbbbbb + + + + + + + + + + +ccccccccc + + + + + + + +1.0 +1.5 +2.0 +2.5 +3.0 + + + + + +1.0 +1.5 +2.0 +2.5 +3.0 + + + + + +1.0 +1.5 +2.0 +2.5 +3.0 +a +b +c + + + +x +short +h/v alignment, right and top axes + + diff --git a/tests/testthat/_snaps/plot_grid/horizontal-alignment-bottom-axis.svg b/tests/testthat/_snaps/plot_grid/horizontal-alignment-bottom-axis.svg new file mode 100644 index 0000000..fac9bdb --- /dev/null +++ b/tests/testthat/_snaps/plot_grid/horizontal-alignment-bottom-axis.svg @@ -0,0 +1,237 @@ + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + +a +b +c + + + + + + + + +1.0 +1.5 +2.0 +2.5 +3.0 +x +short + +short + + + + + + +a +b +c + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + +aaaaaaaa + + + + + + + + + + +bbbbbbbb + + + + + + + + + + +ccccccccc + + + + + + + +1.0 +1.5 +2.0 +2.5 +3.0 + + + + + +1.0 +1.5 +2.0 +2.5 +3.0 + + + + + +1.0 +1.5 +2.0 +2.5 +3.0 +a +b +c + + + +x +short +horizontal alignment, bottom axis + + diff --git a/tests/testthat/_snaps/plot_grid/horizontal-alignment-top-axis.svg b/tests/testthat/_snaps/plot_grid/horizontal-alignment-top-axis.svg new file mode 100644 index 0000000..6dc53da --- /dev/null +++ b/tests/testthat/_snaps/plot_grid/horizontal-alignment-top-axis.svg @@ -0,0 +1,237 @@ + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + +a +b +c + + + + + + + + +1.0 +1.5 +2.0 +2.5 +3.0 +x +short + +short + + + + + + +a +b +c + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + +aaaaaaaa + + + + + + + + + + +bbbbbbbb + + + + + + + + + + +ccccccccc + + + + + + + +1.0 +1.5 +2.0 +2.5 +3.0 + + + + + +1.0 +1.5 +2.0 +2.5 +3.0 + + + + + +1.0 +1.5 +2.0 +2.5 +3.0 +a +b +c + + + +x +short +horizontal alignment, top axis + + diff --git a/tests/testthat/_snaps/plot_grid/horizontal-alignment.svg b/tests/testthat/_snaps/plot_grid/horizontal-alignment.svg new file mode 100644 index 0000000..0083015 --- /dev/null +++ b/tests/testthat/_snaps/plot_grid/horizontal-alignment.svg @@ -0,0 +1,129 @@ + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + +a +b +c + + + + + + + + +1.0 +1.5 +2.0 +2.5 +3.0 +x +short + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + +1.0 +1.5 +2.0 +2.5 +3.0 + + + + + + + + +aaaaaaaa +bbbbbbbb +ccccccccc +long +x +horizontal alignment + + diff --git a/tests/testthat/_snaps/plot_grid/horizontal-vertical-alignment.svg b/tests/testthat/_snaps/plot_grid/horizontal-vertical-alignment.svg new file mode 100644 index 0000000..4462530 --- /dev/null +++ b/tests/testthat/_snaps/plot_grid/horizontal-vertical-alignment.svg @@ -0,0 +1,182 @@ + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + +a +b +c + + + + + + + + +1.0 +1.5 +2.0 +2.5 +3.0 +x +short + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + +aaaaaaaa +bbbbbbbb +ccccccccc + + + + + + + + +1.0 +1.5 +2.0 +2.5 +3.0 +x +long + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + +1.0 +1.5 +2.0 +2.5 +3.0 + + + + + + + + +aaaaaaaa +bbbbbbbb +ccccccccc +long +x +horizontal & vertical alignment + + diff --git a/tests/testthat/_snaps/plot_grid/scaling-plots.svg b/tests/testthat/_snaps/plot_grid/scaling-plots.svg new file mode 100644 index 0000000..5f62af1 --- /dev/null +++ b/tests/testthat/_snaps/plot_grid/scaling-plots.svg @@ -0,0 +1,86 @@ + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + +A + + + + + + + + + + + + + + + + + + + + +B + + + + + + + + + + + + + + + + + + + + +C +scaling plots + + diff --git a/tests/testthat/_snaps/plot_grid/setting-heights-1-1-2.svg b/tests/testthat/_snaps/plot_grid/setting-heights-1-1-2.svg new file mode 100644 index 0000000..9624694 --- /dev/null +++ b/tests/testthat/_snaps/plot_grid/setting-heights-1-1-2.svg @@ -0,0 +1,83 @@ + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + +setting heights, 1:1:2 + + diff --git a/tests/testthat/_snaps/plot_grid/setting-widths-1-1-2.svg b/tests/testthat/_snaps/plot_grid/setting-widths-1-1-2.svg new file mode 100644 index 0000000..edca96c --- /dev/null +++ b/tests/testthat/_snaps/plot_grid/setting-widths-1-1-2.svg @@ -0,0 +1,83 @@ + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + +setting widths, 1:1:2 + + diff --git a/tests/testthat/_snaps/plot_grid/vertical-alignment-left-axis.svg b/tests/testthat/_snaps/plot_grid/vertical-alignment-left-axis.svg new file mode 100644 index 0000000..3cb5fc1 --- /dev/null +++ b/tests/testthat/_snaps/plot_grid/vertical-alignment-left-axis.svg @@ -0,0 +1,237 @@ + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + +a +b +c + + + + + + + + +1.0 +1.5 +2.0 +2.5 +3.0 +x +short + +short + + + + + + +a +b +c + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + +aaaaaaaa + + + + + + + + + + +bbbbbbbb + + + + + + + + + + +ccccccccc + + + + + + + +1.0 +1.5 +2.0 +2.5 +3.0 + + + + + +1.0 +1.5 +2.0 +2.5 +3.0 + + + + + +1.0 +1.5 +2.0 +2.5 +3.0 +a +b +c + + + +x +short +vertical alignment, left axis + + diff --git a/tests/testthat/_snaps/plot_grid/vertical-alignment-right-axis.svg b/tests/testthat/_snaps/plot_grid/vertical-alignment-right-axis.svg new file mode 100644 index 0000000..c9e4141 --- /dev/null +++ b/tests/testthat/_snaps/plot_grid/vertical-alignment-right-axis.svg @@ -0,0 +1,237 @@ + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + +a +b +c + + + + + + + + +1.0 +1.5 +2.0 +2.5 +3.0 +x +short + +short + + + + + + +a +b +c + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + +aaaaaaaa + + + + + + + + + + +bbbbbbbb + + + + + + + + + + +ccccccccc + + + + + + + +1.0 +1.5 +2.0 +2.5 +3.0 + + + + + +1.0 +1.5 +2.0 +2.5 +3.0 + + + + + +1.0 +1.5 +2.0 +2.5 +3.0 +a +b +c + + + +x +short +vertical alignment, right axis + + diff --git a/tests/testthat/_snaps/plot_grid/vertical-alignment.svg b/tests/testthat/_snaps/plot_grid/vertical-alignment.svg new file mode 100644 index 0000000..fc7846d --- /dev/null +++ b/tests/testthat/_snaps/plot_grid/vertical-alignment.svg @@ -0,0 +1,129 @@ + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + +a +b +c + + + + + + + + +1.0 +1.5 +2.0 +2.5 +3.0 +x +short + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + +aaaaaaaa +bbbbbbbb +ccccccccc + + + + + + + + +1.0 +1.5 +2.0 +2.5 +3.0 +x +long +vertical alignment + + diff --git a/tests/testthat/_snaps/themes/theme-half-open-huge.svg b/tests/testthat/_snaps/themes/theme-half-open-huge.svg new file mode 100644 index 0000000..8267148 --- /dev/null +++ b/tests/testthat/_snaps/themes/theme-half-open-huge.svg @@ -0,0 +1,103 @@ + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + +100 +200 +300 +400 + + + + + + + + +100 +200 +300 +hp +disp +factor(cyl) + + + +4 +6 +8 +mpg + + + + + + + + + +15 +20 +25 +30 +theme half open huge + + diff --git a/tests/testthat/_snaps/themes/theme-half-open-tiny.svg b/tests/testthat/_snaps/themes/theme-half-open-tiny.svg new file mode 100644 index 0000000..8ac5679 --- /dev/null +++ b/tests/testthat/_snaps/themes/theme-half-open-tiny.svg @@ -0,0 +1,103 @@ + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + +100 +200 +300 +400 + + + + + + + + +100 +200 +300 +hp +disp +factor(cyl) + + + +4 +6 +8 +mpg + + + + + + + + + +15 +20 +25 +30 +theme half open tiny + + diff --git a/tests/testthat/_snaps/themes/theme-half-open.svg b/tests/testthat/_snaps/themes/theme-half-open.svg new file mode 100644 index 0000000..f30cb95 --- /dev/null +++ b/tests/testthat/_snaps/themes/theme-half-open.svg @@ -0,0 +1,103 @@ + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + +100 +200 +300 +400 + + + + + + + + +100 +200 +300 +hp +disp +factor(cyl) + + + +4 +6 +8 +mpg + + + + + + + + + +15 +20 +25 +30 +theme half open + + diff --git a/tests/testthat/_snaps/themes/theme-minimal-grid-huge.svg b/tests/testthat/_snaps/themes/theme-minimal-grid-huge.svg new file mode 100644 index 0000000..7ec1479 --- /dev/null +++ b/tests/testthat/_snaps/themes/theme-minimal-grid-huge.svg @@ -0,0 +1,108 @@ + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + +100 +200 +300 +400 + + + + + + + +100 +200 +300 +hp +disp +factor(cyl) + + + +4 +6 +8 +mpg + + + + + + + + + +15 +20 +25 +30 +theme minimal grid huge + + diff --git a/tests/testthat/_snaps/themes/theme-minimal-grid-tiny.svg b/tests/testthat/_snaps/themes/theme-minimal-grid-tiny.svg new file mode 100644 index 0000000..b04a5ba --- /dev/null +++ b/tests/testthat/_snaps/themes/theme-minimal-grid-tiny.svg @@ -0,0 +1,108 @@ + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + +100 +200 +300 +400 + + + + + + + +100 +200 +300 +hp +disp +factor(cyl) + + + +4 +6 +8 +mpg + + + + + + + + + +15 +20 +25 +30 +theme minimal grid tiny + + diff --git a/tests/testthat/_snaps/themes/theme-minimal-grid.svg b/tests/testthat/_snaps/themes/theme-minimal-grid.svg new file mode 100644 index 0000000..f4aabdc --- /dev/null +++ b/tests/testthat/_snaps/themes/theme-minimal-grid.svg @@ -0,0 +1,108 @@ + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + +100 +200 +300 +400 + + + + + + + +100 +200 +300 +hp +disp +factor(cyl) + + + +4 +6 +8 +mpg + + + + + + + + + +15 +20 +25 +30 +theme minimal grid + + diff --git a/tests/testthat/_snaps/themes/theme-minimal-hgrid-huge.svg b/tests/testthat/_snaps/themes/theme-minimal-hgrid-huge.svg new file mode 100644 index 0000000..fe94374 --- /dev/null +++ b/tests/testthat/_snaps/themes/theme-minimal-hgrid-huge.svg @@ -0,0 +1,106 @@ + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + +100 +200 +300 +400 + + + + + + + + +100 +200 +300 +hp +disp +factor(cyl) + + + +4 +6 +8 +mpg + + + + + + + + + +15 +20 +25 +30 +theme minimal hgrid huge + + diff --git a/tests/testthat/_snaps/themes/theme-minimal-hgrid-tiny.svg b/tests/testthat/_snaps/themes/theme-minimal-hgrid-tiny.svg new file mode 100644 index 0000000..fe6ca09 --- /dev/null +++ b/tests/testthat/_snaps/themes/theme-minimal-hgrid-tiny.svg @@ -0,0 +1,106 @@ + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + +100 +200 +300 +400 + + + + + + + + +100 +200 +300 +hp +disp +factor(cyl) + + + +4 +6 +8 +mpg + + + + + + + + + +15 +20 +25 +30 +theme minimal hgrid tiny + + diff --git a/tests/testthat/_snaps/themes/theme-minimal-hgrid.svg b/tests/testthat/_snaps/themes/theme-minimal-hgrid.svg new file mode 100644 index 0000000..760d618 --- /dev/null +++ b/tests/testthat/_snaps/themes/theme-minimal-hgrid.svg @@ -0,0 +1,106 @@ + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + +100 +200 +300 +400 + + + + + + + + +100 +200 +300 +hp +disp +factor(cyl) + + + +4 +6 +8 +mpg + + + + + + + + + +15 +20 +25 +30 +theme minimal hgrid + + diff --git a/tests/testthat/_snaps/themes/theme-minimal-vgrid-huge.svg b/tests/testthat/_snaps/themes/theme-minimal-vgrid-huge.svg new file mode 100644 index 0000000..a4fabd3 --- /dev/null +++ b/tests/testthat/_snaps/themes/theme-minimal-vgrid-huge.svg @@ -0,0 +1,105 @@ + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + +100 +200 +300 +400 + + + + + + + +100 +200 +300 +hp +disp +factor(cyl) + + + +4 +6 +8 +mpg + + + + + + + + + +15 +20 +25 +30 +theme minimal vgrid huge + + diff --git a/tests/testthat/_snaps/themes/theme-minimal-vgrid-tiny.svg b/tests/testthat/_snaps/themes/theme-minimal-vgrid-tiny.svg new file mode 100644 index 0000000..8426766 --- /dev/null +++ b/tests/testthat/_snaps/themes/theme-minimal-vgrid-tiny.svg @@ -0,0 +1,105 @@ + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + +100 +200 +300 +400 + + + + + + + +100 +200 +300 +hp +disp +factor(cyl) + + + +4 +6 +8 +mpg + + + + + + + + + +15 +20 +25 +30 +theme minimal vgrid tiny + + diff --git a/tests/testthat/_snaps/themes/theme-minimal-vgrid.svg b/tests/testthat/_snaps/themes/theme-minimal-vgrid.svg new file mode 100644 index 0000000..a40d7db --- /dev/null +++ b/tests/testthat/_snaps/themes/theme-minimal-vgrid.svg @@ -0,0 +1,105 @@ + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + +100 +200 +300 +400 + + + + + + + +100 +200 +300 +hp +disp +factor(cyl) + + + +4 +6 +8 +mpg + + + + + + + + + +15 +20 +25 +30 +theme minimal vgrid + + From ea5ad1002c382f06d5be7bd2e8a734298ab71186 Mon Sep 17 00:00:00 2001 From: J Kyle Armstrong Date: Thu, 27 Jun 2024 23:09:14 -0400 Subject: [PATCH 2/3] Update get_legend.Rd --- man/get_legend.Rd | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/man/get_legend.Rd b/man/get_legend.Rd index 4248371..f3c7a51 100644 --- a/man/get_legend.Rd +++ b/man/get_legend.Rd @@ -4,7 +4,7 @@ \alias{get_legend} \title{Retrieve the legend of a plot} \usage{ -get_legend(plot) +get_legend(plot, return_all = FALSE) } \arguments{ \item{plot}{A ggplot or gtable from which to retrieve the legend} From 01a7969a5222dd07e3d5b394fd80fdd195e1fa3d Mon Sep 17 00:00:00 2001 From: J Kyle Armstrong Date: Thu, 27 Jun 2024 23:20:42 -0400 Subject: [PATCH 3/3] Update test_get_legend.R updated test so now null is always expected --- tests/testthat/test_get_legend.R | 11 ++--------- 1 file changed, 2 insertions(+), 9 deletions(-) diff --git a/tests/testthat/test_get_legend.R b/tests/testthat/test_get_legend.R index 1a9582b..133660c 100644 --- a/tests/testthat/test_get_legend.R +++ b/tests/testthat/test_get_legend.R @@ -10,14 +10,7 @@ test_that("get legend", { expect_equal(l$name, "guide-box") # return null legend if no legend - # Note by Teun: It is yet unclear to me what the desired behaviour of - # `get_legend()` is in the face of multiple legends. - # For now, this test is conditional on ggplot2 version, but this is - # more of a duct tape solution than a reflection of the intended behaviour - if (utils::packageVersion("ggplot2") >= "3.5.0") { - expect_s3_class(get_legend(p + theme(legend.position = "none")), "zeroGrob") - } else { - expect_null(get_legend(p + theme(legend.position = "none"))) - } + expect_null(get_legend(p + theme(legend.position = "none"))) + })