I want to make a stacked barplot where I can toggle between two data sets using the dropdown menu.
This is the dataset
And my code:
plot_ly(
data=dat,
type=“bar”,
color=~Gender) %>%
add_trace(y=~Employees, visible=T, color=~Gender)%>%
add_trace(y=~Payroll, visible=F, color=~Gender)%>%
layout(
yaxis=list(title=“value”),
barmode=“stack”,
updatemenus=list(
list(buttons=list(
list(method=“restyle”,
args=list(“visible”, list(TRUE, FALSE)),
label=“Employees”),
list(method=“restyle”,
args=list(“visible”, list(FALSE, TRUE)),
label=“Payroll”)
))
)
)
the initial plot is fine
But when I change the dropdown menu to the second option (“Payroll”), I also lose the gender classification. I think it’s because in this line
args=list(“visible”, list(FALSE, TRUE)
there is a switching on and off of the variable I want to change (“Employees” and “Payroll”) but also of Gender.
How do I make it to Gender remains when switching between employees and payroll?