使用plotly在R中创建下拉列表?

问题描述 投票:0回答:1

我想为此图创建一个包含 2019 年至 2024 年的下拉列表:

enter image description here

我正在使用 R Markdown 和plotly 来创建交互式绘图。我设法制作了 2021 年的绘图,但我似乎无法添加下拉列表来按年份过滤绘图。

我正在使用的代码:

dadosbr <- data.frame(
  TIPO_DE_RESISTENCIA = sample(c("Primária", "Adquirida", "Ignorado"), 7525, replace = TRUE),
  Regiao = sample(c("Centro-Oeste", "Nordeste", "Norte", "Sudeste", "Sul"), 7525, replace = TRUE),
  ANO_TRAT = sample(2019:2024, 7525, replace = TRUE),
  UF = sample(c("Acre", "Alagoas", "Amapá", "Amazonas", "Bahia", "Ceará", "Distrito Federal", "Espírito Santo", "Goiás", "Maranhão", "Mato Grosso", "Mato Grosso do Sul",
                "Minas Gerais", "Pará", "Paraíba", "Paraná", "Pernambuco", "Piauí", "Rio de Janeiro", "Rio Grande do Norte", "Rio Grande do Sul", "Rondônia", "Roraima",
                "Santa Catarina", "São Paulo", "Sergipe", "Tocantins"), 7525, replace = TRUE))

graf9 <- dadosbr %>%
  group_by(ANO_TRAT, Regiao, TIPO_DE_RESISTENCIA, UF) %>%
  tally() %>%
  mutate(TIPO_DE_RESISTENCIA = factor(TIPO_DE_RESISTENCIA, levels = c("Primária", "Adquirida", "Ignorado")))

# Convert ggplot object to plotly
graf9_plotly <- ggplot(graf9, aes(x = UF, y= n, fill = TIPO_DE_RESISTENCIA)) +
  geom_bar(position = "fill", stat = "identity") +
  labs(x = "UF de residência", 
       y = "% casos TBDR",
       fill = "Tipo de resistência")+
  scale_fill_brewer(palette = "Set1", direction = 1) +
  theme_classic() + 
  scale_y_continuous(labels = scales::percent, expand = expansion(mult = c(0, .1))) +
  facet_grid(. ~ Regiao , scales = "free", space = "free") +
  theme(axis.text.x = element_text(angle = 90, hjust = 1, vjust = 0.5)) +
  theme(
    legend.position = "right",
    axis.line = element_line(colour = "black"),
    axis.text.x = element_text(colour = "black"),
    axis.text.y = element_text(colour = "black"),
    plot.caption = element_text(hjust = 0.5, vjust = 1, margin = margin(t = 10), size = 10),
    plot.title = element_text(hjust = 0.5, vjust = 1, margin = margin(t = 10), size = 12)
  )

# Convert ggplot to plotly
graf9_plotly <- ggplotly(graf9_plotly)

# Display the plotly object
graf9_plotly

我有 ggplot 代码,并在 chatgpt 的帮助下我将其制作成交互式绘图。

r plotly
1个回答
0
投票

我不认为你可以用

ggplotly()
创建一个绘图下拉菜单。相反,您可以使用
Plotly
库本身来实现此目的。这是一个例子:

library(plotly)

fig <- iris |> 
  plot_ly(x = ~Species, 
          y = ~Sepal.Width, 
          split = ~Species,
          type = "violin") |> 
  layout(
    title = "Dropdown Menu in plotly in R",
    updatemenus = list(
      list(
        buttons = list(
          list(
            method = "restyle",
            args = list("type", "violin"),
            label = "Violin Plot"
          ),
          list(
            method = "restyle",
            args = list("type", "box"),
            label = "Box Plot"
          ),
          list(
            method = "restyle",
            args = list("type", "bar"),
            label = "Bar Plot"
          )
        )
      )
    )
  )
 
fig

enter image description here

希望这有帮助!

© www.soinside.com 2019 - 2024. All rights reserved.