【问题标题】:How do I order data in a lollipop graph in Shiny based on user inputs?如何根据用户输入在 Shiny 的棒棒糖图中排序数据?
【发布时间】:2018-08-11 09:51:00
【问题描述】:

我正在 Shiny 中创建一个简单的应用程序来了解它是如何工作的。

我有一个棒棒糖图表,它会根据用户输入而变化。

图表按国家 z-a 排序。

我希望能够按照用户选择的数据(输入 ID = dta)从顶部最大到底部最小进行排序。

我可以在普通 R 中做到这一点,例如:

data %>%
arrange(Market_size) %>% 
mutate(Country = factor(Country, levels = Country)) %>% 
ggplot(aes(y = Market_size, x = Country)) +
geom_lollipop(aes(color = Region), point.size = 5) +
theme(panel.background = element_blank(), axis.text.x = element_text(size = 
13), axis.text.y = element_text(size = 13), 
legend.text=element_text(size=13), legend.title=element_blank()) +
coord_flip() +
xlab("") +
ylab("") +
guides(colour = guide_legend(override.aes = list(size = 4)))

但我似乎无法将其翻译成 Shiny。

我对这部分 Shiny 的代码是:

# LIBRARIES AND DATA LOAD

library(shiny)
library(ggplot2)
library(readxl)
library(dplyr)
library(ggalt)
library(shinythemes)
library(DT)
library(plotly)

data <- read_excel("data.xlsx")

# USER INTERFACE

ui <- fluidPage(theme = shinytheme("flatly"),

navbarPage(title = "Database",

# HOME TAB

tabPanel("Home",

verticalLayout(

p(strong("Welcome to the database, select above the tabs to navigate."))
)
), 

# SINGLE VARIABLE TAB

tabPanel("Single variable",

sidebarLayout(      

sidebarPanel(

selectInput(inputId = "cntry",
label = "Choose countries:", 
choices= data$Country,
multiple = TRUE),

selectInput(inputId = "rgns",
label = "Choose regions:", 
choices= data$Region,
multiple = TRUE),

selectInput(inputId = "dta",
label = "Choose data:", 
choices= names(data[3:6]),
multiple = FALSE)
),

mainPanel(
plotOutput(outputId = "plot")
)
)
)
)
)

# SERVER

server <- function(input, output) {

# DATA FOR SINGLE VARIABLE TAB AND SINGLE VARIABLE DATA TABLE  

data_subset <- reactive({
filter(data, Country %in% input$cntry | Region %in% input$rgns) %>% 
select(Country, Region, input$dta)
})

# PLOT FOR SINGLE VARIABLE TAB  

output$plot <- renderPlot({
ggplot(data = data_subset(), aes_string(y = input$dta, x = "Country")) +
geom_lollipop(aes(color = Region), point.size = 5) +
theme(panel.background = element_blank(), axis.text.x = element_text(size = 
13), axis.text.y = element_text(size = 13), 
legend.text=element_text(size=13), legend.title=element_blank()) +
coord_flip() +
xlab("") +
ylab("") +
guides(colour = guide_legend(override.aes = list(size = 4)))
})
}

# RUN THE APP

shinyApp (ui = ui, server = server) 

【问题讨论】:

  • 如果你能提供玩具数据会很有帮助。
  • 是的,我的数据是虚构的,因为这只是为了练习,您在下面的回答通过我将在下面写的附加部分解决了这个问题。对于其他人,如果他们偶然发现了这个,我的数据看起来像:
  • 国家、地区、Market_size、No_smokers_18_plus、Daily_users、Closed_system
  • 阿富汗,亚洲,784000, 200000, 25000, 0.10

标签: r ggplot2 shiny


【解决方案1】:

试试:

data_subset <- reactive({
filter(data, Country %in% input$cntry | Region %in% input$rgns) %>% 
select(Country, Region, input$dta) %>%
mutate(y_var = .[[input$dta]]
})

然后在您的 ggplot 调用中,将您的第一行更改为:

ggplot(data = data_subset(), aes(y = y_var, x = Country))

【讨论】:

  • 非常感谢你,一旦我在第一位添加了两行,这就会起作用: mutate(y_var = .[[input$dta]]) %>%arrange(y_var) %> % mutate(Country = factor(Country, levels = Country))
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2014-12-15
  • 1970-01-01
  • 2016-12-26
相关资源
最近更新 更多