From 1e82015c23f4e43363893d7a8887364626bfebb2 Mon Sep 17 00:00:00 2001 From: Matthew Kay Date: Tue, 15 Mar 2022 16:24:02 -0500 Subject: [PATCH 1/5] orientation-aware key glyphs for boxplot/crossbar/linerange/pointrange, for #4732 --- R/geom-linerange.r | 2 +- R/legend-draw.r | 18 ++++- .../_snaps/draw-key/horizontal-boxplot.svg | 77 +++++++++++++++++++ .../_snaps/draw-key/horizontal-crossbar.svg | 69 +++++++++++++++++ .../_snaps/draw-key/horizontal-linerange.svg | 63 +++++++++++++++ .../_snaps/draw-key/horizontal-pointrange.svg | 67 ++++++++++++++++ .../_snaps/draw-key/vertical-boxplot.svg | 75 ++++++++++++++++++ .../_snaps/draw-key/vertical-crossbar.svg | 67 ++++++++++++++++ .../_snaps/draw-key/vertical-linerange.svg | 63 +++++++++++++++ .../_snaps/draw-key/vertical-pointrange.svg | 67 ++++++++++++++++ tests/testthat/test-draw-key.R | 61 +++++++++++++++ 11 files changed, 625 insertions(+), 4 deletions(-) create mode 100755 tests/testthat/_snaps/draw-key/horizontal-boxplot.svg create mode 100755 tests/testthat/_snaps/draw-key/horizontal-crossbar.svg create mode 100755 tests/testthat/_snaps/draw-key/horizontal-linerange.svg create mode 100755 tests/testthat/_snaps/draw-key/horizontal-pointrange.svg create mode 100755 tests/testthat/_snaps/draw-key/vertical-boxplot.svg create mode 100755 tests/testthat/_snaps/draw-key/vertical-crossbar.svg create mode 100755 tests/testthat/_snaps/draw-key/vertical-linerange.svg create mode 100755 tests/testthat/_snaps/draw-key/vertical-pointrange.svg diff --git a/R/geom-linerange.r b/R/geom-linerange.r index 5e9462013b..2d5bf284d6 100644 --- a/R/geom-linerange.r +++ b/R/geom-linerange.r @@ -93,7 +93,7 @@ geom_linerange <- function(mapping = NULL, data = NULL, GeomLinerange <- ggproto("GeomLinerange", Geom, default_aes = aes(colour = "black", size = 0.5, linetype = 1, alpha = NA), - draw_key = draw_key_vpath, + draw_key = draw_key_linerange, required_aes = c("x|y", "ymin|xmin", "ymax|xmax"), diff --git a/R/legend-draw.r b/R/legend-draw.r index ac02b97fea..61b1352426 100644 --- a/R/legend-draw.r +++ b/R/legend-draw.r @@ -106,7 +106,8 @@ draw_key_boxplot <- function(data, params, size) { lty = data$linetype %||% 1, lineend = params$lineend %||% "butt", linejoin = params$linejoin %||% "mitre" - ) + ), + vp = if (isTRUE(params$flipped_aes)) viewport(angle = 90) ) } @@ -123,7 +124,8 @@ draw_key_crossbar <- function(data, params, size) { lty = data$linetype %||% 1, lineend = params$lineend %||% "butt", linejoin = params$linejoin %||% "mitre" - ) + ), + vp = if (isTRUE(params$flipped_aes)) viewport(angle = 90) ) } @@ -177,11 +179,21 @@ draw_key_dotplot <- function(data, params, size) { ) } +#' @export +#' @rdname draw_key +draw_key_linerange <- function(data, params, size) { + if (isTRUE(params$flipped_aes)) { + draw_key_path(data, params, size) + } else { + draw_key_vpath(data, params, size) + } +} + #' @export #' @rdname draw_key draw_key_pointrange <- function(data, params, size) { grobTree( - draw_key_vpath(data, params, size), + draw_key_linerange(data, params, size), draw_key_point(transform(data, size = (data$size %||% 1.5) * 4), params) ) } diff --git a/tests/testthat/_snaps/draw-key/horizontal-boxplot.svg b/tests/testthat/_snaps/draw-key/horizontal-boxplot.svg new file mode 100755 index 0000000000..c241a915bc --- /dev/null +++ b/tests/testthat/_snaps/draw-key/horizontal-boxplot.svg @@ -0,0 +1,77 @@ + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + +a +b + + + + + + + + +-1 +0 +1 +2 +3 +4 +middle +group + +group + + + + + + + + + + + + +a +b +horizontal boxplot + + diff --git a/tests/testthat/_snaps/draw-key/horizontal-crossbar.svg b/tests/testthat/_snaps/draw-key/horizontal-crossbar.svg new file mode 100755 index 0000000000..8bc8e5c63a --- /dev/null +++ b/tests/testthat/_snaps/draw-key/horizontal-crossbar.svg @@ -0,0 +1,69 @@ + + + + + + + + + + + + + + + + + + + + + + + + + + + + +a +b + + + + + + + + +-1 +0 +1 +2 +3 +4 +middle +group + +group + + + + + + + + +a +b +horizontal crossbar + + diff --git a/tests/testthat/_snaps/draw-key/horizontal-linerange.svg b/tests/testthat/_snaps/draw-key/horizontal-linerange.svg new file mode 100755 index 0000000000..940851b5f4 --- /dev/null +++ b/tests/testthat/_snaps/draw-key/horizontal-linerange.svg @@ -0,0 +1,63 @@ + + + + + + + + + + + + + + + + + + + + + + + + + + +a +b + + + + + + + + +-1 +0 +1 +2 +3 +4 +middle +group + +group + + + + +a +b +horizontal linerange + + diff --git a/tests/testthat/_snaps/draw-key/horizontal-pointrange.svg b/tests/testthat/_snaps/draw-key/horizontal-pointrange.svg new file mode 100755 index 0000000000..3714848a87 --- /dev/null +++ b/tests/testthat/_snaps/draw-key/horizontal-pointrange.svg @@ -0,0 +1,67 @@ + + + + + + + + + + + + + + + + + + + + + + + + + + + + +a +b + + + + + + + + +-1 +0 +1 +2 +3 +4 +middle +group + +group + + + + + + +a +b +horizontal pointrange + + diff --git a/tests/testthat/_snaps/draw-key/vertical-boxplot.svg b/tests/testthat/_snaps/draw-key/vertical-boxplot.svg new file mode 100755 index 0000000000..10f60b6ddc --- /dev/null +++ b/tests/testthat/_snaps/draw-key/vertical-boxplot.svg @@ -0,0 +1,75 @@ + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + +-1 +0 +1 +2 +3 +4 + + + + + + + + +a +b +group +middle + +group + + + + + + + + + + +a +b +vertical boxplot + + diff --git a/tests/testthat/_snaps/draw-key/vertical-crossbar.svg b/tests/testthat/_snaps/draw-key/vertical-crossbar.svg new file mode 100755 index 0000000000..7b515555a7 --- /dev/null +++ b/tests/testthat/_snaps/draw-key/vertical-crossbar.svg @@ -0,0 +1,67 @@ + + + + + + + + + + + + + + + + + + + + + + + + + + + + +-1 +0 +1 +2 +3 +4 + + + + + + + + +a +b +group +middle + +group + + + + + + +a +b +vertical crossbar + + diff --git a/tests/testthat/_snaps/draw-key/vertical-linerange.svg b/tests/testthat/_snaps/draw-key/vertical-linerange.svg new file mode 100755 index 0000000000..f60714ef79 --- /dev/null +++ b/tests/testthat/_snaps/draw-key/vertical-linerange.svg @@ -0,0 +1,63 @@ + + + + + + + + + + + + + + + + + + + + + + + + + + +-1 +0 +1 +2 +3 +4 + + + + + + + + +a +b +group +middle + +group + + + + +a +b +vertical linerange + + diff --git a/tests/testthat/_snaps/draw-key/vertical-pointrange.svg b/tests/testthat/_snaps/draw-key/vertical-pointrange.svg new file mode 100755 index 0000000000..e53cab0956 --- /dev/null +++ b/tests/testthat/_snaps/draw-key/vertical-pointrange.svg @@ -0,0 +1,67 @@ + + + + + + + + + + + + + + + + + + + + + + + + + + + + +-1 +0 +1 +2 +3 +4 + + + + + + + + +a +b +group +middle + +group + + + + + + +a +b +vertical pointrange + + diff --git a/tests/testthat/test-draw-key.R b/tests/testthat/test-draw-key.R index d378473b20..ba56605b46 100644 --- a/tests/testthat/test-draw-key.R +++ b/tests/testthat/test-draw-key.R @@ -17,3 +17,64 @@ test_that("alternative key glyphs work", { geom_point(aes(fill = z), pch = 21, size = 3, stroke = 2, key_glyph = draw_key_dotplot) ) }) + +# Orientation-aware key glyphs -------------------------------------------- + +test_that("orientation-aware key glyphs work", { + df <- data.frame( + middle = 1:2, + lower = 0:1, + upper = 2:3, + min = -1:0, + max = 3:4, + group = c("a","b") + ) + + p_horizontal <- ggplot(df, aes( + x = middle, + y = group, + xmiddle = middle, + xlower = lower, + xupper = upper, + xmin = min, + xmax = max, + color = group + )) + + p_vertical <- ggplot(df, aes( + x = group, + y = middle, + middle = middle, + lower = lower, + upper = upper, + ymin = min, + ymax = max, + color = group + )) + + expect_doppelganger("vertical boxplot", + p_vertical + geom_boxplot(stat = "identity") + ) + expect_doppelganger("vertical crossbar", + p_vertical + geom_crossbar() + ) + expect_doppelganger("vertical pointrange", + p_vertical + geom_pointrange() + ) + expect_doppelganger("vertical linerange", + p_vertical + geom_linerange() + ) + + expect_doppelganger("horizontal boxplot", + p_horizontal + geom_boxplot(stat = "identity") + ) + expect_doppelganger("horizontal crossbar", + p_horizontal + geom_crossbar() + ) + expect_doppelganger("horizontal pointrange", + p_horizontal + geom_pointrange() + ) + expect_doppelganger("horizontal linerange", + p_horizontal + geom_linerange() + ) +}) From 55ce0fd40a15655db0371e6142e06ac82bba09a9 Mon Sep 17 00:00:00 2001 From: Matthew Kay Date: Tue, 15 Mar 2022 16:26:24 -0500 Subject: [PATCH 2/5] document draw_key_linerange --- NAMESPACE | 1 + man/draw_key.Rd | 3 +++ 2 files changed, 4 insertions(+) diff --git a/NAMESPACE b/NAMESPACE index 143c78b767..a931b99665 100644 --- a/NAMESPACE +++ b/NAMESPACE @@ -305,6 +305,7 @@ export(draw_key_boxplot) export(draw_key_crossbar) export(draw_key_dotplot) export(draw_key_label) +export(draw_key_linerange) export(draw_key_path) export(draw_key_point) export(draw_key_pointrange) diff --git a/man/draw_key.Rd b/man/draw_key.Rd index 302855aced..1c0dfc7761 100644 --- a/man/draw_key.Rd +++ b/man/draw_key.Rd @@ -12,6 +12,7 @@ \alias{draw_key_path} \alias{draw_key_vpath} \alias{draw_key_dotplot} +\alias{draw_key_linerange} \alias{draw_key_pointrange} \alias{draw_key_smooth} \alias{draw_key_text} @@ -40,6 +41,8 @@ draw_key_vpath(data, params, size) draw_key_dotplot(data, params, size) +draw_key_linerange(data, params, size) + draw_key_pointrange(data, params, size) draw_key_smooth(data, params, size) From df11b07c3cc2b9a162992896f0176cd5a490f7bb Mon Sep 17 00:00:00 2001 From: Matthew Kay Date: Tue, 15 Mar 2022 16:31:48 -0500 Subject: [PATCH 3/5] add news entry for orientation-aware key glyphs (#4732) --- NEWS.md | 2 ++ 1 file changed, 2 insertions(+) diff --git a/NEWS.md b/NEWS.md index 64dc53b67c..12b9dd961c 100644 --- a/NEWS.md +++ b/NEWS.md @@ -64,6 +64,8 @@ * Updated documentation for `geom_contour()` to correctly reflect argument precedence between `bins` and `binwidth`. (@eliocamp, #4651) +* Key glyphs for `geom_boxplot()`, `geom_crossbar()`, `geom_pointrange()`, and + `geom_linerange()` are now orientation-aware (@mjskay, #4732) # ggplot2 3.3.5 This is a very small release focusing on fixing a couple of untenable issues From 53da9e0570350445cb75565b60ab36a4421e6707 Mon Sep 17 00:00:00 2001 From: Matthew Kay Date: Tue, 15 Mar 2022 19:48:03 -0500 Subject: [PATCH 4/5] should rotate key glyphs clockwise --- R/legend-draw.r | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/R/legend-draw.r b/R/legend-draw.r index 61b1352426..aebcc83c36 100644 --- a/R/legend-draw.r +++ b/R/legend-draw.r @@ -107,7 +107,7 @@ draw_key_boxplot <- function(data, params, size) { lineend = params$lineend %||% "butt", linejoin = params$linejoin %||% "mitre" ), - vp = if (isTRUE(params$flipped_aes)) viewport(angle = 90) + vp = if (isTRUE(params$flipped_aes)) viewport(angle = -90) ) } @@ -125,7 +125,7 @@ draw_key_crossbar <- function(data, params, size) { lineend = params$lineend %||% "butt", linejoin = params$linejoin %||% "mitre" ), - vp = if (isTRUE(params$flipped_aes)) viewport(angle = 90) + vp = if (isTRUE(params$flipped_aes)) viewport(angle = -90) ) } From fae01e31d0e9ebd76d3c66b8710272bda1707f0a Mon Sep 17 00:00:00 2001 From: Matthew Kay Date: Tue, 15 Mar 2022 19:50:57 -0500 Subject: [PATCH 5/5] condense orientation-aware key glyph visual tests --- .../horizontal-boxplot-and-crossbar.svg | 97 +++++++++++++++++++ .../_snaps/draw-key/horizontal-boxplot.svg | 77 --------------- .../_snaps/draw-key/horizontal-crossbar.svg | 69 ------------- .../horizontal-linerange-and-pointrange.svg | 81 ++++++++++++++++ .../_snaps/draw-key/horizontal-linerange.svg | 63 ------------ .../_snaps/draw-key/horizontal-pointrange.svg | 67 ------------- .../_snaps/draw-key/vertical-boxplot.svg | 75 -------------- .../_snaps/draw-key/vertical-crossbar.svg | 67 ------------- .../_snaps/draw-key/vertical-linerange.svg | 63 ------------ .../_snaps/draw-key/vertical-pointrange.svg | 67 ------------- tests/testthat/test-draw-key.R | 53 +++------- 11 files changed, 191 insertions(+), 588 deletions(-) create mode 100755 tests/testthat/_snaps/draw-key/horizontal-boxplot-and-crossbar.svg delete mode 100755 tests/testthat/_snaps/draw-key/horizontal-boxplot.svg delete mode 100755 tests/testthat/_snaps/draw-key/horizontal-crossbar.svg create mode 100755 tests/testthat/_snaps/draw-key/horizontal-linerange-and-pointrange.svg delete mode 100755 tests/testthat/_snaps/draw-key/horizontal-linerange.svg delete mode 100755 tests/testthat/_snaps/draw-key/horizontal-pointrange.svg delete mode 100755 tests/testthat/_snaps/draw-key/vertical-boxplot.svg delete mode 100755 tests/testthat/_snaps/draw-key/vertical-crossbar.svg delete mode 100755 tests/testthat/_snaps/draw-key/vertical-linerange.svg delete mode 100755 tests/testthat/_snaps/draw-key/vertical-pointrange.svg diff --git a/tests/testthat/_snaps/draw-key/horizontal-boxplot-and-crossbar.svg b/tests/testthat/_snaps/draw-key/horizontal-boxplot-and-crossbar.svg new file mode 100755 index 0000000000..3d583d9ea6 --- /dev/null +++ b/tests/testthat/_snaps/draw-key/horizontal-boxplot-and-crossbar.svg @@ -0,0 +1,97 @@ + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + +a +b +c +d + + + + + + + + + + +-1 +0 +1 +2 +3 +4 +middle +group1 + +group2 + + + + + + + + +c +d + +group1 + + + + + + + + + + + + +a +b +horizontal boxplot and crossbar + + diff --git a/tests/testthat/_snaps/draw-key/horizontal-boxplot.svg b/tests/testthat/_snaps/draw-key/horizontal-boxplot.svg deleted file mode 100755 index c241a915bc..0000000000 --- a/tests/testthat/_snaps/draw-key/horizontal-boxplot.svg +++ /dev/null @@ -1,77 +0,0 @@ - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -a -b - - - - - - - - --1 -0 -1 -2 -3 -4 -middle -group - -group - - - - - - - - - - - - -a -b -horizontal boxplot - - diff --git a/tests/testthat/_snaps/draw-key/horizontal-crossbar.svg b/tests/testthat/_snaps/draw-key/horizontal-crossbar.svg deleted file mode 100755 index 8bc8e5c63a..0000000000 --- a/tests/testthat/_snaps/draw-key/horizontal-crossbar.svg +++ /dev/null @@ -1,69 +0,0 @@ - - - - - - - - - - - - - - - - - - - - - - - - - - - - -a -b - - - - - - - - --1 -0 -1 -2 -3 -4 -middle -group - -group - - - - - - - - -a -b -horizontal crossbar - - diff --git a/tests/testthat/_snaps/draw-key/horizontal-linerange-and-pointrange.svg b/tests/testthat/_snaps/draw-key/horizontal-linerange-and-pointrange.svg new file mode 100755 index 0000000000..a427b44746 --- /dev/null +++ b/tests/testthat/_snaps/draw-key/horizontal-linerange-and-pointrange.svg @@ -0,0 +1,81 @@ + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + +a +b +c +d + + + + + + + + + + +-1 +0 +1 +2 +3 +4 +middle +group1 + +group2 + + + + + + +c +d + +group1 + + + + +a +b +horizontal linerange and pointrange + + diff --git a/tests/testthat/_snaps/draw-key/horizontal-linerange.svg b/tests/testthat/_snaps/draw-key/horizontal-linerange.svg deleted file mode 100755 index 940851b5f4..0000000000 --- a/tests/testthat/_snaps/draw-key/horizontal-linerange.svg +++ /dev/null @@ -1,63 +0,0 @@ - - - - - - - - - - - - - - - - - - - - - - - - - - -a -b - - - - - - - - --1 -0 -1 -2 -3 -4 -middle -group - -group - - - - -a -b -horizontal linerange - - diff --git a/tests/testthat/_snaps/draw-key/horizontal-pointrange.svg b/tests/testthat/_snaps/draw-key/horizontal-pointrange.svg deleted file mode 100755 index 3714848a87..0000000000 --- a/tests/testthat/_snaps/draw-key/horizontal-pointrange.svg +++ /dev/null @@ -1,67 +0,0 @@ - - - - - - - - - - - - - - - - - - - - - - - - - - - - -a -b - - - - - - - - --1 -0 -1 -2 -3 -4 -middle -group - -group - - - - - - -a -b -horizontal pointrange - - diff --git a/tests/testthat/_snaps/draw-key/vertical-boxplot.svg b/tests/testthat/_snaps/draw-key/vertical-boxplot.svg deleted file mode 100755 index 10f60b6ddc..0000000000 --- a/tests/testthat/_snaps/draw-key/vertical-boxplot.svg +++ /dev/null @@ -1,75 +0,0 @@ - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - --1 -0 -1 -2 -3 -4 - - - - - - - - -a -b -group -middle - -group - - - - - - - - - - -a -b -vertical boxplot - - diff --git a/tests/testthat/_snaps/draw-key/vertical-crossbar.svg b/tests/testthat/_snaps/draw-key/vertical-crossbar.svg deleted file mode 100755 index 7b515555a7..0000000000 --- a/tests/testthat/_snaps/draw-key/vertical-crossbar.svg +++ /dev/null @@ -1,67 +0,0 @@ - - - - - - - - - - - - - - - - - - - - - - - - - - - - --1 -0 -1 -2 -3 -4 - - - - - - - - -a -b -group -middle - -group - - - - - - -a -b -vertical crossbar - - diff --git a/tests/testthat/_snaps/draw-key/vertical-linerange.svg b/tests/testthat/_snaps/draw-key/vertical-linerange.svg deleted file mode 100755 index f60714ef79..0000000000 --- a/tests/testthat/_snaps/draw-key/vertical-linerange.svg +++ /dev/null @@ -1,63 +0,0 @@ - - - - - - - - - - - - - - - - - - - - - - - - - - --1 -0 -1 -2 -3 -4 - - - - - - - - -a -b -group -middle - -group - - - - -a -b -vertical linerange - - diff --git a/tests/testthat/_snaps/draw-key/vertical-pointrange.svg b/tests/testthat/_snaps/draw-key/vertical-pointrange.svg deleted file mode 100755 index e53cab0956..0000000000 --- a/tests/testthat/_snaps/draw-key/vertical-pointrange.svg +++ /dev/null @@ -1,67 +0,0 @@ - - - - - - - - - - - - - - - - - - - - - - - - - - - - --1 -0 -1 -2 -3 -4 - - - - - - - - -a -b -group -middle - -group - - - - - - -a -b -vertical pointrange - - diff --git a/tests/testthat/test-draw-key.R b/tests/testthat/test-draw-key.R index ba56605b46..aeba592a6c 100644 --- a/tests/testthat/test-draw-key.R +++ b/tests/testthat/test-draw-key.R @@ -20,61 +20,34 @@ test_that("alternative key glyphs work", { # Orientation-aware key glyphs -------------------------------------------- -test_that("orientation-aware key glyphs work", { +test_that("horizontal key glyphs work", { df <- data.frame( middle = 1:2, lower = 0:1, upper = 2:3, min = -1:0, max = 3:4, - group = c("a","b") + group1 = c("a","b"), + group2 = c("c","d") ) - p_horizontal <- ggplot(df, aes( + p <- ggplot(df, aes( x = middle, - y = group, xmiddle = middle, xlower = lower, xupper = upper, xmin = min, - xmax = max, - color = group + xmax = max )) - p_vertical <- ggplot(df, aes( - x = group, - y = middle, - middle = middle, - lower = lower, - upper = upper, - ymin = min, - ymax = max, - color = group - )) - - expect_doppelganger("vertical boxplot", - p_vertical + geom_boxplot(stat = "identity") - ) - expect_doppelganger("vertical crossbar", - p_vertical + geom_crossbar() - ) - expect_doppelganger("vertical pointrange", - p_vertical + geom_pointrange() - ) - expect_doppelganger("vertical linerange", - p_vertical + geom_linerange() - ) - - expect_doppelganger("horizontal boxplot", - p_horizontal + geom_boxplot(stat = "identity") - ) - expect_doppelganger("horizontal crossbar", - p_horizontal + geom_crossbar() - ) - expect_doppelganger("horizontal pointrange", - p_horizontal + geom_pointrange() + expect_doppelganger("horizontal boxplot and crossbar", + p + + geom_boxplot(aes(y = group1, color = group1), stat = "identity") + + geom_crossbar(aes(y = group2, fill = group2)) ) - expect_doppelganger("horizontal linerange", - p_horizontal + geom_linerange() + expect_doppelganger("horizontal linerange and pointrange", + p + + geom_linerange(aes(y = group1, color = group1)) + + geom_pointrange(aes(y = group2, shape = group2)) ) })