From b4636170a4537af3143a711ba6fd60e5597b64ba Mon Sep 17 00:00:00 2001 From: Teun van den Brand <49372158+teunbrand@users.noreply.github.com> Date: Wed, 26 Apr 2023 17:07:57 +0200 Subject: [PATCH 01/23] Swap tick anchorpoint --- R/guide-.R | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/R/guide-.R b/R/guide-.R index a1c194bf99..a934d315a3 100644 --- a/R/guide-.R +++ b/R/guide-.R @@ -297,7 +297,7 @@ Guide <- ggproto( pos <- unname(c(top = 1, bottom = 0, left = 0, right = 1)[position]) dir <- -2 * pos + 1 pos <- unit(rep(pos, 2 * n_breaks), "npc") - dir <- rep(vec_interleave(0, dir), n_breaks) * tick_len + dir <- rep(vec_interleave(dir, 0), n_breaks) * tick_len tick <- pos + dir # Build grob From 6ef9990552923ad8a97e53079879cc1b3c4579dd Mon Sep 17 00:00:00 2001 From: Teun van den Brand <49372158+teunbrand@users.noreply.github.com> Date: Wed, 26 Apr 2023 17:08:30 +0200 Subject: [PATCH 02/23] `Guide$build_ticks()` accepts a length value --- R/guide-.R | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/R/guide-.R b/R/guide-.R index a934d315a3..fa85275c66 100644 --- a/R/guide-.R +++ b/R/guide-.R @@ -273,7 +273,8 @@ Guide <- ggproto( }, # Renders tickmarks - build_ticks = function(key, elements, params, position = params$position) { + build_ticks = function(key, elements, params, position = params$position, + length = elements$ticks_length) { if (!is.list(key)) { breaks <- key @@ -287,8 +288,7 @@ Guide <- ggproto( return(zeroGrob()) } - tick_len <- rep(elements$ticks_length %||% unit(0.2, "npc"), - length.out = n_breaks) + tick_len <- rep(length %||% unit(0.2, "npc"), length.out = n_breaks) # Resolve mark mark <- unit(rep(breaks, each = 2), "npc") From c45961388b593f485933e1f396fbe48e53df580f Mon Sep 17 00:00:00 2001 From: Teun van den Brand <49372158+teunbrand@users.noreply.github.com> Date: Wed, 26 Apr 2023 17:09:28 +0200 Subject: [PATCH 03/23] Add tick arguments --- R/guide-axis.R | 11 ++++++++++- 1 file changed, 10 insertions(+), 1 deletion(-) diff --git a/R/guide-axis.R b/R/guide-axis.R index 221157fb7f..864f7a30ae 100644 --- a/R/guide-axis.R +++ b/R/guide-axis.R @@ -37,7 +37,9 @@ #' # can also be used to add a duplicate guide #' p + guides(x = guide_axis(n.dodge = 2), y.sec = guide_axis()) guide_axis <- function(title = waiver(), check.overlap = FALSE, angle = NULL, - n.dodge = 1, order = 0, position = waiver()) { + n.dodge = 1, major.length = 1, minor.length = 0.75, + minor.ticks = element_blank(), + order = 0, position = waiver()) { new_guide( title = title, @@ -45,6 +47,9 @@ guide_axis <- function(title = waiver(), check.overlap = FALSE, angle = NULL, check.overlap = check.overlap, angle = angle, n.dodge = n.dodge, + major.length = major.length, + minor.length = minor.length, + minor.ticks = minor.ticks, # parameter available_aes = c("x", "y"), @@ -72,6 +77,9 @@ GuideAxis <- ggproto( direction = NULL, angle = NULL, n.dodge = 1, + major.length = 1, + minor.length = 0.75, + minor.ticks = NULL, order = 0, check.overlap = FALSE ), @@ -158,6 +166,7 @@ GuideAxis <- ggproto( ), calc_element, theme = theme ) + elements$minor_ticks <- combine_elements(params$minor.ticks, elements$ticks) elements }, From 4a7f7b954566ae89060a5c6ae5684f76381a753c Mon Sep 17 00:00:00 2001 From: Teun van den Brand <49372158+teunbrand@users.noreply.github.com> Date: Wed, 26 Apr 2023 17:10:43 +0200 Subject: [PATCH 04/23] `GuideAxis$extract_key()` can get minor ticks --- R/guide-axis.R | 15 +++++++++++++++ 1 file changed, 15 insertions(+) diff --git a/R/guide-axis.R b/R/guide-axis.R index 864f7a30ae..985d9b550f 100644 --- a/R/guide-axis.R +++ b/R/guide-axis.R @@ -95,6 +95,21 @@ GuideAxis <- ggproto( ticks_length = "axis.ticks.length" ), + extract_key = function(scale, aesthetic, minor.ticks, ...) { + major <- Guide$extract_key(scale, aesthetic, ...) + if (inherits(minor.ticks, "element_blank")) { + return(major) + } + if (!is.null(major)) { + major$.type <- "major" + } + minor <- setdiff(scale$get_breaks_minor(), major$.value) + new_scale <- ggproto(NULL, scale, breaks = minor, get_labels = .no_labels) + minor <- Guide$extract_key(new_scale, aesthetic, ...) + minor$.type <- "minor" + vec_rbind(major, minor) + }, + extract_params = function(scale, params, hashables, ...) { params$name <- paste0(params$name, "_", params$aesthetic) Guide$extract_params(scale, params, hashables) From 599632ec2ea799f9a8f9ec209817773dd2dce456 Mon Sep 17 00:00:00 2001 From: Teun van den Brand <49372158+teunbrand@users.noreply.github.com> Date: Wed, 26 Apr 2023 17:11:13 +0200 Subject: [PATCH 05/23] `GuideAxis$build_ticks()` makes draws minor ticks --- R/guide-axis.R | 25 +++++++++++++++++++++++++ 1 file changed, 25 insertions(+) diff --git a/R/guide-axis.R b/R/guide-axis.R index 985d9b550f..ebe3b507ab 100644 --- a/R/guide-axis.R +++ b/R/guide-axis.R @@ -260,6 +260,31 @@ GuideAxis <- ggproto( ) }, + build_ticks = function(key, elements, params, position = params$opposite) { + if (!".type" %in% names(key)) { + ticks <- Guide$build_ticks( + key, elements, params, position, + elements$ticks_length * params$major.length + ) + return(ticks) + } + major <- vec_slice(key, key$.type == "major") + major <- Guide$build_ticks( + major, elements, params, position, + elements$ticks_length * params$major.length + ) + if (inherits(elements$minor_ticks, "element_blank")) { + return(major) + } + elements$ticks <- elements$minor_ticks + minor <- vec_slice(key, key$.type == "minor") + minor <- Guide$build_ticks( + minor, elements, params, position, + elements$ticks_length * params$minor.length + ) + grobTree(major, minor, name = "ticks") + }, + build_labels = function(key, elements, params) { labels <- key$.label n_labels <- length(labels) From 62d9f441f7ead0540a6c697f10bd4e53718a568c Mon Sep 17 00:00:00 2001 From: Teun van den Brand <49372158+teunbrand@users.noreply.github.com> Date: Wed, 26 Apr 2023 17:11:53 +0200 Subject: [PATCH 06/23] Fix bug with unlabelled breaks --- R/guide-axis.R | 2 ++ 1 file changed, 2 insertions(+) diff --git a/R/guide-axis.R b/R/guide-axis.R index ebe3b507ab..acf0e36a0a 100644 --- a/R/guide-axis.R +++ b/R/guide-axis.R @@ -286,6 +286,7 @@ GuideAxis <- ggproto( }, build_labels = function(key, elements, params) { + key <- vec_slice(key, !is.na(key$.label %||% NA)) labels <- key$.label n_labels <- length(labels) @@ -545,3 +546,4 @@ axis_label_element_overrides <- function(axis_position, angle = NULL) { )) } } + From d28b62e5b6a8d8ccd2ce0aadfb7cf292a2060f4e Mon Sep 17 00:00:00 2001 From: Teun van den Brand <49372158+teunbrand@users.noreply.github.com> Date: Wed, 26 Apr 2023 17:13:02 +0200 Subject: [PATCH 07/23] Adjust tick spacing --- R/guide-axis.R | 17 ++++++----------- 1 file changed, 6 insertions(+), 11 deletions(-) diff --git a/R/guide-axis.R b/R/guide-axis.R index acf0e36a0a..b2d4f16f0c 100644 --- a/R/guide-axis.R +++ b/R/guide-axis.R @@ -212,15 +212,6 @@ GuideAxis <- ggproto( "horizontal" } - # TODO: delete following comment at some point: - # I found the 'position_*'/'non-position_*' and '*_dim' names confusing. - # For my own understanding, these have been renamed as follows: - # * 'aes' and 'orth_aes' for the aesthetic direction and the direction - # orthogonal to the aesthetic direction, respectively. - # * 'para_sizes' and 'orth_size(s)' for the dimension parallel to the - # aesthetic and orthogonal to the aesthetic respectively. - # I also tried to trim down the verbosity of the variable names a bit - new_params <- c("aes", "orth_aes", "para_sizes", "orth_size", "orth_sizes", "vertical", "measure_gtable", "measure_text") if (direction == "vertical") { @@ -328,8 +319,12 @@ GuideAxis <- ggproto( measure <- params$measure_text - length <- elements$ticks_length - spacer <- max(unit(0, "pt"), -1 * length) + # Ticks + range <- range(0, params$major.length, params$minor.length) + length <- elements$ticks_length * range[2] + spacer <- max(unit(0, "pt"), -1 * elements$ticks_length * diff(range)) + + # Text labels <- do.call(unit.c, lapply(grobs$label, measure)) title <- measure(grobs$title) From e4a460292538e171bc823b00a40d05337f1f76d2 Mon Sep 17 00:00:00 2001 From: Teun van den Brand <49372158+teunbrand@users.noreply.github.com> Date: Wed, 26 Apr 2023 17:13:17 +0200 Subject: [PATCH 08/23] Finishing touches --- R/guide-axis.R | 17 +++++++++++++++++ 1 file changed, 17 insertions(+) diff --git a/R/guide-axis.R b/R/guide-axis.R index b2d4f16f0c..ea371d4969 100644 --- a/R/guide-axis.R +++ b/R/guide-axis.R @@ -14,6 +14,13 @@ #' @param n.dodge The number of rows (for vertical axes) or columns (for #' horizontal axes) that should be used to render the labels. This is #' useful for displaying labels that would otherwise overlap. +#' @param major.length,minor.length A `numeric` of length 1 giving the length +#' of major and minor tick marks relative to the theme's setting. +#' @param minor.ticks A theme element inheriting from `element_line` or +#' `element_blank` for drawing minor ticks. Alternatively, a `logical` of +#' length 1 as shorthand for `element_line()` (`TRUE`) or `element_blank()` +#' (`FALSE`). `minor.ticks = element_line(...)` can be used to style the +#' minor ticks. #' @param order A positive `integer` of length 1 that specifies the order of #' this guide among multiple guides. This controls in which order guides are #' merged if there are multiple guides for the same position. If 0 (default), @@ -40,6 +47,15 @@ guide_axis <- function(title = waiver(), check.overlap = FALSE, angle = NULL, n.dodge = 1, major.length = 1, minor.length = 0.75, minor.ticks = element_blank(), order = 0, position = waiver()) { + if (is.logical(minor.ticks)) { + check_bool(minor.ticks) + minor.ticks <- if (minor.ticks) element_line() else element_blank() + } + check_inherits(minor.ticks, c("element_line", "element_blank")) + if (inherits(minor.ticks, "element_blank")) { + minor.length <- 0 + } + new_guide( title = title, @@ -542,3 +558,4 @@ axis_label_element_overrides <- function(axis_position, angle = NULL) { } } +.no_labels = function(...) NULL From b749e8097caad4d187c73a7c9d96071040c526ec Mon Sep 17 00:00:00 2001 From: Teun van den Brand <49372158+teunbrand@users.noreply.github.com> Date: Wed, 26 Apr 2023 17:13:41 +0200 Subject: [PATCH 09/23] Add test --- .../_snaps/guides/guides-with-minor-ticks.svg | 131 ++++++++++++++++++ tests/testthat/test-guides.R | 18 +++ 2 files changed, 149 insertions(+) create mode 100644 tests/testthat/_snaps/guides/guides-with-minor-ticks.svg diff --git a/tests/testthat/_snaps/guides/guides-with-minor-ticks.svg b/tests/testthat/_snaps/guides/guides-with-minor-ticks.svg new file mode 100644 index 0000000000..d276326e20 --- /dev/null +++ b/tests/testthat/_snaps/guides/guides-with-minor-ticks.svg @@ -0,0 +1,131 @@ + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + +2 +3 +4 +5 +100 +200 +300 +400 + + + + + + + + + + + + +100 +200 +300 +400 + + + + + + + + + +2 +3 +4 +5 +wt +disp +guides with minor ticks + + diff --git a/tests/testthat/test-guides.R b/tests/testthat/test-guides.R index 71314e8cfb..c3f76cb4c7 100644 --- a/tests/testthat/test-guides.R +++ b/tests/testthat/test-guides.R @@ -462,6 +462,24 @@ test_that("Axis titles won't be blown away by coord_*()", { # expect_doppelganger("guide titles with coord_sf()", plot + coord_sf()) }) +test_that("guide_axis() draws minor ticks correctly", { + p <- ggplot(mtcars, aes(wt, disp)) + + geom_point() + + theme(axis.ticks.length = unit(1, "cm"), + axis.ticks.x.bottom = element_line(linetype = 2)) + + guides( + # Test for styling and style inheritance + x = guide_axis(minor.ticks = element_line(colour = "red")), + # Test for opposed lengths + y = guide_axis(minor.ticks = TRUE, minor.length = -0.5), + # Test for flipped lenghts + x.sec = guide_axis(minor.ticks = TRUE, major.length = -0.5, minor.length = -0.5), + # Test that minor.length doesn't influence spacing when no minor ticks are drawn + y.sec = guide_axis(minor.ticks = FALSE, minor.length = 5) + ) + expect_doppelganger("guides with minor ticks", p) +}) + test_that("guides are positioned correctly", { df <- data_frame(x = 1, y = 1, z = factor("a")) From a69fedfb0245c3d7d2fbe17ea1c19c3926a6997b Mon Sep 17 00:00:00 2001 From: Teun van den Brand <49372158+teunbrand@users.noreply.github.com> Date: Wed, 26 Apr 2023 17:14:16 +0200 Subject: [PATCH 10/23] Accept tick-ordering changes in snapshots --- .../hex-bin-plot-in-polar-coordinates.svg | 16 +++++------ .../hex-bin-plot-with-sqrt-transformed-y.svg | 16 +++++------ ...e-hex-bin-with-width-and-height-of-0-1.svg | 4 +-- .../_snaps/geom-raster/1-x-3-just-0-0.svg | 20 ++++++------- .../_snaps/geom-raster/1-x-3-set-limits.svg | 20 ++++++------- tests/testthat/_snaps/geom-raster/1-x-3.svg | 20 ++++++------- .../_snaps/geom-raster/3-x-1-just-0-0.svg | 20 ++++++------- .../_snaps/geom-raster/3-x-1-set-limits.svg | 20 ++++++------- tests/testthat/_snaps/geom-raster/3-x-1.svg | 20 ++++++------- .../_snaps/geom-raster/3-x-2-just-0-0.svg | 24 ++++++++-------- .../_snaps/geom-raster/3-x-2-set-limits.svg | 24 ++++++++-------- tests/testthat/_snaps/geom-raster/3-x-2.svg | 24 ++++++++-------- .../guides/guide-bins-can-show-arrows.svg | 6 ++-- .../guides/guide-bins-can-show-limits.svg | 10 +++---- .../guides/guide-bins-can-show-ticks.svg | 12 ++++---- .../guides/guide-bins-looks-as-it-should.svg | 6 ++-- ...s-sets-labels-when-limits-is-in-breaks.svg | 10 +++---- ...derstands-coinciding-limits-and-bins-2.svg | 10 +++---- ...derstands-coinciding-limits-and-bins-3.svg | 12 ++++---- ...understands-coinciding-limits-and-bins.svg | 10 +++---- .../guides/guide-bins-work-horizontally.svg | 6 ++-- ...t-positioning-and-alignment-via-themes.svg | 16 +++++------ ...ap-of-1cm-between-guide-and-guide-text.svg | 20 ++++++------- ...e-plot-bottom-left-of-legend-at-center.svg | 20 ++++++------- .../guides/legend-inside-plot-bottom-left.svg | 20 ++++++------- .../guides/legend-inside-plot-centered.svg | 20 ++++++------- .../guides/legend-inside-plot-top-right.svg | 20 ++++++------- .../guides/multi-line-guide-title-works.svg | 20 ++++++------- ...olorbar-for-colour-and-fill-aesthetics.svg | 28 +++++++++---------- .../_snaps/guides/padding-in-legend-box.svg | 20 ++++++------- .../rotated-guide-titles-and-labels.svg | 20 ++++++------- ...p-of-1cm-between-guide-title-and-guide.svg | 20 ++++++------- ...colorbar-thick-black-ticks-green-frame.svg | 20 ++++++------- ...e-to-red-colorbar-white-ticks-no-frame.svg | 20 ++++++------- 34 files changed, 287 insertions(+), 287 deletions(-) diff --git a/tests/testthat/_snaps/geom-hex/hex-bin-plot-in-polar-coordinates.svg b/tests/testthat/_snaps/geom-hex/hex-bin-plot-in-polar-coordinates.svg index a1d64e9da4..a5d0e7ad77 100644 --- a/tests/testthat/_snaps/geom-hex/hex-bin-plot-in-polar-coordinates.svg +++ b/tests/testthat/_snaps/geom-hex/hex-bin-plot-in-polar-coordinates.svg @@ -166,14 +166,14 @@ count - - - - - - - - + + + + + + + + 2.5 5.0 7.5 diff --git a/tests/testthat/_snaps/geom-hex/hex-bin-plot-with-sqrt-transformed-y.svg b/tests/testthat/_snaps/geom-hex/hex-bin-plot-with-sqrt-transformed-y.svg index bb7623336a..3ef7c5378d 100644 --- a/tests/testthat/_snaps/geom-hex/hex-bin-plot-with-sqrt-transformed-y.svg +++ b/tests/testthat/_snaps/geom-hex/hex-bin-plot-with-sqrt-transformed-y.svg @@ -164,14 +164,14 @@ count - - - - - - - - + + + + + + + + 2.5 5.0 7.5 diff --git a/tests/testthat/_snaps/geom-hex/single-hex-bin-with-width-and-height-of-0-1.svg b/tests/testthat/_snaps/geom-hex/single-hex-bin-with-width-and-height-of-0-1.svg index 0e624b23b8..55c430bb5c 100644 --- a/tests/testthat/_snaps/geom-hex/single-hex-bin-with-width-and-height-of-0-1.svg +++ b/tests/testthat/_snaps/geom-hex/single-hex-bin-with-width-and-height-of-0-1.svg @@ -56,8 +56,8 @@ count - - + + 1 single hex bin with width and height of 0.1 diff --git a/tests/testthat/_snaps/geom-raster/1-x-3-just-0-0.svg b/tests/testthat/_snaps/geom-raster/1-x-3-just-0-0.svg index d6925d5277..3cda3a9d4a 100644 --- a/tests/testthat/_snaps/geom-raster/1-x-3-just-0-0.svg +++ b/tests/testthat/_snaps/geom-raster/1-x-3-just-0-0.svg @@ -57,16 +57,16 @@ z - - - - - - - - - - + + + + + + + + + + 1.0 1.5 2.0 diff --git a/tests/testthat/_snaps/geom-raster/1-x-3-set-limits.svg b/tests/testthat/_snaps/geom-raster/1-x-3-set-limits.svg index 0d7aa1e7e9..e2ebc6bbc9 100644 --- a/tests/testthat/_snaps/geom-raster/1-x-3-set-limits.svg +++ b/tests/testthat/_snaps/geom-raster/1-x-3-set-limits.svg @@ -59,16 +59,16 @@ z - - - - - - - - - - + + + + + + + + + + 1.0 1.5 2.0 diff --git a/tests/testthat/_snaps/geom-raster/1-x-3.svg b/tests/testthat/_snaps/geom-raster/1-x-3.svg index f5cf7f593a..40b3a41601 100644 --- a/tests/testthat/_snaps/geom-raster/1-x-3.svg +++ b/tests/testthat/_snaps/geom-raster/1-x-3.svg @@ -55,16 +55,16 @@ z - - - - - - - - - - + + + + + + + + + + 1.0 1.5 2.0 diff --git a/tests/testthat/_snaps/geom-raster/3-x-1-just-0-0.svg b/tests/testthat/_snaps/geom-raster/3-x-1-just-0-0.svg index 090bf3e379..22d38e2920 100644 --- a/tests/testthat/_snaps/geom-raster/3-x-1-just-0-0.svg +++ b/tests/testthat/_snaps/geom-raster/3-x-1-just-0-0.svg @@ -57,16 +57,16 @@ z - - - - - - - - - - + + + + + + + + + + 1.0 1.5 2.0 diff --git a/tests/testthat/_snaps/geom-raster/3-x-1-set-limits.svg b/tests/testthat/_snaps/geom-raster/3-x-1-set-limits.svg index f1493847f4..d979920d86 100644 --- a/tests/testthat/_snaps/geom-raster/3-x-1-set-limits.svg +++ b/tests/testthat/_snaps/geom-raster/3-x-1-set-limits.svg @@ -59,16 +59,16 @@ z - - - - - - - - - - + + + + + + + + + + 1.0 1.5 2.0 diff --git a/tests/testthat/_snaps/geom-raster/3-x-1.svg b/tests/testthat/_snaps/geom-raster/3-x-1.svg index 81c8824ccb..66a91af91e 100644 --- a/tests/testthat/_snaps/geom-raster/3-x-1.svg +++ b/tests/testthat/_snaps/geom-raster/3-x-1.svg @@ -55,16 +55,16 @@ z - - - - - - - - - - + + + + + + + + + + 1.0 1.5 2.0 diff --git a/tests/testthat/_snaps/geom-raster/3-x-2-just-0-0.svg b/tests/testthat/_snaps/geom-raster/3-x-2-just-0-0.svg index 9c47db029b..436e69545e 100644 --- a/tests/testthat/_snaps/geom-raster/3-x-2-just-0-0.svg +++ b/tests/testthat/_snaps/geom-raster/3-x-2-just-0-0.svg @@ -60,18 +60,18 @@ z - - - - - - - - - - - - + + + + + + + + + + + + 1 2 3 diff --git a/tests/testthat/_snaps/geom-raster/3-x-2-set-limits.svg b/tests/testthat/_snaps/geom-raster/3-x-2-set-limits.svg index 87dbec7788..3da4913fe1 100644 --- a/tests/testthat/_snaps/geom-raster/3-x-2-set-limits.svg +++ b/tests/testthat/_snaps/geom-raster/3-x-2-set-limits.svg @@ -62,18 +62,18 @@ z - - - - - - - - - - - - + + + + + + + + + + + + 1 2 3 diff --git a/tests/testthat/_snaps/geom-raster/3-x-2.svg b/tests/testthat/_snaps/geom-raster/3-x-2.svg index 3662119bd7..072a3983c2 100644 --- a/tests/testthat/_snaps/geom-raster/3-x-2.svg +++ b/tests/testthat/_snaps/geom-raster/3-x-2.svg @@ -58,18 +58,18 @@ z - - - - - - - - - - - - + + + + + + + + + + + + 1 2 3 diff --git a/tests/testthat/_snaps/guides/guide-bins-can-show-arrows.svg b/tests/testthat/_snaps/guides/guide-bins-can-show-arrows.svg index 3a553b96cc..442087e8c3 100644 --- a/tests/testthat/_snaps/guides/guide-bins-can-show-arrows.svg +++ b/tests/testthat/_snaps/guides/guide-bins-can-show-arrows.svg @@ -68,9 +68,9 @@ - - - + + + 1.5 2.0 2.5 diff --git a/tests/testthat/_snaps/guides/guide-bins-can-show-limits.svg b/tests/testthat/_snaps/guides/guide-bins-can-show-limits.svg index c398df926b..d2271a703e 100644 --- a/tests/testthat/_snaps/guides/guide-bins-can-show-limits.svg +++ b/tests/testthat/_snaps/guides/guide-bins-can-show-limits.svg @@ -66,11 +66,11 @@ - - - - - + + + + + 1 1.5 2.0 diff --git a/tests/testthat/_snaps/guides/guide-bins-can-show-ticks.svg b/tests/testthat/_snaps/guides/guide-bins-can-show-ticks.svg index 18d41ecf3e..27ba63dfb1 100644 --- a/tests/testthat/_snaps/guides/guide-bins-can-show-ticks.svg +++ b/tests/testthat/_snaps/guides/guide-bins-can-show-ticks.svg @@ -59,12 +59,12 @@ - - - - - - + + + + + + 1.5 2.0 3.0 diff --git a/tests/testthat/_snaps/guides/guide-bins-looks-as-it-should.svg b/tests/testthat/_snaps/guides/guide-bins-looks-as-it-should.svg index 39c44206df..14885226bf 100644 --- a/tests/testthat/_snaps/guides/guide-bins-looks-as-it-should.svg +++ b/tests/testthat/_snaps/guides/guide-bins-looks-as-it-should.svg @@ -66,9 +66,9 @@ - - - + + + 1.5 2.0 2.5 diff --git a/tests/testthat/_snaps/guides/guide-bins-sets-labels-when-limits-is-in-breaks.svg b/tests/testthat/_snaps/guides/guide-bins-sets-labels-when-limits-is-in-breaks.svg index b558c84e13..aadb4a0b81 100644 --- a/tests/testthat/_snaps/guides/guide-bins-sets-labels-when-limits-is-in-breaks.svg +++ b/tests/testthat/_snaps/guides/guide-bins-sets-labels-when-limits-is-in-breaks.svg @@ -297,11 +297,11 @@ - - - - - + + + + + 1 2 3 diff --git a/tests/testthat/_snaps/guides/guide-bins-understands-coinciding-limits-and-bins-2.svg b/tests/testthat/_snaps/guides/guide-bins-understands-coinciding-limits-and-bins-2.svg index 644678f65a..4139ddca47 100644 --- a/tests/testthat/_snaps/guides/guide-bins-understands-coinciding-limits-and-bins-2.svg +++ b/tests/testthat/_snaps/guides/guide-bins-understands-coinciding-limits-and-bins-2.svg @@ -297,11 +297,11 @@ - - - - - + + + + + 2000 2002 2004 diff --git a/tests/testthat/_snaps/guides/guide-bins-understands-coinciding-limits-and-bins-3.svg b/tests/testthat/_snaps/guides/guide-bins-understands-coinciding-limits-and-bins-3.svg index c0a8fc0cff..5a89f75984 100644 --- a/tests/testthat/_snaps/guides/guide-bins-understands-coinciding-limits-and-bins-3.svg +++ b/tests/testthat/_snaps/guides/guide-bins-understands-coinciding-limits-and-bins-3.svg @@ -297,12 +297,12 @@ - - - - - - + + + + + + 1999 2000 2002 diff --git a/tests/testthat/_snaps/guides/guide-bins-understands-coinciding-limits-and-bins.svg b/tests/testthat/_snaps/guides/guide-bins-understands-coinciding-limits-and-bins.svg index 837acb103a..db8bce73dd 100644 --- a/tests/testthat/_snaps/guides/guide-bins-understands-coinciding-limits-and-bins.svg +++ b/tests/testthat/_snaps/guides/guide-bins-understands-coinciding-limits-and-bins.svg @@ -297,11 +297,11 @@ - - - - - + + + + + 1999 2000 2002 diff --git a/tests/testthat/_snaps/guides/guide-bins-work-horizontally.svg b/tests/testthat/_snaps/guides/guide-bins-work-horizontally.svg index 3bddc3b3cb..587e0b20c3 100644 --- a/tests/testthat/_snaps/guides/guide-bins-work-horizontally.svg +++ b/tests/testthat/_snaps/guides/guide-bins-work-horizontally.svg @@ -66,9 +66,9 @@ - - - + + + 1.5 2.0 2.5 diff --git a/tests/testthat/_snaps/guides/guide-title-and-text-positioning-and-alignment-via-themes.svg b/tests/testthat/_snaps/guides/guide-title-and-text-positioning-and-alignment-via-themes.svg index 09233a4cf7..97e54fd3fb 100644 --- a/tests/testthat/_snaps/guides/guide-title-and-text-positioning-and-alignment-via-themes.svg +++ b/tests/testthat/_snaps/guides/guide-title-and-text-positioning-and-alignment-via-themes.svg @@ -58,14 +58,14 @@ x - - - - - - - - + + + + + + + + 25 50 75 diff --git a/tests/testthat/_snaps/guides/horizontal-gap-of-1cm-between-guide-and-guide-text.svg b/tests/testthat/_snaps/guides/horizontal-gap-of-1cm-between-guide-and-guide-text.svg index 11b0044813..c3591ec4e8 100644 --- a/tests/testthat/_snaps/guides/horizontal-gap-of-1cm-between-guide-and-guide-text.svg +++ b/tests/testthat/_snaps/guides/horizontal-gap-of-1cm-between-guide-and-guide-text.svg @@ -58,16 +58,16 @@ y - - - - - - - - - - + + + + + + + + + + 1.0 1.5 2.0 diff --git a/tests/testthat/_snaps/guides/legend-inside-plot-bottom-left-of-legend-at-center.svg b/tests/testthat/_snaps/guides/legend-inside-plot-bottom-left-of-legend-at-center.svg index 2bf9fc2fa0..86ccc60920 100644 --- a/tests/testthat/_snaps/guides/legend-inside-plot-bottom-left-of-legend-at-center.svg +++ b/tests/testthat/_snaps/guides/legend-inside-plot-bottom-left-of-legend-at-center.svg @@ -54,16 +54,16 @@ 1:3 - - - - - - - - - - + + + + + + + + + + 1.0 1.5 2.0 diff --git a/tests/testthat/_snaps/guides/legend-inside-plot-bottom-left.svg b/tests/testthat/_snaps/guides/legend-inside-plot-bottom-left.svg index 2ccc33b55a..62b74251c4 100644 --- a/tests/testthat/_snaps/guides/legend-inside-plot-bottom-left.svg +++ b/tests/testthat/_snaps/guides/legend-inside-plot-bottom-left.svg @@ -54,16 +54,16 @@ 1:3 - - - - - - - - - - + + + + + + + + + + 1.0 1.5 2.0 diff --git a/tests/testthat/_snaps/guides/legend-inside-plot-centered.svg b/tests/testthat/_snaps/guides/legend-inside-plot-centered.svg index 7b5535e219..58268d3724 100644 --- a/tests/testthat/_snaps/guides/legend-inside-plot-centered.svg +++ b/tests/testthat/_snaps/guides/legend-inside-plot-centered.svg @@ -54,16 +54,16 @@ 1:3 - - - - - - - - - - + + + + + + + + + + 1.0 1.5 2.0 diff --git a/tests/testthat/_snaps/guides/legend-inside-plot-top-right.svg b/tests/testthat/_snaps/guides/legend-inside-plot-top-right.svg index 9e40f28f1d..4411920b60 100644 --- a/tests/testthat/_snaps/guides/legend-inside-plot-top-right.svg +++ b/tests/testthat/_snaps/guides/legend-inside-plot-top-right.svg @@ -54,16 +54,16 @@ 1:3 - - - - - - - - - - + + + + + + + + + + 1.0 1.5 2.0 diff --git a/tests/testthat/_snaps/guides/multi-line-guide-title-works.svg b/tests/testthat/_snaps/guides/multi-line-guide-title-works.svg index ff7dc3c3bf..3d9c7438f5 100644 --- a/tests/testthat/_snaps/guides/multi-line-guide-title-works.svg +++ b/tests/testthat/_snaps/guides/multi-line-guide-title-works.svg @@ -60,16 +60,16 @@ continuous colorscale - - - - - - - - - - + + + + + + + + + + 1.0 1.5 2.0 diff --git a/tests/testthat/_snaps/guides/one-combined-colorbar-for-colour-and-fill-aesthetics.svg b/tests/testthat/_snaps/guides/one-combined-colorbar-for-colour-and-fill-aesthetics.svg index b8ca13811b..9d656ece9f 100644 --- a/tests/testthat/_snaps/guides/one-combined-colorbar-for-colour-and-fill-aesthetics.svg +++ b/tests/testthat/_snaps/guides/one-combined-colorbar-for-colour-and-fill-aesthetics.svg @@ -58,20 +58,20 @@ value - - - - - - - - - - - - - - + + + + + + + + + + + + + + 1 2 3 diff --git a/tests/testthat/_snaps/guides/padding-in-legend-box.svg b/tests/testthat/_snaps/guides/padding-in-legend-box.svg index d55d3978fb..e8776944d0 100644 --- a/tests/testthat/_snaps/guides/padding-in-legend-box.svg +++ b/tests/testthat/_snaps/guides/padding-in-legend-box.svg @@ -54,16 +54,16 @@ 1:3 - - - - - - - - - - + + + + + + + + + + 1.0 1.5 2.0 diff --git a/tests/testthat/_snaps/guides/rotated-guide-titles-and-labels.svg b/tests/testthat/_snaps/guides/rotated-guide-titles-and-labels.svg index 1a4de9074b..fa41704f31 100644 --- a/tests/testthat/_snaps/guides/rotated-guide-titles-and-labels.svg +++ b/tests/testthat/_snaps/guides/rotated-guide-titles-and-labels.svg @@ -69,16 +69,16 @@ value - - - - - - - - - - + + + + + + + + + + 5.0 7.5 10.0 diff --git a/tests/testthat/_snaps/guides/vertical-gap-of-1cm-between-guide-title-and-guide.svg b/tests/testthat/_snaps/guides/vertical-gap-of-1cm-between-guide-title-and-guide.svg index 9abc788f7d..a2fd873e1e 100644 --- a/tests/testthat/_snaps/guides/vertical-gap-of-1cm-between-guide-title-and-guide.svg +++ b/tests/testthat/_snaps/guides/vertical-gap-of-1cm-between-guide-title-and-guide.svg @@ -58,16 +58,16 @@ y - - - - - - - - - - + + + + + + + + + + 1.0 1.5 2.0 diff --git a/tests/testthat/_snaps/guides/white-to-red-colorbar-thick-black-ticks-green-frame.svg b/tests/testthat/_snaps/guides/white-to-red-colorbar-thick-black-ticks-green-frame.svg index 8c10cc5ff0..a10cf6f3e1 100644 --- a/tests/testthat/_snaps/guides/white-to-red-colorbar-thick-black-ticks-green-frame.svg +++ b/tests/testthat/_snaps/guides/white-to-red-colorbar-thick-black-ticks-green-frame.svg @@ -59,16 +59,16 @@ x - - - - - - - - - - + + + + + + + + + + 0.0 0.5 1.0 diff --git a/tests/testthat/_snaps/guides/white-to-red-colorbar-white-ticks-no-frame.svg b/tests/testthat/_snaps/guides/white-to-red-colorbar-white-ticks-no-frame.svg index 16b1604f52..068063c944 100644 --- a/tests/testthat/_snaps/guides/white-to-red-colorbar-white-ticks-no-frame.svg +++ b/tests/testthat/_snaps/guides/white-to-red-colorbar-white-ticks-no-frame.svg @@ -58,16 +58,16 @@ x - - - - - - - - - - + + + + + + + + + + 0.0 0.5 1.0 From 6614ce2346585e855072bdae8192b425bc4cf135 Mon Sep 17 00:00:00 2001 From: Teun van den Brand <49372158+teunbrand@users.noreply.github.com> Date: Wed, 26 Apr 2023 17:17:29 +0200 Subject: [PATCH 11/23] Add news bullet --- NEWS.md | 3 +++ 1 file changed, 3 insertions(+) diff --git a/NEWS.md b/NEWS.md index 401083cb6d..984e4b87a0 100644 --- a/NEWS.md +++ b/NEWS.md @@ -26,6 +26,9 @@ * More informative error for mismatched `direction`/`theme(legend.direction = ...)` arguments (#4364, #4930). * `guide_coloursteps()` and `guide_bins()` sort breaks (#5152). + * `guide_axis()` gains a `minor.ticks` argument to draw minor ticks. + The `major.length` and `minor.length` arguments control the relative + lengths of the ticks (#4387). * `geom_label()` now uses the `angle` aesthetic (@teunbrand, #2785) * 'lines' units in `geom_label()`, often used in the `label.padding` argument, From ec3de5867fbcaf743ed030016f16170bf31d92fe Mon Sep 17 00:00:00 2001 From: Teun van den Brand <49372158+teunbrand@users.noreply.github.com> Date: Wed, 26 Apr 2023 19:29:57 +0200 Subject: [PATCH 12/23] Fix expression labels --- R/guide-axis.R | 5 ++++- tests/testthat/test-guides.R | 1 + 2 files changed, 5 insertions(+), 1 deletion(-) diff --git a/R/guide-axis.R b/R/guide-axis.R index ea371d4969..a015e49c74 100644 --- a/R/guide-axis.R +++ b/R/guide-axis.R @@ -113,6 +113,9 @@ GuideAxis <- ggproto( extract_key = function(scale, aesthetic, minor.ticks, ...) { major <- Guide$extract_key(scale, aesthetic, ...) + if (is.expression(major$.label)) { + major$.label <- as.list(major$.label) + } if (inherits(minor.ticks, "element_blank")) { return(major) } @@ -293,7 +296,7 @@ GuideAxis <- ggproto( }, build_labels = function(key, elements, params) { - key <- vec_slice(key, !is.na(key$.label %||% NA)) + key <- vec_slice(key, !vec_detect_missing(key$.label %||% NA)) labels <- key$.label n_labels <- length(labels) diff --git a/tests/testthat/test-guides.R b/tests/testthat/test-guides.R index c3f76cb4c7..d1b4b46645 100644 --- a/tests/testthat/test-guides.R +++ b/tests/testthat/test-guides.R @@ -467,6 +467,7 @@ test_that("guide_axis() draws minor ticks correctly", { geom_point() + theme(axis.ticks.length = unit(1, "cm"), axis.ticks.x.bottom = element_line(linetype = 2)) + + scale_x_continuous(labels = math_format()) + guides( # Test for styling and style inheritance x = guide_axis(minor.ticks = element_line(colour = "red")), From 0d2f874c2fe2e7b2d879351690dac2da9fbff022 Mon Sep 17 00:00:00 2001 From: Teun van den Brand <49372158+teunbrand@users.noreply.github.com> Date: Wed, 26 Apr 2023 19:31:18 +0200 Subject: [PATCH 13/23] Document --- man/guide_axis.Rd | 12 ++++++++++++ 1 file changed, 12 insertions(+) diff --git a/man/guide_axis.Rd b/man/guide_axis.Rd index 34c358c671..0075a2c0e5 100644 --- a/man/guide_axis.Rd +++ b/man/guide_axis.Rd @@ -9,6 +9,9 @@ guide_axis( check.overlap = FALSE, angle = NULL, n.dodge = 1, + major.length = 1, + minor.length = 0.75, + minor.ticks = element_blank(), order = 0, position = waiver() ) @@ -30,6 +33,15 @@ you probably want.} horizontal axes) that should be used to render the labels. This is useful for displaying labels that would otherwise overlap.} +\item{major.length, minor.length}{A \code{numeric} of length 1 giving the length +of major and minor tick marks relative to the theme's setting.} + +\item{minor.ticks}{A theme element inheriting from \code{element_line} or +\code{element_blank} for drawing minor ticks. Alternatively, a \code{logical} of +length 1 as shorthand for \code{element_line()} (\code{TRUE}) or \code{element_blank()} +(\code{FALSE}). \code{minor.ticks = element_line(...)} can be used to style the +minor ticks.} + \item{order}{A positive \code{integer} of length 1 that specifies the order of this guide among multiple guides. This controls in which order guides are merged if there are multiple guides for the same position. If 0 (default), From 640c788b2d590d0fcc9a4073d3775ba888e4ef9d Mon Sep 17 00:00:00 2001 From: Teun van den Brand <49372158+teunbrand@users.noreply.github.com> Date: Wed, 26 Apr 2023 19:45:30 +0200 Subject: [PATCH 14/23] Update snapshot --- .../_snaps/guides/guides-with-minor-ticks.svg | 206 +++++++++--------- 1 file changed, 107 insertions(+), 99 deletions(-) diff --git a/tests/testthat/_snaps/guides/guides-with-minor-ticks.svg b/tests/testthat/_snaps/guides/guides-with-minor-ticks.svg index d276326e20..e661ca1969 100644 --- a/tests/testthat/_snaps/guides/guides-with-minor-ticks.svg +++ b/tests/testthat/_snaps/guides/guides-with-minor-ticks.svg @@ -21,109 +21,117 @@ - - + + - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + - - - - - - - - - -2 -3 -4 -5 -100 -200 -300 -400 - - - - - - - - - - - - -100 -200 -300 -400 - - - - - - - - - -2 -3 -4 -5 + + + + + + + + + +10 +2 +10 +3 +10 +4 +10 +5 +100 +200 +300 +400 + + + + + + + + + + + + +100 +200 +300 +400 + + + + + + + + + +10 +2 +10 +3 +10 +4 +10 +5 wt disp guides with minor ticks From c368ab9b5be0f1ad03d3d39c27a97845f5aaa1e4 Mon Sep 17 00:00:00 2001 From: Teun van den Brand <49372158+teunbrand@users.noreply.github.com> Date: Sun, 27 Aug 2023 21:07:22 +0200 Subject: [PATCH 15/23] minor tick theme options --- R/theme-defaults.R | 3 +++ R/theme-elements.R | 17 +++++++++++++++++ R/theme.R | 16 ++++++++++++++++ man/theme.Rd | 16 ++++++++++++++++ 4 files changed, 52 insertions(+) diff --git a/R/theme-defaults.R b/R/theme-defaults.R index fd565307bb..1bb85f7c18 100644 --- a/R/theme-defaults.R +++ b/R/theme-defaults.R @@ -147,6 +147,7 @@ theme_grey <- function(base_size = 11, base_family = "", axis.ticks.length.y = NULL, axis.ticks.length.y.left = NULL, axis.ticks.length.y.right = NULL, + axis.minor.ticks.length = unit(half_line * 0.375, "pt"), axis.title.x = element_text( margin = margin(t = half_line / 2), vjust = 1 @@ -478,6 +479,7 @@ theme_void <- function(base_size = 11, base_family = "", axis.ticks.length.y = NULL, axis.ticks.length.y.left = NULL, axis.ticks.length.y.right = NULL, + axis.minor.ticks.length = unit(0, "pt"), legend.box = NULL, legend.key.size = unit(1.2, "lines"), legend.position = "right", @@ -559,6 +561,7 @@ theme_test <- function(base_size = 11, base_family = "", axis.ticks.length.y = NULL, axis.ticks.length.y.left = NULL, axis.ticks.length.y.right = NULL, + axis.minor.ticks.length = unit(half_line * 0.375, "pt"), axis.title.x = element_text( margin = margin(t = half_line / 2), vjust = 1 diff --git a/R/theme-elements.R b/R/theme-elements.R index 044df52406..184335fd8f 100644 --- a/R/theme-elements.R +++ b/R/theme-elements.R @@ -441,12 +441,14 @@ el_def <- function(class = NULL, inherit = NULL, description = NULL) { axis.line.y = el_def("element_line", "axis.line"), axis.line.y.left = el_def("element_line", "axis.line.y"), axis.line.y.right = el_def("element_line", "axis.line.y"), + axis.text.x = el_def("element_text", "axis.text"), axis.text.x.top = el_def("element_text", "axis.text.x"), axis.text.x.bottom = el_def("element_text", "axis.text.x"), axis.text.y = el_def("element_text", "axis.text"), axis.text.y.left = el_def("element_text", "axis.text.y"), axis.text.y.right = el_def("element_text", "axis.text.y"), + axis.ticks.length = el_def("unit"), axis.ticks.length.x = el_def("unit", "axis.ticks.length"), axis.ticks.length.x.top = el_def("unit", "axis.ticks.length.x"), @@ -454,12 +456,14 @@ el_def <- function(class = NULL, inherit = NULL, description = NULL) { axis.ticks.length.y = el_def("unit", "axis.ticks.length"), axis.ticks.length.y.left = el_def("unit", "axis.ticks.length.y"), axis.ticks.length.y.right = el_def("unit", "axis.ticks.length.y"), + axis.ticks.x = el_def("element_line", "axis.ticks"), axis.ticks.x.top = el_def("element_line", "axis.ticks.x"), axis.ticks.x.bottom = el_def("element_line", "axis.ticks.x"), axis.ticks.y = el_def("element_line", "axis.ticks"), axis.ticks.y.left = el_def("element_line", "axis.ticks.y"), axis.ticks.y.right = el_def("element_line", "axis.ticks.y"), + axis.title.x = el_def("element_text", "axis.title"), axis.title.x.top = el_def("element_text", "axis.title.x"), axis.title.x.bottom = el_def("element_text", "axis.title.x"), @@ -467,6 +471,19 @@ el_def <- function(class = NULL, inherit = NULL, description = NULL) { axis.title.y.left = el_def("element_text", "axis.title.y"), axis.title.y.right = el_def("element_text", "axis.title.y"), + axis.minor.ticks.x.top = el_def("element_line", "axis.ticks.x.top"), + axis.minor.ticks.x.bottom = el_def("element_line", "axis.ticks.x.bottom"), + axis.minor.ticks.y.left = el_def("element_line", "axis.ticks.y.left"), + axis.minor.ticks.y.right = el_def("element_line", "axis.ticks.y.right"), + + axis.minor.ticks.length = el_def("unit"), + axis.minor.ticks.length.x = el_def("unit", "axis.minor.ticks.length"), + axis.minor.ticks.length.x.top = el_def("unit", "axis.minor.ticks.length.x"), + axis.minor.ticks.length.x.bottom = el_def("unit", "axis.minor.ticks.length.x"), + axis.minor.ticks.length.y = el_def("unit", "axis.minor.ticks.length"), + axis.minor.ticks.length.y.left = el_def("unit", "axis.minor.ticks.length.y"), + axis.minor.ticks.length.y.right = el_def("unit", "axis.minor.ticks.length.y"), + legend.background = el_def("element_rect", "rect"), legend.margin = el_def("margin"), legend.spacing = el_def("unit"), diff --git a/R/theme.R b/R/theme.R index 2bfdd5a835..35867a5034 100644 --- a/R/theme.R +++ b/R/theme.R @@ -48,8 +48,13 @@ #' `axis.ticks.y.left`, `axis.ticks.y.right`). `axis.ticks.*.*` inherits from #' `axis.ticks.*` which inherits from `axis.ticks`, which in turn inherits #' from `line` +#' @param axis.minor.ticks.x.top,axis.minor.ticks.x.bottom,axis.minor.ticks.y.left,axis.minor.ticks.y.right, +#' minor tick marks along axes ([element_line()]). `axis.minor.ticks.*.*` +#' inherit from the corresponding major ticks `axis.ticks.*.*`. #' @param axis.ticks.length,axis.ticks.length.x,axis.ticks.length.x.top,axis.ticks.length.x.bottom,axis.ticks.length.y,axis.ticks.length.y.left,axis.ticks.length.y.right #' length of tick marks (`unit`) +#' @param axis.minor.ticks.length,axis.minor.ticks.length.x,axis.minor.ticks.length.x.top,axis.minor.ticks.length.x.bottom,axis.minor.ticks.length.y,axis.minor.ticks.length.y.left,axis.minor.ticks.length.y.right +#' length of minor tick marks (`unit`) #' @param axis.line,axis.line.x,axis.line.x.top,axis.line.x.bottom,axis.line.y,axis.line.y.left,axis.line.y.right #' lines along axes ([element_line()]). Specify lines along all axes (`axis.line`), #' lines for each plane (using `axis.line.x` or `axis.line.y`), or individually @@ -302,6 +307,10 @@ theme <- function(line, axis.ticks.y, axis.ticks.y.left, axis.ticks.y.right, + axis.minor.ticks.x.top, + axis.minor.ticks.x.bottom, + axis.minor.ticks.y.left, + axis.minor.ticks.y.right, axis.ticks.length, axis.ticks.length.x, axis.ticks.length.x.top, @@ -309,6 +318,13 @@ theme <- function(line, axis.ticks.length.y, axis.ticks.length.y.left, axis.ticks.length.y.right, + axis.minor.ticks.length, + axis.minor.ticks.length.x, + axis.minor.ticks.length.x.top, + axis.minor.ticks.length.x.bottom, + axis.minor.ticks.length.y, + axis.minor.ticks.length.y.left, + axis.minor.ticks.length.y.right, axis.line, axis.line.x, axis.line.x.top, diff --git a/man/theme.Rd b/man/theme.Rd index e433fe7206..2b6f2fa9a6 100644 --- a/man/theme.Rd +++ b/man/theme.Rd @@ -31,6 +31,10 @@ theme( axis.ticks.y, axis.ticks.y.left, axis.ticks.y.right, + axis.minor.ticks.x.top, + axis.minor.ticks.x.bottom, + axis.minor.ticks.y.left, + axis.minor.ticks.y.right, axis.ticks.length, axis.ticks.length.x, axis.ticks.length.x.top, @@ -38,6 +42,13 @@ theme( axis.ticks.length.y, axis.ticks.length.y.left, axis.ticks.length.y.right, + axis.minor.ticks.length, + axis.minor.ticks.length.x, + axis.minor.ticks.length.x.top, + axis.minor.ticks.length.x.bottom, + axis.minor.ticks.length.y, + axis.minor.ticks.length.y.left, + axis.minor.ticks.length.y.right, axis.line, axis.line.x, axis.line.x.top, @@ -139,8 +150,13 @@ for each axis (using \code{axis.ticks.x.bottom}, \code{axis.ticks.x.top}, \verb{axis.ticks.*} which inherits from \code{axis.ticks}, which in turn inherits from \code{line}} +\item{axis.minor.ticks.x.top, axis.minor.ticks.x.bottom, axis.minor.ticks.y.left, axis.minor.ticks.y.right, }{minor tick marks along axes (\code{\link[=element_line]{element_line()}}). \verb{axis.minor.ticks.*.*} +inherit from the corresponding major ticks \verb{axis.ticks.*.*}.} + \item{axis.ticks.length, axis.ticks.length.x, axis.ticks.length.x.top, axis.ticks.length.x.bottom, axis.ticks.length.y, axis.ticks.length.y.left, axis.ticks.length.y.right}{length of tick marks (\code{unit})} +\item{axis.minor.ticks.length, axis.minor.ticks.length.x, axis.minor.ticks.length.x.top, axis.minor.ticks.length.x.bottom, axis.minor.ticks.length.y, axis.minor.ticks.length.y.left, axis.minor.ticks.length.y.right}{length of minor tick marks (\code{unit})} + \item{axis.line, axis.line.x, axis.line.x.top, axis.line.x.bottom, axis.line.y, axis.line.y.left, axis.line.y.right}{lines along axes (\code{\link[=element_line]{element_line()}}). Specify lines along all axes (\code{axis.line}), lines for each plane (using \code{axis.line.x} or \code{axis.line.y}), or individually for each axis (using \code{axis.line.x.bottom}, \code{axis.line.x.top}, From ee130e782ec67ea06c858b899b0f630cfb8f94e8 Mon Sep 17 00:00:00 2001 From: Teun van den Brand <49372158+teunbrand@users.noreply.github.com> Date: Sun, 27 Aug 2023 21:07:56 +0200 Subject: [PATCH 16/23] Revise minor to use theme --- NEWS.md | 4 +- R/guide-axis.R | 78 +++++++++++++++--------------------- man/guide_axis.Rd | 14 ++----- tests/testthat/test-guides.R | 22 ++++++---- 4 files changed, 51 insertions(+), 67 deletions(-) diff --git a/NEWS.md b/NEWS.md index b961956f1e..13b5a6744c 100644 --- a/NEWS.md +++ b/NEWS.md @@ -78,9 +78,7 @@ * More informative error for mismatched `direction`/`theme(legend.direction = ...)` arguments (#4364, #4930). * `guide_coloursteps()` and `guide_bins()` sort breaks (#5152). - * `guide_axis()` gains a `minor.ticks` argument to draw minor ticks. - The `major.length` and `minor.length` arguments control the relative - lengths of the ticks (#4387). + * `guide_axis()` gains a `minor.ticks` argument to draw minor ticks (#4387). * `guide_axis()` gains a `cap` argument that can be used to trim the axis line to extreme breaks (#4907). * `guide_colourbar()` and `guide_coloursteps()` merge properly when one diff --git a/R/guide-axis.R b/R/guide-axis.R index ecd5bf43bd..4d2342707d 100644 --- a/R/guide-axis.R +++ b/R/guide-axis.R @@ -14,13 +14,8 @@ #' @param n.dodge The number of rows (for vertical axes) or columns (for #' horizontal axes) that should be used to render the labels. This is #' useful for displaying labels that would otherwise overlap. -#' @param major.length,minor.length A `numeric` of length 1 giving the length -#' of major and minor tick marks relative to the theme's setting. -#' @param minor.ticks A theme element inheriting from `element_line` or -#' `element_blank` for drawing minor ticks. Alternatively, a `logical` of -#' length 1 as shorthand for `element_line()` (`TRUE`) or `element_blank()` -#' (`FALSE`). `minor.ticks = element_line(...)` can be used to style the -#' minor ticks. +#' @param minor.ticks Whether to draw the minor ticks (`TRUE`) or not draw +#' minor ticks (`FALSE`, default). #' @param cap A `character` to cut the axis line back to the last breaks. Can #' be `"none"` (default) to draw the axis line along the whole panel, or #' `"upper"` and `"lower"` to draw the axis to the upper or lower break, or @@ -49,17 +44,9 @@ #' # can also be used to add a duplicate guide #' p + guides(x = guide_axis(n.dodge = 2), y.sec = guide_axis()) guide_axis <- function(title = waiver(), check.overlap = FALSE, angle = NULL, - n.dodge = 1, major.length = 1, minor.length = 0.75, - minor.ticks = element_blank(), cap = "none", + n.dodge = 1, minor.ticks = FALSE, cap = "none", order = 0, position = waiver()) { - if (is.logical(minor.ticks)) { - check_bool(minor.ticks) - minor.ticks <- if (minor.ticks) element_line() else element_blank() - } - check_inherits(minor.ticks, c("element_line", "element_blank")) - if (inherits(minor.ticks, "element_blank")) { - minor.length <- 0 - } + check_bool(minor.ticks) if (is.logical(cap)) { check_bool(cap) cap <- if (cap) "both" else "none" @@ -73,9 +60,7 @@ guide_axis <- function(title = waiver(), check.overlap = FALSE, angle = NULL, check.overlap = check.overlap, angle = angle, n.dodge = n.dodge, - major.length = major.length, - minor.length = minor.length, - minor.ticks = minor.ticks, + minor.ticks = minor.ticks, cap = cap, # parameter @@ -104,9 +89,7 @@ GuideAxis <- ggproto( direction = NULL, angle = NULL, n.dodge = 1, - major.length = 1, - minor.length = 0.75, - minor.ticks = NULL, + minor.ticks = FALSE, cap = "none", order = 0, check.overlap = FALSE @@ -120,12 +103,14 @@ GuideAxis <- ggproto( line = "axis.line", text = "axis.text", ticks = "axis.ticks", - ticks_length = "axis.ticks.length" + minor = "axis.minor.ticks", + major_length = "axis.ticks.length", + minor_length = "axis.minor.ticks.length" ), extract_key = function(scale, aesthetic, minor.ticks, ...) { major <- Guide$extract_key(scale, aesthetic, ...) - if (inherits(minor.ticks, "element_blank")) { + if (!minor.ticks) { return(major) } if (!is.null(major)) { @@ -225,7 +210,7 @@ GuideAxis <- ggproto( }, setup_elements = function(params, elements, theme) { - axis_elem <- c("line", "text", "ticks", "ticks_length") + axis_elem <- c("line", "text", "ticks", "minor", "major_length", "minor_length") is_char <- vapply(elements[axis_elem], is.character, logical(1)) axis_elem <- axis_elem[is_char] elements[axis_elem] <- lapply( @@ -235,7 +220,6 @@ GuideAxis <- ggproto( ), calc_element, theme = theme ) - elements$minor_ticks <- combine_elements(params$minor.ticks, elements$ticks) elements }, @@ -308,26 +292,22 @@ GuideAxis <- ggproto( }, build_ticks = function(key, elements, params, position = params$opposite) { - if (!".type" %in% names(key)) { - ticks <- Guide$build_ticks( - key, elements, params, position, - elements$ticks_length * params$major.length - ) - return(ticks) - } - major <- vec_slice(key, key$.type == "major") + major <- Guide$build_ticks( - major, elements, params, position, - elements$ticks_length * params$major.length + vec_slice(key, (key$.type %||% "major") == "major"), + elements, params, position, + elements$major_length ) - if (inherits(elements$minor_ticks, "element_blank")) { + + if (!params$minor.ticks || inherits(elements$minor, "element_blank")) { return(major) } - elements$ticks <- elements$minor_ticks - minor <- vec_slice(key, key$.type == "minor") + + elements$ticks <- elements$minor minor <- Guide$build_ticks( - minor, elements, params, position, - elements$ticks_length * params$minor.length + vec_slice(key, (key$.type %||% "major") == "minor"), + elements, params, position, + elements$minor_length ) grobTree(major, minor, name = "ticks") }, @@ -372,9 +352,17 @@ GuideAxis <- ggproto( measure <- params$measure_text # Ticks - range <- range(0, params$major.length, params$minor.length) - length <- elements$ticks_length * range[2] - spacer <- max(unit(0, "pt"), -1 * elements$ticks_length * diff(range)) + range <- range( + 0, convertUnit(elements$major_length, "cm", valueOnly = TRUE) + ) + if (params$minor.ticks && !inherits(elements$minor, "element_blank")) { + range <- range( + range, convertUnit(elements$minor_length, "cm", valueOnly = TRUE) + ) + } + + length <- unit(range[2], "cm") + spacer <- max(unit(0, "pt"), unit(-1 * diff(range), "cm")) # Text labels <- do.call(unit.c, lapply(grobs$label, measure)) diff --git a/man/guide_axis.Rd b/man/guide_axis.Rd index 45a437b08e..d2efadff8e 100644 --- a/man/guide_axis.Rd +++ b/man/guide_axis.Rd @@ -9,9 +9,7 @@ guide_axis( check.overlap = FALSE, angle = NULL, n.dodge = 1, - major.length = 1, - minor.length = 0.75, - minor.ticks = element_blank(), + minor.ticks = FALSE, cap = "none", order = 0, position = waiver() @@ -34,14 +32,8 @@ you probably want.} horizontal axes) that should be used to render the labels. This is useful for displaying labels that would otherwise overlap.} -\item{major.length, minor.length}{A \code{numeric} of length 1 giving the length -of major and minor tick marks relative to the theme's setting.} - -\item{minor.ticks}{A theme element inheriting from \code{element_line} or -\code{element_blank} for drawing minor ticks. Alternatively, a \code{logical} of -length 1 as shorthand for \code{element_line()} (\code{TRUE}) or \code{element_blank()} -(\code{FALSE}). \code{minor.ticks = element_line(...)} can be used to style the -minor ticks.} +\item{minor.ticks}{Whether to draw the minor ticks (\code{TRUE}) or not draw +minor ticks (\code{FALSE}, default).} \item{cap}{A \code{character} to cut the axis line back to the last breaks. Can be \code{"none"} (default) to draw the axis line along the whole panel, or diff --git a/tests/testthat/test-guides.R b/tests/testthat/test-guides.R index 8e950d3214..5447e45328 100644 --- a/tests/testthat/test-guides.R +++ b/tests/testthat/test-guides.R @@ -494,17 +494,23 @@ test_that("guide_axis() draws minor ticks correctly", { p <- ggplot(mtcars, aes(wt, disp)) + geom_point() + theme(axis.ticks.length = unit(1, "cm"), - axis.ticks.x.bottom = element_line(linetype = 2)) + + axis.ticks.x.bottom = element_line(linetype = 2), + axis.ticks.length.x.top = unit(-0.5, "cm"), + axis.minor.ticks.x.bottom = element_line(colour = "red"), + axis.minor.ticks.length.y.left = unit(-0.5, "cm"), + axis.minor.ticks.length.x.top = unit(-0.5, "cm"), + axis.minor.ticks.length.x.bottom = unit(0.75, "cm"), + axis.minor.ticks.length.y.right = unit(5, "cm")) + scale_x_continuous(labels = math_format()) + guides( # Test for styling and style inheritance - x = guide_axis(minor.ticks = element_line(colour = "red")), - # Test for opposed lengths - y = guide_axis(minor.ticks = TRUE, minor.length = -0.5), - # Test for flipped lenghts - x.sec = guide_axis(minor.ticks = TRUE, major.length = -0.5, minor.length = -0.5), - # Test that minor.length doesn't influence spacing when no minor ticks are drawn - y.sec = guide_axis(minor.ticks = FALSE, minor.length = 5) + x = guide_axis(minor.ticks = TRUE), + # # Test for opposed lengths + y = guide_axis(minor.ticks = TRUE), + # # Test for flipped lenghts + x.sec = guide_axis(minor.ticks = TRUE), + # # Test that minor.length doesn't influence spacing when no minor ticks are drawn + y.sec = guide_axis(minor.ticks = FALSE) ) expect_doppelganger("guides with minor ticks", p) }) From 144183a3276dc9aa550b9fc2d9a7fd82fb6bb7ac Mon Sep 17 00:00:00 2001 From: Teun van den Brand <49372158+teunbrand@users.noreply.github.com> Date: Tue, 12 Sep 2023 14:12:22 +0200 Subject: [PATCH 17/23] Use `rel()` for minor ticks --- R/theme-defaults.R | 4 ++-- R/theme.R | 2 +- man/theme.Rd | 2 +- 3 files changed, 4 insertions(+), 4 deletions(-) diff --git a/R/theme-defaults.R b/R/theme-defaults.R index 1bb85f7c18..da315e2e25 100644 --- a/R/theme-defaults.R +++ b/R/theme-defaults.R @@ -147,7 +147,7 @@ theme_grey <- function(base_size = 11, base_family = "", axis.ticks.length.y = NULL, axis.ticks.length.y.left = NULL, axis.ticks.length.y.right = NULL, - axis.minor.ticks.length = unit(half_line * 0.375, "pt"), + axis.minor.ticks.length = rel(0.75), axis.title.x = element_text( margin = margin(t = half_line / 2), vjust = 1 @@ -561,7 +561,7 @@ theme_test <- function(base_size = 11, base_family = "", axis.ticks.length.y = NULL, axis.ticks.length.y.left = NULL, axis.ticks.length.y.right = NULL, - axis.minor.ticks.length = unit(half_line * 0.375, "pt"), + axis.minor.ticks.length = rel(0.75), axis.title.x = element_text( margin = margin(t = half_line / 2), vjust = 1 diff --git a/R/theme.R b/R/theme.R index 49d78fabf8..fd4a445e32 100644 --- a/R/theme.R +++ b/R/theme.R @@ -54,7 +54,7 @@ #' @param axis.ticks.length,axis.ticks.length.x,axis.ticks.length.x.top,axis.ticks.length.x.bottom,axis.ticks.length.y,axis.ticks.length.y.left,axis.ticks.length.y.right #' length of tick marks (`unit`) #' @param axis.minor.ticks.length,axis.minor.ticks.length.x,axis.minor.ticks.length.x.top,axis.minor.ticks.length.x.bottom,axis.minor.ticks.length.y,axis.minor.ticks.length.y.left,axis.minor.ticks.length.y.right -#' length of minor tick marks (`unit`) +#' length of minor tick marks (`unit`), or relative to `axis.ticks.length` when provided with `rel()`. #' @param axis.line,axis.line.x,axis.line.x.top,axis.line.x.bottom,axis.line.y,axis.line.y.left,axis.line.y.right #' lines along axes ([element_line()]). Specify lines along all axes (`axis.line`), #' lines for each plane (using `axis.line.x` or `axis.line.y`), or individually diff --git a/man/theme.Rd b/man/theme.Rd index 2b6f2fa9a6..7672d42c5a 100644 --- a/man/theme.Rd +++ b/man/theme.Rd @@ -155,7 +155,7 @@ inherit from the corresponding major ticks \verb{axis.ticks.*.*}.} \item{axis.ticks.length, axis.ticks.length.x, axis.ticks.length.x.top, axis.ticks.length.x.bottom, axis.ticks.length.y, axis.ticks.length.y.left, axis.ticks.length.y.right}{length of tick marks (\code{unit})} -\item{axis.minor.ticks.length, axis.minor.ticks.length.x, axis.minor.ticks.length.x.top, axis.minor.ticks.length.x.bottom, axis.minor.ticks.length.y, axis.minor.ticks.length.y.left, axis.minor.ticks.length.y.right}{length of minor tick marks (\code{unit})} +\item{axis.minor.ticks.length, axis.minor.ticks.length.x, axis.minor.ticks.length.x.top, axis.minor.ticks.length.x.bottom, axis.minor.ticks.length.y, axis.minor.ticks.length.y.left, axis.minor.ticks.length.y.right}{length of minor tick marks (\code{unit}), or relative to \code{axis.ticks.length} when provided with \code{rel()}.} \item{axis.line, axis.line.x, axis.line.x.top, axis.line.x.bottom, axis.line.y, axis.line.y.left, axis.line.y.right}{lines along axes (\code{\link[=element_line]{element_line()}}). Specify lines along all axes (\code{axis.line}), lines for each plane (using \code{axis.line.x} or \code{axis.line.y}), or individually From 10354e1bcb4479f49eca5e7b4747b4b1b4576c9a Mon Sep 17 00:00:00 2001 From: Teun van den Brand <49372158+teunbrand@users.noreply.github.com> Date: Tue, 12 Sep 2023 14:44:03 +0200 Subject: [PATCH 18/23] Biparental inheritance for minor tick length leaf nodes --- R/theme-elements.R | 18 +++++++++++++----- tests/testthat/test-theme.R | 17 +++++++++++++++++ 2 files changed, 30 insertions(+), 5 deletions(-) diff --git a/R/theme-elements.R b/R/theme-elements.R index d1d8c38cb6..4dda819879 100644 --- a/R/theme-elements.R +++ b/R/theme-elements.R @@ -476,13 +476,21 @@ el_def <- function(class = NULL, inherit = NULL, description = NULL) { axis.minor.ticks.y.left = el_def("element_line", "axis.ticks.y.left"), axis.minor.ticks.y.right = el_def("element_line", "axis.ticks.y.right"), - axis.minor.ticks.length = el_def(c("unit", "rel"), "axis.ticks.length"), + axis.minor.ticks.length = el_def(c("unit", "rel")), axis.minor.ticks.length.x = el_def(c("unit", "rel"), "axis.minor.ticks.length"), - axis.minor.ticks.length.x.top = el_def(c("unit", "rel"), "axis.minor.ticks.length.x"), - axis.minor.ticks.length.x.bottom = el_def(c("unit", "rel"), "axis.minor.ticks.length.x"), + axis.minor.ticks.length.x.top = el_def( + c("unit", "rel"), c("axis.minor.ticks.length.x", "axis.ticks.length.x.top") + ), + axis.minor.ticks.length.x.bottom = el_def( + c("unit", "rel"), c("axis.minor.ticks.length.x", "axis.ticks.length.x.bottom") + ), axis.minor.ticks.length.y = el_def(c("unit", "rel"), "axis.minor.ticks.length"), - axis.minor.ticks.length.y.left = el_def(c("unit", "rel"), "axis.minor.ticks.length.y"), - axis.minor.ticks.length.y.right = el_def(c("unit", "rel"), "axis.minor.ticks.length.y"), + axis.minor.ticks.length.y.left = el_def( + c("unit", "rel"), c("axis.minor.ticks.length.y", "axis.ticks.length.y.left") + ), + axis.minor.ticks.length.y.right = el_def( + c("unit", "rel"), c("axis.minor.ticks.length.y", "axis.ticks.length.y.right") + ), legend.background = el_def("element_rect", "rect"), legend.margin = el_def("margin"), diff --git a/tests/testthat/test-theme.R b/tests/testthat/test-theme.R index 7bf37c90e1..e6e6cfdb55 100644 --- a/tests/testthat/test-theme.R +++ b/tests/testthat/test-theme.R @@ -509,6 +509,23 @@ test_that("Theme validation behaves as expected", { expect_snapshot_error(validate_element("A", "aspect.ratio", tree)) }) +test_that("Minor tick length supports biparental inheritance", { + my_theme <- theme_gray() + theme( + axis.ticks.length = unit(1, "cm"), + axis.ticks.length.y.left = unit(1, "pt"), + axis.minor.ticks.length.y = unit(1, "inch"), + axis.minor.ticks.length = rel(0.5) + ) + expect_equal( # Inherits rel(0.5) from minor, 1cm from major + calc_element("axis.minor.ticks.length.x.bottom", my_theme), + unit(1, "cm") * 0.5 + ) + expect_equal( # Inherits 1inch directly from minor + calc_element("axis.minor.ticks.length.y.left", my_theme), + unit(1, "inch") + ) +}) + # Visual tests ------------------------------------------------------------ test_that("aspect ratio is honored", { From 2eae4095ce96c88bb923d213bfd6bf0d8a8a7f18 Mon Sep 17 00:00:00 2001 From: Teun van den Brand <49372158+teunbrand@users.noreply.github.com> Date: Wed, 4 Oct 2023 10:08:32 +0200 Subject: [PATCH 19/23] change minor break extraction --- R/guide-axis.R | 23 +++++++++++++++++------ 1 file changed, 17 insertions(+), 6 deletions(-) diff --git a/R/guide-axis.R b/R/guide-axis.R index 4d2342707d..170974b4bb 100644 --- a/R/guide-axis.R +++ b/R/guide-axis.R @@ -113,14 +113,25 @@ GuideAxis <- ggproto( if (!minor.ticks) { return(major) } - if (!is.null(major)) { - major$.type <- "major" + + minor_breaks <- scale$get_breaks_minor() + minor_breaks <- setdiff(minor_breaks, major$.value) + minor_breaks <- minor_breaks[is.finite(minor_breaks)] + + if (length(minor_breaks) < 1) { + return(major) } - minor <- setdiff(scale$get_breaks_minor(), major$.value) - new_scale <- ggproto(NULL, scale, breaks = minor, get_labels = .no_labels) - minor <- Guide$extract_key(new_scale, aesthetic, ...) + + minor <- data_frame0(!!aesthetic := scale$map(minor_breaks)) + minor$.value <- minor_breaks minor$.type <- "minor" - vec_rbind(major, minor) + + if (nrow(major) > 0) { + major$.type <- "major" + vec_rbind(major, minor) + } else { + minor + } }, extract_params = function(scale, params, ...) { From 0f15a419b92228c500902cf12428bdaa3ec3f59c Mon Sep 17 00:00:00 2001 From: Teun van den Brand <49372158+teunbrand@users.noreply.github.com> Date: Wed, 4 Oct 2023 10:23:45 +0200 Subject: [PATCH 20/23] Skip tick calculation with blank elements --- R/guide-.R | 8 +++++++- 1 file changed, 7 insertions(+), 1 deletion(-) diff --git a/R/guide-.R b/R/guide-.R index 97caa1e560..a3f449b9ed 100644 --- a/R/guide-.R +++ b/R/guide-.R @@ -353,6 +353,12 @@ Guide <- ggproto( # Renders tickmarks build_ticks = function(key, elements, params, position = params$position, length = elements$ticks_length) { + if (!inherits(elements, "element")) { + elements <- elements$ticks + } + if (!inherits(elements, "element_line")) { + return(zeroGrob()) + } if (!is.list(key)) { breaks <- key @@ -380,7 +386,7 @@ Guide <- ggproto( # Build grob flip_element_grob( - elements$ticks, + elements, x = tick, y = mark, id.lengths = rep(2, n_breaks), flip = position %in% c("top", "bottom") From d4240bbedf6fdd2e8414a0ded7a633ab29232c02 Mon Sep 17 00:00:00 2001 From: Teun van den Brand <49372158+teunbrand@users.noreply.github.com> Date: Wed, 4 Oct 2023 10:24:06 +0200 Subject: [PATCH 21/23] clean up axis ticks building --- R/guide-axis.R | 7 +++---- 1 file changed, 3 insertions(+), 4 deletions(-) diff --git a/R/guide-axis.R b/R/guide-axis.R index 170974b4bb..e3ea6df32e 100644 --- a/R/guide-axis.R +++ b/R/guide-axis.R @@ -306,18 +306,17 @@ GuideAxis <- ggproto( major <- Guide$build_ticks( vec_slice(key, (key$.type %||% "major") == "major"), - elements, params, position, + elements$ticks, params, position, elements$major_length ) - if (!params$minor.ticks || inherits(elements$minor, "element_blank")) { + if (!params$minor.ticks) { return(major) } - elements$ticks <- elements$minor minor <- Guide$build_ticks( vec_slice(key, (key$.type %||% "major") == "minor"), - elements, params, position, + elements$minor, params, position, elements$minor_length ) grobTree(major, minor, name = "ticks") From d302663077eec4250901dae8d7a79f233fe2f4cd Mon Sep 17 00:00:00 2001 From: Teun van den Brand <49372158+teunbrand@users.noreply.github.com> Date: Wed, 4 Oct 2023 10:32:35 +0200 Subject: [PATCH 22/23] change measurement function to width_cm/height_cm --- R/guide-axis.R | 18 ++++++++---------- 1 file changed, 8 insertions(+), 10 deletions(-) diff --git a/R/guide-axis.R b/R/guide-axis.R index e3ea6df32e..d55fa17073 100644 --- a/R/guide-axis.R +++ b/R/guide-axis.R @@ -266,12 +266,12 @@ GuideAxis <- ggproto( if (direction == "vertical") { params[new_params] <- list( "y", "x", "heights", "width", "widths", - TRUE, gtable_width, grobWidth + TRUE, gtable_width, width_cm ) } else { params[new_params] <- list( "x", "y", "widths", "height", "heights", - FALSE, gtable_height, grobHeight + FALSE, gtable_height, height_cm ) } @@ -362,21 +362,19 @@ GuideAxis <- ggproto( measure <- params$measure_text # Ticks - range <- range( - 0, convertUnit(elements$major_length, "cm", valueOnly = TRUE) - ) + major_cm <- convertUnit(elements$major_length, "cm", valueOnly = TRUE) + range <- range(0, major_cm) if (params$minor.ticks && !inherits(elements$minor, "element_blank")) { - range <- range( - range, convertUnit(elements$minor_length, "cm", valueOnly = TRUE) - ) + minor_cm <- convertUnit(elements$minor_length, "cm", valueOnly = TRUE) + range <- range(range, minor_cm) } length <- unit(range[2], "cm") spacer <- max(unit(0, "pt"), unit(-1 * diff(range), "cm")) # Text - labels <- do.call(unit.c, lapply(grobs$label, measure)) - title <- measure(grobs$title) + labels <- unit(measure(grobs$label), "cm") + title <- unit(measure(grobs$title), "cm") sizes <- unit.c(length, spacer, labels, title) if (params$lab_first) { From f7e8afc99b48673016b2d37fcd35ef8dcd4cec6e Mon Sep 17 00:00:00 2001 From: Teun van den Brand <49372158+teunbrand@users.noreply.github.com> Date: Wed, 4 Oct 2023 10:44:58 +0200 Subject: [PATCH 23/23] Remove redundant function --- R/guide-axis.R | 2 -- 1 file changed, 2 deletions(-) diff --git a/R/guide-axis.R b/R/guide-axis.R index d55fa17073..ad544716c0 100644 --- a/R/guide-axis.R +++ b/R/guide-axis.R @@ -597,5 +597,3 @@ axis_label_element_overrides <- function(axis_position, angle = NULL) { )) } } - -.no_labels = function(...) NULL