r - 在闪亮的仪表板R中动态绘制多个变量
问题描述
我正在尝试构建一个闪亮的仪表板,向用户显示多个国家和多个变量随时间变化的数据图。仪表板工作得很好,但我真的很感激能帮助我解决我一直遇到的问题:
我目前能够为单个变量绘制多个国家(例如,在某个时间段内,美国和 AFG 的 Variable_1)
但是,我目前无法为多个变量绘制多个国家(例如,一段时间内美国的 Variable_1 和 Variable_2 或一段时间内的美国和 AFG 的 Variable_1 和 Variable_2)。当我尝试选择多个变量时,我收到错误“解析了多个表达式”。
我非常感谢一种无需硬编码即可生成多个变量的 ggplots 的方法,因为实际数据包含更多变量和更多国家。如果有人能指出我正确的方向或提供解决方案,那就太好了。下面提供了一个最小的工作示例。
感谢任何能够提供帮助的人!:)
library(shiny)
library(dplyr)
library(tidyverse)
df = data.frame("Country" = c("USA","USA","USA","USA","AFG","AFG","AFG","AFG"),
"Year" = c(2000,2001,2002,2003,2000,2001,2002,2003),
"Variable_1" = c(10,12,14,16,10,11,12,13),
"Variable_2" = c(20,19,18,17,20,21,22,23),
"Variable_3" = c(13,13,14,14,16,16,12,12))
#df_long <- melt(df, id=c("Country","Year"))
ui = fluidPage(
titlePanel("My Dashboard"),
pickerInput("myvariable","Pick variables", choices =c("Variable_1","Variable_2","Variable_3"),
options =list("actions-box" = TRUE),
multiple=TRUE,
selected = "Variable_1"),
sliderInput("year_selector", "Select Year Range",min = 2000,max = 2003,value = c(2000, 2013)),
pickerInput("choicePicker","Pick countries",choices =c("USA", "AFG"),
options =list("actions-box" = TRUE),
multiple=TRUE,
selected="USA"),
plotOutput("trend")
)
server = function(input, output, session){
selected <- reactive(filter(df, Country %in% input$choicePicker, Year>=input$year_selector[1], Year<=input$year_selector[2]))
output$trend = renderPlot({
ggplot(selected(), aes_string(x="Year", y=input$myvariable, color="Country", group="Country")) +
geom_line(size = 2) +
scale_x_continuous(breaks = pretty_breaks()) +
labs(x = "",
y = paste0(input$myvariable),
title = paste(" ,", " "),
caption = paste(" ", " ")) +
theme(legend.position = c(0.8, 0.8))
})
}
shinyApp(ui=ui, server=server)
解决方案
您可以使用构面来绘制多个国家,并为多个变量使用颜色。用长格式数据绘制会更容易。
df_long <- tidyr::pivot_longer(df, starts_with('Variable'))
ui = fluidPage(
titlePanel("My Dashboard"),
pickerInput("myvariable","Pick variables", choices =c("Variable_1","Variable_2","Variable_3"),
options =list("actions-box" = TRUE),
multiple=TRUE,
selected = "Variable_1"),
sliderInput("year_selector", "Select Year Range",min = 2000,max = 2003,value = c(2000, 2013)),
pickerInput("choicePicker","Pick countries",choices =c("USA", "AFG"),
options =list("actions-box" = TRUE),
multiple=TRUE,
selected="USA"),
plotOutput("trend")
)
server = function(input, output, session){
selected <- reactive(filter(df_long, Country %in% input$choicePicker,
Year>=input$year_selector[1],
Year<=input$year_selector[2],
name %in% input$myvariable))
output$trend = renderPlot({
ggplot(selected(), aes(Year, y=value, color=name, group=name)) +
geom_line(size = 2) +
scale_x_continuous(breaks = pretty_breaks()) +
labs(x = "",
y = 'value',
title = paste(" ,", " "),
caption = paste(" ", " ")) +
theme(legend.position = c(0.8, 0.8)) +
facet_wrap(~Country)
})
}
shinyApp(ui=ui, server=server)
推荐阅读
- spring - 使用将抽象类扩展为@RequestBody 的类是否被认为是一种好习惯?
- android - 以 Activity 为入口点的库 API - 如何向主模块提供结果?
- html - 导航栏中的 Google 自定义搜索引擎搜索栏
- unix - 有没有办法在尝试复制到 Unix 服务器之前检查 MVS 中的文件是否存在
- javascript - (Nuxt)页面不会在重新加载时重新呈现数据
- python - 如何将 Django 中生成的 PDF 文件保存到我的项目中的指定目录?
- angular - 在 Angular 应用程序中进行身份验证的可能性
- google-bigquery - 使用 Regexp_contains
- java - 是否存在本地接口类?
- javascript - Javascript mm:ss 格式的倒数计时器,文本输入受限