R / Shiny / ggplot2:checkboxGroup用于绘制特定数据

时间:2016-05-20 09:45:58

标签: r ggplot2 shiny

从我上一个post构建我想使用selectInput来允许用户绘制他们想要绘制的区域。

我的数据如下:

Year    Ratio   Region
1983 Q1 2.9 Northern
1983 Q2 3   Northern
1983 Q3 3.1 Northern
1983 Q4 3   Northern
...
2015 Q2 5.1 UK
2015 Q3 5.1 UK
2015 Q4 5.2 UK
2016 Q1 5.2 UK

server.R代码段

houseratio <- read.csv("houseratio.csv", stringsAsFactors = FALSE)  

output$housePlot <- renderPlot({
ggplot(data=houseratio[,input$region_choose], aes(x=Year, y=Ratio, group=Region, colour=Region)) +
  geom_line() +
  geom_point()
})

ui.r摘录

checkboxGroupInput("region_choose", label = "Choose a region",
          choices = c("The North"="Northern", "Yorkshire & Humber" = "Yorks & H",
                      "North West"="NW","East Midlands"="East Mids", 
                      "West Midlands"="West Mids", "East Anglia"="East Anglia",
                      "Outer South East"="Outer SE", "Outer Met"="Outer Met", 
                      "London"="London", "South West"="SW", "Wales"="Wales",
                      "Northern Ireland"="NI", "UK"="UK")
          ),

  plotOutput("housePlot")
)

This postthis post有点帮助,但因为我的数据格式很长,所以它似乎不起作用(也因为它们是selectInput而是weh)。

如果我错过了任何重要的事情,那么任何帮助都会受到赞赏 - 抱歉,这是什么?

1 个答案:

答案 0 :(得分:1)

1)我认为您在过滤器尝试中遇到问题

data=houseratio[houseratio$region%in%input$region_choose,]

2)更好的分离问题2:数据处理和绘图见例子

library(shiny)
library(ggplot2)
ui=shinyUI(fluidPage(checkboxGroupInput("region_choose", label = "Choose a region",
                              choices = c("setosa","versicolor","virginica")
),

plotOutput("housePlot")
))

server=function(input,output){
  #data manipulation
  data_1=reactive({
    return(iris[iris$Species%in%input$region_choose,])
  })
  #plot
  output$housePlot <- renderPlot({
    ggplot(data=data_1(), aes(x=Sepal.Length, y=Petal.Width, group=Species, colour=Species)) +
      geom_line() +
      geom_point()
  })
}

shinyApp(ui,server)