I'm creating an HTML file with RMarkdown that contain's some interactive plots. With some help, I got to create a dropdown list to filter a variable that changes the plot. I want to do 2 things:
First, I want the year of "2023" to appear as the first plot. Secondly, I would like to omit the first plot that appears with all the years and months grouped.
The code I'm working with:
data <- data.frame(
anoobito = sample(2019:2023),
diadasemana = sample(c("Janeiro", "Fevereiro", "Março", "Abril",
"Maio", "Junho", "Julho", "Agosto", "Setembro", "Outubro", "Novembro", "Dezembro"), 23000, replace = TRUE)
)
mes <- data %>%
group_by(anoobito, MES_OBITO) %>%
tally %>%
mutate(MES_OBITO = factor(MES_OBITO, levels = c("Janeiro", "Fevereiro", "Março", "Abril",
"Maio", "Junho", "Julho", "Agosto", "Setembro", "Outubro", "Novembro", "Dezembro"))) %>%
rename("Mês" = "MES_OBITO",
"Ano do Óbito" = "anoobito")
fig <- mes |>
plot_ly(x = ~Mês,
y = ~n,
split = ~`Ano do Óbito`,
type = 'bar',
hoverinfo = 'text',
textposition = "none",
text = ~paste('</br> Ano do Óbito: ', `Ano do Óbito`,
'</br> Mês do Óbito: ', Mês,
'</br> Óbitos: ', n))|>
layout(width = 820,
xaxis = list(title = "Mês do Óbito", linecolor = 'black'),
yaxis = list(side = 'left', title = 'Número de Óbitos', showgrid = F, zeroline = T,
linecolor = 'black', range = c(0, max(mes$n)+10)),
colorway = c("#4567a9", "#118dff", "#107dac", "#1ebbd7", "#064273", "#71c7ec"),
showlegend = T,
updatemenus = list(
list(
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")
)
)
) ,
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
)
)
fig
When I knit it to HTML, the first plot that appears is the one below:
Is there any way I can omit this first plot and only the show the respective plot for each year when selecting the year on the dropdown menu?

activeargument inupdatemenusand set to 4 (5th plot, zero-indexed), for example:updatemenus = list(list(active = 4, buttons = list(...and (2) to omit the first grouped plot, you might want to separate out the 5 plot years in plotly; if you did that, you can makevisible = "legendonly"for 4 of the 5 plots as you have now, then add the 5th plot which would be visible...visible.