I'm working with plotly in RMarkdown to create interactive plots with dropdown menu list to filter years.
I'm having trouble trying to omit the empty categories from each year. I only want the top 5 categories for each year. This is the code I'm working with:
cid <- import("R:/COE/GIE/0 SERVIDORES/Lana Meijinhos/CID-10-SUBCATEGORIAS.CSV") %>%
select(SUBCAT, DESCRABREV)
malf <- nv %>%
filter(idanomal == "1")
malf$codanomal <- gsub("(.{4})", "*\\1", malf$codanomal)
malf_cid <- malf %>%
separate_rows(codanomal, sep = "\\*") %>%
filter(codanomal != "") %>%
mutate(codanomal = ifelse(codanomal == "Q699", "Q690", codanomal)) %>%
group_by(anonasc, codanomal) %>%
summarise(frequency = n(), .groups = "drop") %>%
arrange(anonasc, desc(frequency)) %>%
spread(., key=anonasc, value=frequency) %>%
adorn_totals("col")
malf_cid[is.na(malf_cid)] <- 0
res <- nv %>%
group_by(anonasc) %>%
tally %>%
spread(., key=anonasc, value=n)
colnames(res) <- paste0("nv", colnames(res))
malf_cid <- bind_cols(malf_cid, res)
malf_cid$tx20 <- round((malf_cid$"2020"/res$nv2020)*1000,1)
malf_cid$tx21 <- round((malf_cid$"2021"/res$nv2021)*1000,1)
malf_cid$tx22 <- round((malf_cid$"2022"/res$nv2022)*1000,1)
malf_cid$tx23 <- round((malf_cid$"2023"/res$nv2023)*1000,1)
malf_cid$tx24 <- round((malf_cid$"2024"/res$nv2024)*1000,1)
malf_cid <- malf_cid %>%
left_join(cid, by = c("codanomal" = "SUBCAT")) %>%
select(-codanomal) %>%
rename("codanomal" = "DESCRABREV") %>%
arrange(desc(Total))
graf <- malf_cid %>%
select(codanomal, "2020":"2024") %>%
pivot_longer(cols = starts_with("20"), names_to = "year", values_to = "n")
graf2 <- malf_cid %>%
select(codanomal, tx20:tx24) %>%
pivot_longer(cols = starts_with("tx"), names_to = "year", values_to = "frequency")
graf2$year <- ifelse(graf2$year == "tx20", "2020",
ifelse(graf2$year == "tx21", "2021",
ifelse(graf2$year == "tx22", "2022",
ifelse(graf2$year == "tx23", "2023",
ifelse(graf2$year == "tx24", "2024", NA)))))
graf3 <- malf_cid %>%
select(codanomal, nv2020:nv2024) %>%
pivot_longer(cols = starts_with("nv"), names_to = "year", values_to = "nv")
graf3$year <- ifelse(graf3$year == "nv2020", "2020",
ifelse(graf3$year == "nv2021", "2021",
ifelse(graf3$year == "nv2022", "2022",
ifelse(graf3$year == "nv2023", "2023",
ifelse(graf3$year == "nv2024", "2024", NA)))))
graf <- graf %>%
left_join(graf2, by = c("year", "codanomal")) %>%
left_join(graf3, by = c("year", "codanomal"))
graf <- graf %>%
group_by(year) %>%
mutate(codanomal = reorder(codanomal, frequency)) %>%
ungroup() %>%
mutate(frequency = ifelse(frequency == 0, NA, frequency),
n = ifelse(n == 0, NA, n),
nv = ifelse(is.na(n), NA, nv),
year = ifelse(is.na(n), NA, year))
fig <- plot_ly() %>%
add_trace(data = graf %>% filter(year == "2020" & frequency > 0) %>% arrange(desc(frequency)) %>% slice(1:5),
x = ~frequency,
y = ~codanomal,
type = 'bar',
name = '2020',
hoverinfo = 'text',
textposition = "none",
text = ~paste('</br> Ano do Nascimento: ', year,
'</br> Causa: ', codanomal,
'</br> Número de Anomalias: ', n,
'</br> Número de Nascidos Vivos: ', nv,
'</br> Prevalência: ', frequency),
visible = TRUE) %>%
add_trace(data = graf %>% filter(year == "2021" & frequency > 0) %>% arrange(desc(frequency)) %>% slice(1:5),
x = ~frequency,
y = ~codanomal,
type = 'bar',
name = '2021',
hoverinfo = 'text',
textposition = "none",
text = ~paste('</br> Ano do Nascimento: ', year,
'</br> Causa: ', codanomal,
'</br> Número de Anomalias: ', n,
'</br> Número de Nascidos Vivos: ', nv,
'</br> Prevalência: ', frequency),
visible = FALSE) %>%
add_trace(data = graf %>% filter(year == "2022" & frequency > 0) %>% arrange(desc(frequency)) %>% slice(1:5),
x = ~frequency,
y = ~codanomal,
type = 'bar',
name = '2022',
hoverinfo = 'text',
textposition = "none",
text = ~paste('</br> Ano do Nascimento: ', year,
'</br> Causa: ', codanomal,
'</br> Número de Anomalias: ', n,
'</br> Número de Nascidos Vivos: ', nv,
'</br> Prevalência: ', frequency),
visible = FALSE) %>%
add_trace(data = graf %>% filter(year == "2023" & frequency > 0) %>% arrange(desc(frequency)) %>% slice(1:5),
x = ~frequency,
y = ~codanomal,
type = 'bar',
name = '2023',
hoverinfo = 'text',
textposition = "none",
text = ~paste('</br> Ano do Nascimento: ', year,
'</br> Causa: ', codanomal,
'</br> Número de Anomalias: ', n,
'</br> Número de Nascidos Vivos: ', nv,
'</br> Prevalência: ', frequency),
visible = FALSE) %>%
add_trace(data = graf %>% filter(year == "2024" & frequency > 0) %>% arrange(desc(frequency)) %>% slice(1:5),
x = ~frequency,
y = ~codanomal,
type = 'bar',
name = '2024',
hoverinfo = 'text',
textposition = "none",
text = ~paste('</br> Ano do Nascimento: ', year,
'</br> Causa: ', codanomal,
'</br> Número de Anomalias: ', n,
'</br> Número de Nascidos Vivos: ', nv,
'</br> Prevalência: ', frequency),
visible = FALSE) %>%
layout(width = 820,
yaxis = list(title = " ", linecolor = 'black'),
xaxis = list(side = 'bottom', title = 'Prevalência de Malformação Congênita (/1.000 nascidos vivos)', showgrid = F, zeroline = T,
linecolor = 'black', range = c(0, max(graf$frequency)+2)),
colorway = c("#4567a9", "#118dff", "#107dac", "#1ebbd7", "#064273"),
showlegend = F,
margin = list(l = 0, r = 0, b = 0, t = 0, pad = 0), # Adjusted to remove margins
xaxis = list(
showline = TRUE, # Added to show x-axis line
showgrid = FALSE # Added to hide x-axis grid
),
updatemenus = list(
list(
buttons = list(
list(method = "restyle",
args = list("visible", list(TRUE, FALSE, FALSE, FALSE, FALSE)),
label = "2020"),
list(method = "restyle",
args = list("visible", list(FALSE, TRUE, FALSE, FALSE, FALSE)),
label = "2021"),
list(method = "restyle",
args = list("visible", list(FALSE, FALSE, TRUE, FALSE, FALSE)),
label = "2022"),
list(method = "restyle",
args = list("visible", list(FALSE, FALSE, FALSE, TRUE, FALSE)),
label = "2023"),
list(method = "restyle",
args = list("visible", list(FALSE, FALSE, FALSE, FALSE, TRUE)),
label = "2024")
),
direction = "down",
pad = list(r = 10, t = 10),
showactive = TRUE,
x = -0.4,
xanchor = "left",
y = 1.1,
yanchor = "top"
)
)
)
fig
And this is the plot it generates for 2024, for example:
I only want to keep the categories that have actually bars and omit the empty ones. I've tried everything but nothing seems to work.
Any tips?



graftable with enough data to generate the chart in the figure. Be careful not to inadvertently expose your personal data or violate your organization's PSI (olha a LGPD!).