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()) } 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 ca795411b4..c8ee9b2bb8 100644 --- a/tests/testthat/test-guides.R +++ b/tests/testthat/test-guides.R @@ -331,6 +331,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") + ) +}) + test_that("guide_axis_logticks calculates appropriate ticks", { test_scale <- function(trans = identity_trans(), limits = c(NA, NA)) {