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

Cannot control visibility of single legend item via style() #1873

Closed
ismirsehregal opened this issue Oct 27, 2020 · 1 comment
Closed

Cannot control visibility of single legend item via style() #1873

ismirsehregal opened this issue Oct 27, 2020 · 1 comment

Comments

@ismirsehregal
Copy link

ismirsehregal commented Oct 27, 2020

Not sure if I am missing something but I'm not able to show a single legend item via style:

style(p, showlegend = TRUE, traces = 1L) using plotly 4.9.2.9000

Once it is the last legend item it is hidden automatically.

Here is a modified example from ?style:

library(plotly)

marker <- list(
  color = "red",
  line = list(
    width = 20, 
    color = "black"
  )
)

p1 <- plot_ly(x = 1:10, y = 1:10, name = rep(LETTERS[1:2], each = 5), marker = marker, type = "scatter", mode = "markers")
p1 <- style(p1, marker = list(line = list(color = "blue")), traces = 2L) # as expected
p1

style(p1, showlegend = FALSE, traces = 1L)  # when 2 traces exist - hiding all legend items instead of only one

p2 <- add_trace(p1, x = 11:15, y = 11:15, name = "C", showlegend = FALSE, inherit = FALSE, type = "scatter", mode = "markers") # as expected
p2

p3 <- add_trace(p1, x = 11:15, y = 11:15, name = "C", showlegend = TRUE, inherit = FALSE, type = "scatter", mode = "markers") # as expected
p3

p4 <- style(p3, showlegend = FALSE, traces = 2L)  # when 2 traces exist - as expected, hiding only one legend item
p4

p5 <- style(p4, showlegend = FALSE, traces = 1L)  # hiding all legend items instead of only one
style(p5, showlegend = TRUE, traces = 3L)  # hiding all legend items instead showing only one

PS: coming from here.

@ismirsehregal ismirsehregal changed the title Cannot control visibility of specific legend items via style() Cannot control visibility of single legend item via style() Oct 28, 2020
@ismirsehregal
Copy link
Author

layout(showlegend = TRUE) needs to be set for this to work as expected:

library(plotly)

marker <- list(
  color = "red",
  line = list(
    width = 20, 
    color = "black"
  )
)

p0 <- plot_ly(x = 1:10, y = 1:10, name = rep(LETTERS[1:2], each = 5), marker = marker, type = "scatter", mode = "markers") %>% layout(showlegend = TRUE)
p0 <- style(p1, marker = list(color = "green", line = list(width = 20, color = "blue")), traces = 2L) # as expected
p0

p1 <- style(p0, showlegend = FALSE, traces = 1L)  # when 2 traces exist - hiding all legend items instead of only one
p1

p2 <- add_trace(p1, x = 11:15, y = 11:15, name = "C", showlegend = FALSE, inherit = FALSE, type = "scatter", mode = "markers") # as expected
p2

p3 <- add_trace(p1, x = 11:15, y = 11:15, name = "C", showlegend = TRUE, inherit = FALSE, type = "scatter", mode = "markers") # as expected
p3

p4 <- style(p3, showlegend = FALSE, traces = 2L)  # when 2 traces exist - as expected, hiding only one legend item
p4

p5 <- style(p4, showlegend = FALSE, traces = 1L)  # hiding all legend items instead of only one
style(p5, showlegend = TRUE, traces = 3L)  # hiding all legend items instead showing only one

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant