【问题标题】:How can I change a column to plot a geom_bar() in Shiny?如何更改列以在 Shiny 中绘制 geom_bar()?
【发布时间】:2021-02-06 22:45:46
【问题描述】:

我需要创建一个selectInput 来选择一列来绘制geom_bar()

我一直在尝试的:

用户界面

      tabItems(
        tabItem("Coop_ativas", 
                fluidPage(h1("Cooperativas Brasileiras")),
                dataTableOutput("cooptable"), 
                box(plotOutput("correlation_plot"), widht = 8), 
                box(selectInput("features", "Características:", 
                                c("situacao_cadastral", "identificador_matriz_filial")), width = 4),
                box(plotOutput("regiao_plot"), widht = 8), 
                box(selectInput("regiao", "Região:", 
                                choices = list("Estado" = "uf", "Região" = "regiao"),
                                selected = "Estado"))
                ))


服务器

 output$regiao_plot <- renderPlot({
      

      Coop_ativas %>% select(input$regiao[1]) %>% group_by(input$regiao[1]) %>% count() %>% arrange(desc(n)) %>% head(10) %>% 
                      ggplot(aes(reorder(input$regiao[1], n), n), ) + 
                      geom_bar(stat="identity", fill="steelblue") +
                      geom_text(aes(label=n), vjust=0.5, hjust=-0.5, color="darkgrey", size=3) +
                      labs(title = "Cooperativas Ativas por Estado",
                           subtitle = "02/2020",
                           caption = "Fonte: RFB, tratado por OBSCOOP/USP",
                           #tag = "Figure 1",
                           x = "Estado",
                           y = "Quantidade") +
                      theme_minimal() + theme(plot.title = element_text(hjust = 0.5),
                                              plot.subtitle = element_text(hjust = 0.5),
                                              plot.caption = element_text(0.0)) +         
                      coord_flip() 
  
    
  })

input$regiao[1] 在哪里,我希望它是 ufregiao

【问题讨论】:

    标签: r ggplot2 shiny


    【解决方案1】:

    好的,刚刚找到了。

    只需要将input$regiao 转换为sym。 并与!! 一起使用。

      output$regiao_plot <- renderPlot({
          
          col <- sym(input$regiao)
    
          Coop_ativas %>% select(!! col) %>% group_by(!! col) %>% count() %>% arrange(desc(n)) %>% head(10) %>% 
                          ggplot(aes(reorder(!! col, n), n), ) 
    
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2016-02-02
      • 2020-07-10
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2015-07-22
      相关资源
      最近更新 更多