Using ggvis to show longitudinal data, where a slider controls the year

前端 未结 2 1938
悲哀的现实
悲哀的现实 2021-02-06 16:18

I\'m trying to use a slider to control year in a longitudinal spatial data set, essentially a set of scatter plots. I can\'t figure out how to assign the slider to this variable

2条回答
  •  梦谈多话
    2021-02-06 16:55

    The answers above are great. Definitively worth study. This is what I came up with for the original question for a quick fix.

    Global.R:

    
        library(shiny)
        library(ggvis)
    
            data<-data.frame(year=rep(2000:2002, each=23), x=rnorm(23*3,10), y=rnorm(23*3,10),
                            count=c(rnorm(23,2),rnorm(23,4),rnorm(23,6))) 
    
    

    ui.R:

    
         shinyUI(bootstrapPage(
             h3("Ploting Diferent Years Using a Slider",align="center"),
             br(),
             fluidRow(column(4,ggvisOutput("yearPlot"),offset=3)),
             fluidRow(column(3,sliderInput("YearSelect", "Year:     ",min=2000,max=2002,step=1,value=2000),offset=5))
    ))
    

    Server.R:

    
    
        shinyServer(function(input, output,session) {
    
        plotdata <- reactive({
            chosendat <- data[data$year==input$YearSelect, ]
            names(chosendat) <- c("year","xvar","yvar","count")
            return(chosendat)
          })
    
        vis1% ggvis(~xvar, ~yvar, size=~count) %>% layer_points() 
    
        })
    
        vis1 %>% bind_shiny("yearPlot")
    
        })
    

提交回复
热议问题