首页 > 解决方案 > 在闪亮的仪表板R中动态绘制多个变量

问题描述

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

我非常感谢一种无需硬编码即可生成多个变量的 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)

标签: rggplot2shinyshinydashboard

解决方案


您可以使用构面来绘制多个国家,并为多个变量使用颜色。用长格式数据绘制会更容易。

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)

在此处输入图像描述


推荐阅读