【发布时间】: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反应式中添加一个索引变量。像%>% mutate(Row=as.factor(row_number()))这样的东西在管道的末端。然后在renderPlotly图中使用Row作为x 变量。要恢复轴标签,请使用scale_x_discrete为因子提供标签。我不能为你做这件事,因为你没有给我们原始数据。