3

I use the R plotly package and the functions ggplotly and subplot to create an interactive plot consisting of multiple individually interactive ggplot2 plots. Some of the plots share the same grouping variables.

col <- factor(rep(c(1, 2), 5))
fill <- factor(c(rep("a", 5), rep("b", 5)))
x1 <- x2 <- y1 <- y2 <- 1:10
x3 <- y3 <- 11:20
d1 <- dplyr::tibble(x1 = x1, y1 = y1, col = col)
d2 <- dplyr::tibble(x2 = x2, y2 = y2, col = col, fill = fill)
d3 <- dplyr::tibble(x3 = x3, y3 = y3, col = col)

g1 <- 
  ggplot2::ggplot(d1) + 
  ggplot2::geom_point(ggplot2::aes(x = x1, y = y1, col = col))

g2 <- 
  ggplot2::ggplot(d2) + 
  ggplot2::geom_point(ggplot2::aes(x = x2, y = y2, col = col, fill = fill)) + 
  ggplot2::scale_fill_manual(values = c("red","green")) 

g3 <- 
  ggplot2::ggplot(d3) + 
  ggplot2::geom_point(ggplot2::aes(x = x3, y = y3, col = col))

plotly::subplot(plotly::ggplotly(g1), plotly::ggplotly(g2), plotly::ggplotly(g3))

enter image description here

1) How can I remove the duplicated "col" labels in the interactive plotly legend?

2) How can I remove the legend for "fill", but keep the legend for "col"?


EDIT: I know about the following "dirty" solution, which is to manually disable the legend:

t <- 
plotly::subplot(plotly::ggplotly(g1), plotly::ggplotly(g2), plotly::ggplotly(g3))

t$x$data[[1]]$showlegend <- FALSE
t$x$data[[2]]$showlegend <- FALSE
t$x$data[[3]]$showlegend <- FALSE
t$x$data[[4]]$showlegend <- FALSE

However, this requires me to know the positions of the list elements in advance, which is why I am looking for a more general solution.

3
  • Hi, did you find a solution to this issue? Commented May 1, 2020 at 7:50
  • Unfortunately, no. I ended up using the "dirty" solution. Commented May 2, 2020 at 8:41
  • Hi, did anyone found a more elegant solution? Commented Jan 22, 2022 at 23:12

1 Answer 1

2

Another way to manually remove the unwanted legends is to use style(). In your example, lt <- t %>% style(t, showlegend = FALSE, traces = 3:n), where n<-8 is defined before, will suppress the unwanted legends.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.