简体   繁体   English

动态 plot shiny 仪表板中的多个变量 R

[英]dynamically plot multiple variables in shiny dashboard R

I am trying to build a shiny dashboard that shows the user a plot of data over time for multiple countries and multiple variables.我正在尝试构建一个 shiny 仪表板,该仪表板向用户显示多个国家和多个变量的 plot 数据。 The dashboard works pretty well, but I would really appreciate some help in overcoming a problem I've been having:仪表板工作得很好,但我真的很感激能帮助我解决我一直遇到的问题:

  • I am currently able to plot multiple countries for a single variable (eg Variable_1 for USA and AFG over some time period)我目前能够为单个变量 plot 多个国家/地区(例如,在一段时间内,美国和 AFG 的 Variable_1)

  • However, I am currently unable to plot multiple countries for multiple variables (eg Variable_1 and Variable_2 for USA over some time period OR Variable_1 and Variable_2 for USA and AFG over some time period).但是,我目前无法plot 多个国家/地区的多个变量(例如,一段时间内美国的 Variable_1 和 Variable_2 或一段时间内的美国和 AFG 的 Variable_1 和 Variable_2)。 When I try and select multiple variables, I receive the error "more than one expression parsed".当我尝试 select 多个变量时,我收到错误“解析了多个表达式”。

I would very much appreciate a method of being able to generate ggplots of multiple variables without hard coding since the actual data contains many more variables and many more countries.我非常感谢一种无需硬编码即可生成多个变量的 ggplots 的方法,因为实际数据包含更多变量和更多国家。 If anyone could point me in the right direction or provide a solution, that would be fantastic.如果有人能指出我正确的方向或提供解决方案,那就太好了。 A minimal working example is provided below.下面提供了一个最小的工作示例。

Thankyou to anyone who is able to help: :)感谢任何能够提供帮助的人::)

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)

You may make use of facets to plot multiple countries and for multiple variables use colors.您可以在多个国家/地区使用 plot 的方面,对于多个变量,请使用 colors。 It will be easier to plot with data in long format.使用长格式数据 plot 会更容易。

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)

在此处输入图像描述

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM