2

Suppose I am building a plotly scatterplot that displays three different groups (here specified by colour). I can show/hide each group individually by clicking at the groups in the legend.

library(ggplot2)
library(plotly)
d <- data.frame("a" = sample(1:50, 20, T), "b" = sample(1:50, 20, T), 
                        "col" = factor(sample(1:3, 20, T)))
        gg <- ggplot() + geom_point(aes(x = a, y = b, color = col), data = d)
        gg

plotly_build(gg)

Is there a way to add a button to the legend to show/hide all points ?

nhoeft
  • 535
  • 6
  • 17
  • You mean an additional button to hide all? Or do you want to get rid of the individual functionality? – Martin Schmelzer Jun 24 '16 at 14:26
  • Can you expand on the use case, as I can't quite understand why you'd want to do this. Understanding the purpose might help with thinking of the answer. – dww Jun 24 '16 at 14:26
  • @MartinDabbelJuSmelter I would like to add a button to show/hide all while still keeping the individual functionality. I have a very large scatterplot with 50 groups. When comparing only two or three groups of the 50 it is quite tedious to hide all others manually. – nhoeft Jun 24 '16 at 14:54
  • Ok, that makes sense. Would it help you to start with all the traces hidden by default and show them one at a time by clicking. This will be a lot easer. – dww Jun 24 '16 at 14:59
  • Yes, this would also work for me. Could you show me how to hide the traces by default? – nhoeft Jun 24 '16 at 15:14

2 Answers2

4

This may be closer to what was originally asked for, but it is just a "button" not a legend entry, so placement is difficult.

library(ggplot2)
library(plotly)
d <- data.frame("a" = sample(1:50, 20, T), "b" = sample(1:50, 20, T), 
                "col" = factor(sample(1:3, 20, T)))
gg <- ggplot() + geom_point(aes(x = a, y = b, color = col), data = d)
gg

plotly_build(gg) %>%
layout(updatemenus = list(
    list(
        type = "buttons",
        direction = "right",
        xanchor = "center",
        yanchor = "top",
        showactive = FALSE,
        x = 1.05,
        y = 0.2,
        buttons = list(
            list(method = "restyle",
                 args = list("visible", "all"),
                 label = "show all"),
            list(method = "restyle",
                 args = list("visible", "legendonly"),
                 label = "hide all")
        )
    )
))

This produces (depending on the version of plotly) the following drop-down "buttons":

plotly output showing expanded buttons

I don't know how to dynamically update the buttons based on the current visibility or how to make both buttons permanently visible instead of only available through the drop-down menu.

1

I don't know of a way to toggle all points simultaneously, but (as indicated in the comments that this would be a suitable alternative) it is possible to start with them all hidden and make points visible one at a time

plot_ly(data=d, x=a, y=b, type='scatter', mode='markers', color=col, visible='legendonly') 
dww
  • 30,425
  • 5
  • 68
  • 111