Lana Meijinhos
Lana Meijinhos

Reputation: 185

Create dropdown list in R using plotly?

I want to create a dropdown list with the years 2019 to 2024 for this plot:

enter image description here

I'm using R Markdown and plotly to create the interactive plot. I managed to make this plot for 2021, but I can't seem to add the dropdown list to filter the plot by year.

The code I'm using:

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

I had the ggplot code and with some chatgpt help I got to make it into an interactive plot.

Upvotes: 1

Views: 139

Answers (1)

Ifeanyi Idiaye
Ifeanyi Idiaye

Reputation: 1114

I do not think you can create a plotly dropdown menu with ggplotly(). Instead, you can achieve this using the Plotly library itself. Here is an example:

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

Hope this helps!

Upvotes: 0

Related Questions