I am trying to create a plotly graph with selectable color attribute so that passing the selected categorical data column as color variable, it changes the color of marks as well as the legend of my scatter plot.
Here is the Example:
df <- data.frame(x = runif(200), y = runif(200),
z = sample(c("a", "b", "c"), 200, replace=TRUE),
w = sample(c("d", "e", "f",'g'), 200, replace=TRUE))
p <- plot_ly(df, x = ~x)%>%
add_markers(y = ~y, color = ~z,visible=T)%>%
layout(
title = "Drop down menus - color",
xaxis = list(domain = c(0.1, 1)),
yaxis = list(title = "y"),
updatemenus = list(
list(
y = 0.7,
buttons = list(
list(method = "restyle",
args = list("color", list(~z)),
label = "group by z"),
list(method = "restyle",
args = list("color", list(~w)),
label = "group by w")))
))
However, switching between the two options, the plot does not change. Apparently, we could change any data attribute with dropdown events except the color! Any help would be appreciated.