我已成功放入年度滑块并创建了一个ggplot。但是,我很难弄清楚如何按照年份过滤数据(目前每年都会显示),如滑块所示。
如何获取数据:
library(WDI)
xdat <- WDI(indicator=c('NY.GNP.PCAP.CD', 'SH.DYN.MORT', 'SP.POP.TOTL', 'SP.DYN.LE00.IN', 'SP.DYN.TFRT.IN'), start=1960, end=2014, extra=TRUE)
sdata <- subset(xdat, xdata$region != "Aggregates")
sdata <- na.omit(sdata)
sdata$reg <- ifelse(sdata$region == "Europe & Central Asia (all income levels)", 1, ifelse(sdata$region == "Middle East & North Africa (all income levels)", 2, ifelse(sdata$region == "South Asia", 3, ifelse(sdata$region=="Latin America & Caribbean (all income levels)", 4, ifelse(sdata$region=="Sub-Saharan Africa (all income levels)", 5, ifelse(sdata$region=="East Asia & Pacific (all income levels)", 6, ifelse(sdata$region=="North America", 7, 0)))))))
sdata$col <- ifelse(sdata$reg == 1, "#FFFFFF", ifelse(sdata$reg == 2, "yellow", ifelse(sdata$reg == 3, "red", ifelse(sdata$reg== 4, "green", ifelse(sdata$reg== 5, "orange", ifelse(sdata$reg==6, "lavender", ifelse(sdata$reg== 7, "pink", 0)))))))
ui.r
library(googleCharts)
shinyUI(fluidPage(
plotOutput("view"),
titlePanel("title panel"),
column(
sliderInput("slider1", label=h3("Slider"),
min=1960, max=2014, value=50, sep="", animate=TRUE),
),
fluidRow(
column(
radioButtons("radio", label=h3("Radio buttons"),
choices = list("Mortality" = 1, "Choice 2" = 2, "Choice 3" = 3), selected=1)),
sidebarLayout(
sidebarPanel(""),
mainPanel("Main")
))
))
server.r
library(dplyr)
shinyServer(function(input, output) {
output$view <- renderPlot({
ggplot(sdata, aes(x=NY.GNP.PCAP.CD, y = SP.DYN.LE00.IN)) + geom_point(aes(size=pop^(0.75)/20000000+2, colour=col))
})
})
问题是每年都会出现,当我使用滑块时,它什么都不做:
我也想知道如何使用单选按钮来更改变量。
答案 0 :(得分:0)
ui.r对我不起作用(编辑:更改了一些标签),但我猜是这样的:
ui.r
library(googleCharts)
shinyUI(fluidPage(
plotOutput("view"),
titlePanel("title panel"),
column(4,
sliderInput("slider1", label=h3("Slider"),
min=1960, max=2014, value=50, sep="", animate=TRUE)
),
fluidRow(
column(2,
radioButtons("radio", label=h3("Radio buttons"),
choices = list("Mortality" = 1, "Choice 2" = 2, "Choice 3" = 3), selected=1)),
sidebarLayout(
sidebarPanel(""),
mainPanel("Main")
))
))
和服务器
library(dplyr)
shinyServer(function(input, output) {
output$view <- renderPlot({
temp = sdata[sdata$year>(input$slider1)&sdata$year<2014,]
ggplot(temp, aes(x=NY.GNP.PCAP.CD, y = SP.DYN.LE00.IN)) + geom_point(aes(size=temp$SP.POP.TOTL^(0.75)/20000000+2, colour=col))
})
})