Stacked bar plot with dropdown select menu

I want to make a stacked barplot where I can toggle between two data sets using the dropdown menu.

This is the dataset
image

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
image

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?

found the answer here: Is this a bug in dropdown events?

fixed 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, TRUE, FALSE, FALSE)),
label=“Employees”),
list(method=“restyle”,
args=list(“visible”, list(FALSE, FALSE, TRUE, TRUE)),
label=“Payroll”)
))
)
)