【问题标题】:r - Sorting of Barts-ggplot2 in shiny using reactiver - 使用反应对 Barts-ggplot2 进行排序
【发布时间】:2020-10-11 11:01:21
【问题描述】:

请在下面找到使用 ggplot2 的闪亮应用程序的代码,我不知道如何在 server.R 代码中对它们进行排序

使用下面的代码,我可以显示条形图并对其进行更改,但排序数据似乎是一个问题。

ui.R

ui <- fluidPage(
  titlePanel("Perdas no Gefin"),
  theme = shinythemes::shinytheme('yeti'),
    sidebarLayout(
    sidebarPanel(selectInput('mes', 'Selecione o mês', unique(month(roi$Data_Contab))),
      mainPanel(
        tabsetPanel(
          tabPanel('Word', plotly::plotlyOutput('contagem'))
                    ))
  )
)

服务器.R

server <- function(input, output, session){
  rcontagem <- reactive({
    roi %>%
      filter(month(Data_Contab) == input$mes) %>%
      unnest_tokens(word, Parecer) %>%
      anti_join(stop_words2) %>%
      count(word) %>%
      na.omit() %>%
      top_n(30) %>%
      arrange(desc(n))
      })
  
  output$contagem <- plotly::renderPlotly({
    rcontagem()%>%
    ggplot(aes(x = word, y = n)) +
    geom_col()  +
    # Flip the plot coordinates
    coord_flip() +
    ggtitle("Contagem de palavras")
  
  })
  }

shinyApp(ui = ui, server = server)

无序绘图:

我已经尝试过这个:Sorting of Bars-ggplot2 in shiny,但它不起作用,可能是因为我使用的是响应式。

【问题讨论】:

  • 你的 y 轴(翻转前的 x 轴)是按 word 的值排序的,对吧? (并且 word 的值似乎已被剪裁。)您需要按顺序显示它们。因此,在您的 rcontagem 反应式中添加一个索引变量。像%&gt;% mutate(Row=as.factor(row_number())) 这样的东西在管道的末端。然后在renderPlotly 图中使用Row 作为x 变量。要恢复轴标签,请使用 scale_x_discrete 为因子提供标签。我不能为你做这件事,因为你没有给我们原始数据。

标签: r ggplot2 shiny reactive


【解决方案1】:

你可以在你的服务器端尝试这样的事情:

server <- function(input, output, session){
  rcontagem <- reactive({
    roi %>%
      filter(month(Data_Contab) == input$mes) %>%
      unnest_tokens(word, Parecer) %>%
      anti_join(stop_words2) %>%
      count(word) %>%
      na.omit() %>%
      top_n(30) %>%
      arrange(desc(n))
      })
  
  output$contagem <- plotly::renderPlotly({
    rcontagem()%>%
    ggplot(aes(x = reorder(word,n), y = n)) +
    geom_col()  +
    # Flip the plot coordinates
    coord_flip() +
    ggtitle("Contagem de palavras")
  
  })
  }

【讨论】:

    猜你喜欢
    • 2019-06-25
    • 2011-04-14
    • 2021-11-25
    • 2022-12-29
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多