From 4f2f8fa6bac9a2a9bedd8810a5e8becd154a3c56 Mon Sep 17 00:00:00 2001 From: Teun van den Brand <49372158+teunbrand@users.noreply.github.com> Date: Fri, 3 Nov 2023 14:11:11 +0100 Subject: [PATCH 1/2] use correct direction --- R/guides-.R | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/R/guides-.R b/R/guides-.R index 76bac43de0..19348ec157 100644 --- a/R/guides-.R +++ b/R/guides-.R @@ -491,7 +491,7 @@ Guides <- ggproto( horizontal = c("center", "top") ) - grobs <- self$draw(theme, position, default_direction) + grobs <- self$draw(theme, position, theme$legend.direction) if (length(grobs) < 1) { return(zeroGrob()) } From 98d7827676b9b08937e512e6549dbae51fc0bf37 Mon Sep 17 00:00:00 2001 From: Teun van den Brand <49372158+teunbrand@users.noreply.github.com> Date: Fri, 3 Nov 2023 14:14:16 +0100 Subject: [PATCH 2/2] Add test --- .../guides/horizontal-legend-direction.svg | 115 ++++++++++++++++++ .../guides/vertical-legend-direction.svg | 115 ++++++++++++++++++ tests/testthat/test-guides.R | 17 +++ 3 files changed, 247 insertions(+) create mode 100644 tests/testthat/_snaps/guides/horizontal-legend-direction.svg create mode 100644 tests/testthat/_snaps/guides/vertical-legend-direction.svg diff --git a/tests/testthat/_snaps/guides/horizontal-legend-direction.svg b/tests/testthat/_snaps/guides/horizontal-legend-direction.svg new file mode 100644 index 0000000000..ed4b386f0b --- /dev/null +++ b/tests/testthat/_snaps/guides/horizontal-legend-direction.svg @@ -0,0 +1,115 @@ + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + +10 +15 +20 +25 +30 +35 + + + + + + + + + + +100 +200 +300 +400 +disp +mpg + +drat + + + + + + + + + +3.0 +3.5 +4.0 +4.5 + +factor(cyl) + + + + + + +4 +6 +8 +horizontal legend direction + + diff --git a/tests/testthat/_snaps/guides/vertical-legend-direction.svg b/tests/testthat/_snaps/guides/vertical-legend-direction.svg new file mode 100644 index 0000000000..1f44272cc8 --- /dev/null +++ b/tests/testthat/_snaps/guides/vertical-legend-direction.svg @@ -0,0 +1,115 @@ + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + +10 +15 +20 +25 +30 +35 + + + + + + + + + + +100 +200 +300 +400 +disp +mpg + +drat + + + + + + + + + +3.0 +3.5 +4.0 +4.5 + +factor(cyl) + + + + + + +4 +6 +8 +vertical legend direction + + diff --git a/tests/testthat/test-guides.R b/tests/testthat/test-guides.R index b00fe359c6..491f134c03 100644 --- a/tests/testthat/test-guides.R +++ b/tests/testthat/test-guides.R @@ -338,6 +338,23 @@ test_that("guide_colourbar warns about discrete scales", { }) +test_that("legend directions are set correctly", { + + p <- ggplot(mtcars, aes(disp, mpg, shape = factor(cyl), colour = drat)) + + geom_point() + + theme_test() + + expect_doppelganger( + "vertical legend direction", + p + theme(legend.direction = "vertical") + ) + + expect_doppelganger( + "horizontal legend direction", + p + theme(legend.direction = "horizontal") + ) +}) + # Visual tests ------------------------------------------------------------ test_that("axis guides are drawn correctly", {