I have replicated the unexpected behavior using mtcars.
I want to be able to toggle between Numbers and Percents using a dropdown, but the “Number” from the previously visible plot of the second cateogry seems to get carried over when I toggle to “Percent.”
mtcars %>%
mutate(vs = ifelse(vs == 0, "V", "S"), am = ifelse(am == 0, "auto", "manual")) %>%
group_by(vs, am) %>%
summarise(total = n(), num_big = sum(wt > 2.5),
percent_big = (sum(wt > 2.5) / n())*100) %>%
plot_ly(x = ~vs, color = ~am, colors = c("green", "blue")) %>%
add_bars(y = ~num_big, name = "Number Big Cars", text = ~num_big) %>%
add_bars(y = ~percent_big, name = "Percent Big Cars", text = ~percent_big, visible = F) %>%
layout(title = "Big Cars per Category",
showlegend = T,
barmode = "group",
yaxis = list(title = ""),
updatemenus = list(
list(
buttons = list(
list(method = "restyle",
args = list(
list("visible", list(TRUE, FALSE))),
label = "Number Big Cars"),
list(method = "restyle",
args = list("visible", list(FALSE, TRUE)),
label = "Percent Big Cars")))
))
Surprisingly, the issue is somewhat fixed by adding an extra “FALSE, TRUE” in the middle of the second “visible” arg. But I still can’t toggle back to “Number.”
mtcars %>%
mutate(vs = ifelse(vs == 0, "V", "S"), am = ifelse(am == 0, "auto", "manual")) %>%
group_by(vs, am) %>%
summarise(total = n(), num_big = sum(wt > 2.5),
percent_big = (sum(wt > 2.5) / n())*100) %>%
plot_ly(x = ~vs, color = ~am, colors = c("green", "blue")) %>%
add_bars(y = ~num_big, name = "Number Big Cars", text = ~num_big) %>%
add_bars(y = ~percent_big, name = "Percent Big Cars", text = ~percent_big, visible = F) %>%
layout(title = "Big Cars per Category",
showlegend = T,
barmode = "group",
yaxis = list(title = ""),
updatemenus = list(
list(
buttons = list(
list(method = "restyle",
args = list(
list("visible", list(TRUE, FALSE))),
label = "Number Big Cars"),
list(method = "restyle",
args = list("visible", list(FALSE, FALSE, TRUE, TRUE)),
label = "Percent Big Cars")))
))
What’s going on? How can I toggle back to Number?
Any tips much appreciated.
Many thanks,
Emma