diff --git a/R/guide-.R b/R/guide-.R index 854cdbd52f..0a334c4580 100644 --- a/R/guide-.R +++ b/R/guide-.R @@ -329,7 +329,7 @@ Guide <- ggproto( # Arrange and assemble grobs sizes <- self$measure_grobs(grobs, params, elems) - layout <- self$arrange_layout(key, sizes, params) + layout <- self$arrange_layout(key, sizes, params, elems) self$assemble_drawing(grobs, layout, sizes, params, elems) }, @@ -340,7 +340,7 @@ Guide <- ggproto( }, # Takes care of where grobs should be added to the output gtable. - arrange_layout = function(key, sizes, params) { + arrange_layout = function(key, sizes, params, elements) { return(invisible()) }, diff --git a/R/guide-axis-theta.R b/R/guide-axis-theta.R index 207678be8f..49b128f0e4 100644 --- a/R/guide-axis-theta.R +++ b/R/guide-axis-theta.R @@ -306,7 +306,7 @@ GuideAxisTheta <- ggproto( list(offset = max(height)) }, - arrange_layout = function(key, sizes, params) { + arrange_layout = function(key, sizes, params, elements) { NULL }, diff --git a/R/guide-axis.R b/R/guide-axis.R index ac59ef41b4..85a580bdfa 100644 --- a/R/guide-axis.R +++ b/R/guide-axis.R @@ -386,7 +386,7 @@ GuideAxis <- ggproto( sizes }, - arrange_layout = function(key, sizes, params) { + arrange_layout = function(key, sizes, params, elements) { layout <- seq_along(sizes) diff --git a/R/guide-legend.R b/R/guide-legend.R index 760d006e9b..885e24b3ce 100644 --- a/R/guide-legend.R +++ b/R/guide-legend.R @@ -471,21 +471,16 @@ GuideLegend <- ggproto( ) heights <- head(vec_interleave(!!!heights), -1) - list( - widths = widths, - heights = heights, - padding = elements$padding, - label_position = elements$text_position - ) + list(widths = widths, heights = heights) }, - arrange_layout = function(key, sizes, params) { + arrange_layout = function(key, sizes, params, elements) { break_seq <- seq_len(params$n_breaks %||% 1L) dim <- c(params$nrow %||% 1L, params$ncol %||% 1L) # Find rows / columns of legend items - if (params$byrow %||% FALSE) { + if (elements$byrow %||% FALSE) { row <- ceiling(break_seq / dim[2L]) col <- (break_seq - 1L) %% dim[2L] + 1L } else { @@ -497,7 +492,7 @@ GuideLegend <- ggproto( key_col <- col * 2 - 1 # Make gaps for key-label spacing depending on label position - position <- sizes$label_position + position <- elements$text_position key_row <- key_row + switch(position, top = row, bottom = row - 1, 0) lab_row <- key_row + switch(position, top = -1, bottom = 1, 0) key_col <- key_col + switch(position, left = col, right = col - 1, 0) diff --git a/tests/testthat/_snaps/guides/legend-byrow-true.svg b/tests/testthat/_snaps/guides/legend-byrow-true.svg new file mode 100644 index 0000000000..804a4a1dbe --- /dev/null +++ b/tests/testthat/_snaps/guides/legend-byrow-true.svg @@ -0,0 +1,75 @@ + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + +2 +4 +6 + + + + + + +2 +4 +6 +x +x + +f + + + + + + + + + + + + +A +B +C +D +E +F +legend.byrow = TRUE + + diff --git a/tests/testthat/test-guides.R b/tests/testthat/test-guides.R index df14fa11f1..2cce2521c1 100644 --- a/tests/testthat/test-guides.R +++ b/tests/testthat/test-guides.R @@ -1161,6 +1161,23 @@ test_that("guides() warns if unnamed guides are provided", { expect_null(guides()) }) +test_that("legend.byrow works in `guide_legend()`", { + + df <- data.frame(x = 1:6, f = LETTERS[1:6]) + + p <- ggplot(df, aes(x, x, colour = f)) + + geom_point() + + scale_colour_discrete( + guide = guide_legend( + ncol = 3, + theme = theme(legend.byrow = TRUE) + ) + ) + + expect_doppelganger("legend.byrow = TRUE", p) + +}) + test_that("old S3 guides can be implemented", { my_env <- env()