使用Shiny滑块控制ggplot上的输出

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了使用Shiny滑块控制ggplot上的输出相关的知识,希望对你有一定的参考价值。

我是R和Shiny的完全新手。

我一直在努力开发一种探索经济数据的工具。我正在使用GDP指数数据。到目前为止,该工具允许您加载不同的扇区,但我想控制多年。这就是GDP Tool目前的样子。

我想使用滑块来控制它们在绘图上出现的年份。

这是我到目前为止的代码:

library(shiny)
library(ggplot2)
GDPData = read.csv("Data/GDP_Data_New.csv", stringsAsFactors = FALSE)

ui <- fluidPage(
  sidebarPanel(
    tags$h1("Scottish GDP by Sector")

  ),
  mainPanel(
    tabsetPanel(
      tabPanel("Broad Sectors", 
               plotOutput("Highlevel"),
               sliderInput("SectorTime", "Select a time period", min = 1998, max = 2016,
                           value = c(1998,2016)),
               checkboxGroupInput("Indicator","Select an indicator:",   
                                  c("Total" = "Total ", "Production" = "Production", "Construction" = "Construction","Services" = "Services", "Agriculture, forestry and fishing" = "AFF"),
                                  selected = "Total ")),
      tabPanel("Production", 
               plotOutput("Production"),
               checkboxGroupInput("ProductionIndicator","Select an indicator:",   
                                  c("Total" = "Production", "Manufacturing" = "Manufacturing", "Mining and Quarrying" = "Mning","Electricity & Gas Supply" = "Electricty", "Water Supply & Waste Management" = "WaterWaste"),
                                  selected = "Production")),
      tabPanel("Services",
               plotOutput("Services"),
               checkboxGroupInput("ServicesIndicator","Select an indicator:",   
                                  c("Total" = "Services", "Distribution, Hotels and Catering" = "Distribution","Transport, Storage and Communication" = "Transport", "Business Services and Finance" = "Professional","Government, and Other Services" = "Public"),
                                  selected = "Services"))
               )

    )
)

server <- function(input, output, session) {

  SectorsData=reactive({
    return(GDPData[GDPData$Sector%in%input$Indicator,])
    })
  ProductionData=reactive({
    return(GDPData[GDPData$Sector%in%input$ProductionIndicator,])
  })
  ServicesData=reactive({
    return(GDPData[GDPData$Sector%in%input$ServicesIndicator,])
  })

  output$Highlevel <- renderPlot(ggplot(SectorsData(), aes(x=Year, y=Index, group=Sector, colour=Sector)) + geom_line()+geom_point()+theme_minimal())
  output$Production <- renderPlot(ggplot(ProductionData(), aes(x=Year, y=Index, group=Sector, colour=Sector)) + geom_line()+geom_point()+theme_minimal())
  output$Services <- renderPlot(ggplot(ServicesData(), aes(x=Year, y=Index, group=Sector, colour=Sector)) + geom_line()+geom_point()+theme_minimal())
}
shinyApp(ui = ui, server = server)

CSV文件有三列“扇区”,“年份”,“索引”

任何帮助将不胜感激。

答案

这是您需要做的示例:

library(shiny)

ui <- fluidPage(
sliderInput(inputId = "year", label = "YEAR", min = 1900, max = 2000, value = c(1920, 1980), step = 5)
,
plotOutput("plot")
)


server <- function(input, output, session) {
  main_data <- reactive({
    data.frame(a= 1900:2000, b=rnorm(101) )
  })

  output$plot <- renderPlot({
    df <- main_data()
   df <-  df[df$a>input$year[1] & df$a<input$year[2],]
    plot(df$a, df$b, type = "l")
  })

}
shinyApp(ui = ui, server = server)

以上是关于使用Shiny滑块控制ggplot上的输出的主要内容,如果未能解决你的问题,请参考以下文章

R和Shiny:当悬停在Shiny上的绘图上时显示ggplot2数据值

为啥这个简单的 R Shiny 输出不使用 ggplot 绘图?

对数刻度上的闪亮滑块

R闪亮:如何将滑块和单选按钮链接到ggplot

根据无功输出值设置最大滑块输入值

将 ggplot 气泡图导入 Shiny 时出错