You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

如何在R Markdown的Plotly中设置下拉菜单默认显示2023年并隐藏初始总览图

问题

我正在使用R Markdown生成包含交互式图表的HTML文件,已实现下拉菜单按年份筛选切换图表的功能,目前有两个需求:

  • 默认显示2023年对应的图表;
  • 移除初始加载时显示的所有年份与月份分组的总览图。

以下是我使用的代码:

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

解决方法

要实现这两个需求,只需对代码做两处关键修改:

1. 初始仅显示2023年数据

在plot_ly()函数中添加visible参数,指定初始状态下仅2023年对应的轨迹可见。由于split = ~Ano do Óbito会按2019-2023的顺序生成5条轨迹,因此设置visible = c(FALSE, FALSE, FALSE, FALSE, TRUE),隐藏前四年数据,只保留2023年。

2. 设置下拉菜单默认选中2023年

在updatemenus的按钮列表中,给2023年对应的按钮添加selected = TRUE,让下拉菜单默认显示选中2023年的状态。

修改后的完整代码:

data <- data.frame(
  anoobito = sample(2019:2023),
  # 修正列名不匹配问题,原代码中diadasemana与后续MES_OBITO不一致会报错
  MES_OBITO = 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),
          # 添加visible参数,初始仅显示2023年
          visible = c(FALSE, FALSE, FALSE, FALSE, TRUE))|> 
  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)),
                    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",
                    # 添加selected参数,默认选中2023年
                    selected = TRUE)
             )
           )
         ) ,
         margin = list(l = 0, r = 0, b = 0, t = 0, pad = 0),
         xaxis = list(
           showline = TRUE,
           showgrid = FALSE
         )
  )

fig

内容的提问来源于stack exchange,提问作者Lana Meijinhos

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.06.28 17:55:09