gpt4 book ai didi

r - 在 Shiny 的仪表板 R 中动态绘制多个变量

转载 作者:行者123 更新时间:2023-12-04 14:56:36 27 4
gpt4 key购买 nike

我正在尝试构建一个 Shiny 的仪表板,向用户显示多个国家和多个变量随时间变化的数据图。仪表板工作得很好,但我真的很感激能帮助我克服我一直遇到的问题:

  • 我目前能够为一个变量绘制多个国家(例如,一段时间内美国和 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)

enter image description here

关于r - 在 Shiny 的仪表板 R 中动态绘制多个变量,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/67855470/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com