From cc41d21d51bf1100915e0f156f786c0c775b50e2 Mon Sep 17 00:00:00 2001 From: Carson Date: Tue, 2 Nov 2021 12:51:42 -0500 Subject: [PATCH 1/2] Close #2043: respect guide(aes = 'none') when constructing legend entries --- R/layers2traces.R | 8 ++++++-- 1 file changed, 6 insertions(+), 2 deletions(-) diff --git a/R/layers2traces.R b/R/layers2traces.R index ca260f6835..ec5dbd33be 100644 --- a/R/layers2traces.R +++ b/R/layers2traces.R @@ -102,12 +102,16 @@ 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")) for (i in seq_along(datz)) { d <- datz[[i]] # variables that produce multiple traces and deserve their own legend entries - split_legend <- paste0(names(discreteScales), "_plotlyDomain") + split_legend <- paste0( + setdiff(names(discreteScales), aes_no_guide), + "_plotlyDomain" + ) # add variable that produce multiple traces, but do _not_ deserve entries - split_by <- c(split_legend, "PANEL", "frame", split_on(d)) + split_by <- c(split_legend, aes_no_guide, "PANEL", "frame", split_on(d)) # ensure the factor level orders (which determines traces order) # matches the order of the domain values split_vars <- intersect(split_by, names(d)) From 097f9e9dc0028a4bfe0cfec1c3294b31b4ec3973 Mon Sep 17 00:00:00 2001 From: Carson Date: Tue, 2 Nov 2021 12:59:37 -0500 Subject: [PATCH 2/2] add test; update news --- NEWS.md | 10 +++++++--- tests/testthat/test-ggplot-legend.R | 20 ++++++++++++++++++++ 2 files changed, 27 insertions(+), 3 deletions(-) diff --git a/NEWS.md b/NEWS.md index 0c8c4ab142..97a5af91c1 100644 --- a/NEWS.md +++ b/NEWS.md @@ -1,16 +1,20 @@ # 4.10.0.9000 -## Improvements +## New features -* `ggplotly()` now supports the `{ggalluvial}` package. (#2061, @moutikabdessabour) -* `ggplotly()` does not issue warnings with `options(warnPartialMatchArgs = TRUE)` any longer. (#2046, @bersbersbers) +* `ggplotly()` now supports the `{ggalluvial}` package. (#2061, thanks @moutikabdessabour) ## Bug fixes * `ggplotly()` now converts `stat_ecdf()` properly. (#2065) * `ggplotly()` now correctly handles `geom_tile()` with no `fill` aesthetic. (#2063) +* `ggplotly()` now respects `guide(aes = "none")` (e.g., `guide(fill = "none")`) when constructing legend entries. (#2067) * Fixed an issue with translating `GGally::ggcorr()` via `ggplotly()`. (#2012) +## Improvements + +* `ggplotly()` does not issue warnings with `options(warnPartialMatchArgs = TRUE)` any longer. (#2046, thanks @bersbersbers) + # 4.10.0 ## Breaking changes in JavaScript API diff --git a/tests/testthat/test-ggplot-legend.R b/tests/testthat/test-ggplot-legend.R index 5ee0df7811..11d0fd9886 100644 --- a/tests/testthat/test-ggplot-legend.R +++ b/tests/testthat/test-ggplot-legend.R @@ -39,6 +39,26 @@ test_that("legend vanishes when theme(legend.position = 'none'')", { expect_identical(info$layout$showlegend, FALSE) }) +test_that("aesthetics can be discarded from legend with guide(aes = 'none')", { + df1 <- data.frame( + Date = seq(as.Date("2021-01-01"), as.Date("2021-01-10"), "days"), + Series = c(rep("SeriesA", 10), rep("SeriesB", 10)), + Values = rnorm(n = 20), + Mean = 0, V1 = 2, V2 = -2 + ) + + p <- ggplot(df1, aes(x=Date, y=Values, color = Series, linetype = Series, shape = Series)) + + geom_line(aes(x = Date, y = Mean, color = "Mean", linetype = "Mean")) + + geom_line(aes(x = Date, y = V1, color = "QC", linetype = "QC")) + + geom_line(aes(x = Date, y = V2, color = "QC", linetype = "QC")) + + geom_line() + + geom_point() + + guides(shape = "none", linetype = "none") + + expect_doppelganger(p, "guide-aes-none") +}) + + p <- ggplot(mtcars, aes(x = mpg, y = wt, color = factor(vs))) + geom_point()