R / Shiny / ggplot2:checkboxGroup绘制特定数据 [英] R/Shiny/ggplot2: checkboxGroup to plot specific data

查看:274
本文介绍了R / Shiny / ggplot2:checkboxGroup绘制特定数据的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

从我上一次发布构建我希望使用selectInput来允许用户绘制尽可能多的区域。



我的数据如下所示:

 年份比率地区
1983年一季度2.9北部
1983年第二季度3北部
1983年第三季度3.1北部
1983年第四季度3北部
...
2015 Q2 5.1英国
2015 Q3 5.1英国
2015 Q4 5.2英国
2016 Q1 5.2英国

server.R snippet

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

输出$ housePlot< - renderPlot({
ggplot(data = houseratio [,input $ region_choose],aes(x = Year ,y =比率,group = Region,color = Region))+
geom_line()+
geom_point()
})

ui.r片段

  checkboxGroupInput(region_choose,label =选择一个区域n,
选择= c(北方=北方,约克郡& Humber=Yorks& H,
North West=NW,East Midlands=East Mids,
West Midlands=West Mids,East Anglia=East Anglia,
外部东南部=外部东部,外部大气=外部大气,
伦敦=伦敦,西南部=西南部,威尔士= 威尔士,
北爱尔兰=NI,英国=英国)
),

plotOutput(housePlot)

这篇文章这篇文章有点帮助,但作为我的数据是长格式的,它似乎没有工作(也因为他们是selectInput,但我们)。



如果我错过了任何帮助,将不胜感激什么是关键 - 对不起,这是什么?

解决方案

<1>我认为你的问题在f ilter try

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



<2>更好地将问题分解为2:数据操作和绘图见例

<$ p $
library(ggplot2)
ui = shinyUI(fluidPage(checkboxGroupInput(region_choose,label =选择区域,
choices = c(setosa,versicolor,virginica)
),

plotOutput(housePlot)
))

server =函数(输入,输出){
#data操作
data_1 =反应({
return(虹膜[虹膜$物种%在%输入$ region_choose,])
})
#plot
output $ housePlot< - renderPlot({
ggplot(data = data_1(),aes(x = Sepal.Length,y = Petal.Width,group = Species,color = Species))+
geom_line()+
geom_point()
})
}

shinyApp(ui,server)


Building from my last post I want to use selectInput to allow users to plot as many regions as they wanna plot.

My data looks like this:

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 snippet

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 snippet

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 post and this post kinda helped but as my data is in long format it didn't seem to work (also because they're selectInput but weh).

Any help would be appreciated, if I've missed anything crucial- sorry, what is it?

解决方案

1) I think your have problem in filter try

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

2) better devide problem into 2 : data manipulation and plot see example

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)

这篇关于R / Shiny / ggplot2:checkboxGroup绘制特定数据的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆