I’m trying to plot an age pyramid in R using ploty, and I also want to add a dropdown menu list to filter the plot by year.
Here’s the code I’m using:
dadosbr <- data.frame(
SEXO = sample(c("Masculino", "Feminino"), 7525, replace = TRUE),
FAIXA_ETARIA = sample(c("0-19", "20-29", "30-39", "40-49", "50-59", "60 ou mais"), 7525, replace = TRUE),
ANO_TRAT = sample(2019:2024, 7525, replace = TRUE))
sexo <- dadosbr %>%
group_by(SEXO, FAIXA_ETARIA, ANO_TRAT) %>%
tally() %>%
ungroup()
sexo <- sexo %>%
group_by(ANO_TRAT) %>%
mutate(percent = round(n / sum(n) * 100,1))
sexo <- sexo %>%
mutate(percent = ifelse(SEXO == "Feminino", -percent, percent))
plot_ly(sexo, hoverinfo = 'text', textposition = "none",
text = ~paste('</br> Ano do Início do Tratamento: ', ANO_TRAT,
'</br> Sexo: ', SEXO,
'</br> Número de Casos de TBDR: ', n,
'</br> %: ', percent)) %>%
add_trace(data = sexo[sexo$ANO_TRAT == 2019, ],
x = ~FAIXA_ETARIA, split = ~SEXO, y = ~n, type = 'bar', name = '2019', visible = TRUE) %>%
add_trace(data = sexo[sexo$ANO_TRAT == 2020, ],
x = ~FAIXA_ETARIA, split = ~SEXO, y = ~n, type = 'bar', name = '2020', visible = FALSE) %>%
add_trace(data = sexo[sexo$ANO_TRAT == 2021, ],
x = ~FAIXA_ETARIA, split = ~SEXO, y = ~n, type = 'bar', name = '2021', visible = FALSE) %>%
add_trace(data = sexo[sexo$ANO_TRAT == 2022, ],
x = ~FAIXA_ETARIA, split = ~SEXO, y = ~n, type = 'bar', name = '2022', visible = FALSE) %>%
add_trace(data = sexo[sexo$ANO_TRAT == 2023, ],
x = ~FAIXA_ETARIA, split = ~SEXO, y = ~n, type = 'bar', name = '2023', visible = FALSE) %>%
add_trace(data = sexo[sexo$ANO_TRAT == 2024, ],
x = ~FAIXA_ETARIA, split = ~SEXO, y = ~n, type = 'bar', name = '2024', visible = FALSE) %>%
layout(width = 820,
xaxis = list(title = "Raça/cor", linecolor = 'black', showline = TRUE, showgrid = FALSE),
yaxis = list(title = 'Número de Casos de TBR', showgrid = FALSE, zeroline = TRUE,
linecolor = 'black', range = c(0, 100)),
colorway = c("#4567a9", "#118dff", "#107dac", "#1ebbd7", "#064273", "#71c7ec"),
barmode = 'stack',
showlegend = FALSE,
updatemenus = list(
list(
active = 0,
buttons = list(
list(method = "restyle",
args = list("visible", list(TRUE, FALSE, FALSE, FALSE, FALSE, FALSE)),
label = "2019"),
list(method = "restyle",
args = list("visible", list(FALSE, TRUE, FALSE, FALSE, FALSE, FALSE)),
label = "2020"),
list(method = "restyle",
args = list("visible", list(FALSE, FALSE, TRUE, FALSE, FALSE, FALSE)),
label = "2021"),
list(method = "restyle",
args = list("visible", list(FALSE, FALSE, FALSE, TRUE, FALSE, FALSE)),
label = "2022"),
list(method = "restyle",
args = list("visible", list(FALSE, FALSE, FALSE, FALSE, TRUE, FALSE)),
label = "2023"),
list(method = "restyle",
args = list("visible", list(FALSE, FALSE, FALSE, FALSE, FALSE, TRUE)),
label = "2024")
)
)
),
margin = list(l = 0, r = 0, b = 0, t = 0, pad = 0)
)
I don’t know what’s going wrong. I even tried to chage it to “stack”, but it doesn’t work. I want the output to be something like this:
I made this one with ggplot, but I don’t know how to do it with plotly.