I would like to produce a plotly graph in which it is possible to click on a button to select which variable is used to color the markers.
Now, the following example produces a plotly plot with two buttons, except that they don't work:
df <- data.frame(
x=rnorm(10),
y=rnorm(10))
group1 <- sample(c("A", "B"), 10, replace=TRUE)
group2 <- sample(c("X", "Y"), 10, replace=TRUE)
p <- plot_ly(data=df, type="scatter", mode="markers", x=~ x, y=~ y)
p %>% layout(
updatemenus=list(list(
type="buttons",
y=.8,
buttons=list(
list(method="restyle",
args=list("color", group1),
label="Group 1"),
list(method="restyle",
args=list("color", group2),
label="Group 2")
)
)))
What I could probably do is to add different markers with different colors and use buttons to toggle their visibility only. However, I already use that trick for showing different data sets on the same plot and adding each data set with all the different options would unnecessarily duplicate the data.
Is there a simpler solution?