Search code examples
rggplot2shinyfacet

problems with aesthetic length geom_line shiny


I have data you can generate with:

set.seed(3)
dat.0<-as.data.frame(expand.grid(c("MATH","CHEM","BIOL"),c("Spring","Summer","Fall"),c(2016,2017,2018),0:17))
names(dat.0)<-c("SUBJ","SEM","YEAR","WEEK")
dat.0$MEET<-floor(runif(486,0,150))

I'm trying to make a shiny app that will display a line plot of the "MEET" series with "WEEK" on the x-axis colored by the "SUBJ" field.

outside of shiny the plot looks fine:

dat.0%>%
  ggplot(aes(x = WEEK,y = MEET,colour = SUBJ))+
  geom_point()+
  geom_path()+
  facet_grid(YEAR~SEM)

but when I use shiny to create the app, it spits out an error if I try to plot more than one series on the plot. What's more if you plot each of the series individually inside the app, you realize that no matter which "SUBJ" you select, it prints the same plot:

choices = levels(as.factor(dat.0$SUBJ))
# Define UI for application that draws a histogram
ui <- fluidPage(

  # Application title
  titlePanel("STEM Tutoring App"),

  # Sidebar with a slider input for number of bins 
  sidebarLayout(
    sidebarPanel(
      selectInput("SUBJ",
                  "Course Subject:",
                  choices = choices,
                  multiple = T,
                  selectize = T)
    ),

    # Show a plot of the generated distribution
    mainPanel(
      plotOutput("linePlot")
    )
  )
)

server <- function(input, output) {

  output$linePlot <- renderPlot({
    req(input$SUBJ)
    dat.0%>%
      ggplot(aes(x = WEEK,y = MEET,colour = input$SUBJ))+
      geom_point()+
      geom_path()+
      facet_grid(YEAR~SEM)})}
# Run the application 
shinyApp(ui = ui, server = server)

I know this is almost there, I just don't know how to make it looks good and functions the way you would expect.


Solution

  • The trick to this, as user @aosmith explains in the comments is that You need to make the data set react to user inputs by defining the subsetting parameters after the sidebar, within the renderPlot function, so you would put

    server <- function(input,output) {
    
       output$linePlot<-renderPlot({
           req(input$SUBJ)
    
           plot.data <- dat.0 %>%
               dplyr::filter(SUBJ %in% input$SUBJ)
    
           plot.data %>%
               ggplot(aes(x = WEEK, y = MEET, colour = SUBJ)) +
               geom_point() +
               geom_path() +
               facet_grid(YEAR~SEM)
        })
    }
    

    subsetting based on user input was where I got confused. problem solved, though. thank you for the help!