1

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:

enter image description here

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?

3
  • A couple of initial thoughts: (1) for the first initial plot, you can use the active argument in updatemenus and 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 make visible = "legendonly" for 4 of the 5 plots as you have now, then add the 5th plot which would be visible... Commented Mar 5, 2024 at 20:19
  • @Ben the active part worked, now 2023 appears as the default selection in the dropdown menu, but I didn't get the visible = "legendonly" part. I set all of the years (but 2023) with "legendonly", but the grouped plot keeps appearing, even when I select the year in the dropdown menu. Commented Mar 6, 2024 at 10:49
  • See below answer for example that will show 2023 alone using visible. Commented Mar 6, 2024 at 14:42

1 Answer 1

1

To show a specific initial plot, use active argument in updatemenus. In this case, use 4 (zero-indexed from 0-4 corresponding to 2019 to 2023).

To show initial bar plot, would divide up your plots and use add_bars. Here you can set visible to FALSE for years 2019 to 2022, and TRUE for 2023.

mes |> 
  plot_ly(hoverinfo = 'text',
          textposition = "none",
          text = ~paste('</br> Ano do Óbito: ', `Ano do Óbito`,
                        '</br> Mês do Óbito: ', Mês,
                        '</br> Óbitos: ', n)) |> 
  add_bars(data = mes[mes$`Ano do Óbito` %in% 2019:2022,],
           x = ~Mês, 
           y = ~n, 
           split = ~`Ano do Óbito`,
           visible = F) |>
  add_bars(data = mes[mes$`Ano do Óbito` == 2023,],
           x = ~Mês, 
           y = ~n, 
           split = ~`Ano do Óbito`,
           visible = T) |>
  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 = F,
         updatemenus = list(
           list(
             active = 4,
             buttons = list(
               list(method = "restyle",
                    args = list("visible", list(TRUE, FALSE, FALSE, FALSE, FALSE)),
                    label = "2019"),
               list(method = "restyle",
                    args = list("visible", list(FALSE, TRUE, FALSE, FALSE, FALSE)),
                    label = "2020"),
               list(method = "restyle",
                    args = list("visible", list(FALSE, FALSE, TRUE, FALSE, FALSE)),
                    label = "2021"),
               list(method = "restyle",
                    args = list("visible", list(FALSE, FALSE, FALSE, TRUE, FALSE)),
                    label = "2022"),
               list(method = "restyle",
                    args = list("visible", list(FALSE, FALSE, FALSE, FALSE, TRUE)),
                    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
         )
  )
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.