diff --git a/NEWS.md b/NEWS.md index 456c2c3054..dc7f1aa589 100644 --- a/NEWS.md +++ b/NEWS.md @@ -9,7 +9,7 @@ * Closed #2218: `highlight(selectize = TRUE)` no longer yields an incorrect selectize.js result when there is a combination of crosstalk and non-crosstalk traces. (#2217) * Closed #2208: `ggplotly()` no longer errors given a `geom_area()` with 1 or less data points (error introduced by new behavior in ggplot2 v3.4.0). (#2209) * Closed #2220: `ggplotly()` no longer errors on `stat_summary(geom = "crossbar")`. (#2222) - +* Closed #2212: `ggplotly()` no longer removes legends when setting guide properties via `guides(aes = guide_xxx(...))`. # 4.10.1 diff --git a/R/layers2traces.R b/R/layers2traces.R index 01fa7063a2..5b110cd56e 100644 --- a/R/layers2traces.R +++ b/R/layers2traces.R @@ -102,7 +102,9 @@ layers2traces <- function(data, prestats_data, layout, p) { } # now to the actual layer -> trace conversion trace.list <- list() - aes_no_guide <- names(vapply(p$guides, identical, logical(1), "none")) + + aes_no_guide <- names(p$guides)[vapply(p$guides, identical, logical(1), "none")] + for (i in seq_along(datz)) { d <- datz[[i]] # variables that produce multiple traces and deserve their own legend entries diff --git a/tests/testthat/test-ggplot-legend.R b/tests/testthat/test-ggplot-legend.R index 11d0fd9886..d4dfe4eed2 100644 --- a/tests/testthat/test-ggplot-legend.R +++ b/tests/testthat/test-ggplot-legend.R @@ -58,6 +58,40 @@ test_that("aesthetics can be discarded from legend with guide(aes = 'none')", { expect_doppelganger(p, "guide-aes-none") }) +test_that("legend can be manipulated via guides(aes = guide_xxx())", { + # Issue posted on Stackoverflow + # https://stackoverflow.com/questions/75365694/plotly-did-not-show-legend-when-converted-from-ggplot + data <- data.frame( + stringsAsFactors = FALSE, + Level = c("Fast","Fast","Fast","Fast", + "Fast","Fast","Slow","Slow","Slow", + "Slow","Slow","Slow"), + Period = c("1Year","3Month","1Year","3Month", + "1Year","3Month","1Year","3Month", + "1Year","3Month","1Year","3Month"), + X = c(0.002,0.002,0.1,0.1,0.9,0.9, + 0.002,0.002,0.1,0.1,0.9,0.9), + Y = c(1.38,1.29,1.61,1.61,1.74,0.98, + 1.14,0.97,1.09,1.1,0.94,0.58) + ) + + gg <- ggplot(data = data, + aes(x = X, + y = Y, + shape = Period, + color = Level)) + + geom_point(alpha = 0.6, size = 3) + + labs(x = " ", + y = "Value") + + scale_y_continuous(labels = scales::number_format(accuracy = 0.1)) + + guides(color = guide_legend(title = "Period", order = 1), + shape = guide_legend(title = "", order = 2)) + + theme(axis.text.x = element_text(angle = 90)) + + info <- expect_doppelganger_built(gg, "respect-guides") + + expect_equivalent(sum(sapply(info$data, "[[", "showlegend")), 4) +}) p <- ggplot(mtcars, aes(x = mpg, y = wt, color = factor(vs))) + geom_point() @@ -114,4 +148,3 @@ test_that("many legend items", { p <- ggplot(midwest, aes(category, fill = category)) + geom_bar() info <- expect_traces(p, length(unique(midwest$category)), "many legend items") }) -