繁体   English   中英

如何在 Shiny 中将列更改为 plot 和 geom_bar()?

[英]How can I change a column to plot a geom_bar() in Shiny?

我需要创建一个selectInput到 select 一列到 plot 一个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

好的,刚找到。

只需要将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), ) 

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM