Skip to content

Respect guide(aes = 'none') when constructing legend entries #2067

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
Nov 2, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 7 additions & 3 deletions NEWS.md
Original file line number Diff line number Diff line change
@@ -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
Expand Down
8 changes: 6 additions & 2 deletions R/layers2traces.R
Original file line number Diff line number Diff line change
Expand Up @@ -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))
Expand Down
20 changes: 20 additions & 0 deletions tests/testthat/test-ggplot-legend.R
Original file line number Diff line number Diff line change
Expand Up @@ -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()

Expand Down