Skip to content
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

Fix bug: ggplotly() no longer removes legends #2234

Merged
merged 3 commits into from
Feb 13, 2023
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
2 changes: 1 addition & 1 deletion NEWS.md
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down
4 changes: 3 additions & 1 deletion R/layers2traces.R
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
35 changes: 34 additions & 1 deletion tests/testthat/test-ggplot-legend.R
Original file line number Diff line number Diff line change
Expand Up @@ -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()
Expand Down Expand Up @@ -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")
})