Search code examples
rggplot2shinyshiny-server

qqplot in Shiny Apps changes axis and problems with a conditional plot


I have some problems, implementing ggplot2 into shiny apps

  1. I cannot get the code working in the example I give below. For some reason when I use aes_string it does not find factor1 and when I use aes, it does not find params(). In the file, fed with my data, this step works. Sorry for that.

  2. I want to use qqplot2 to plot the data of my project. When I take the data and plot them outside of shiny apps, everything is fine. When I implement the code in shiny apps it changes my axis from a factorial one to a contentious one.

  3. I am changing the factors on the x-axis according to the user input with with "radioButtons". When I use stripchart(), it works perfectly. When I use ggplot instead, it works fine in the first place, but when I change the the factor (change the value of the radioButton) it does not find the input object (input$strain).

Sorry for the long post. Your help is very much appreciated.

Here is my code:

ui.R

library(shiny)
# Define UI for dataset viewer application
ui<-(fluidPage(
  titlePanel("My project"),
  sidebarLayout(
    sidebarPanel(
      selectInput("dataset", "Choose a dataset:", 
                  choices = c("exp1","exp2", "exp3"),selected="exp1"),
      selectInput(inputId = "param",choices = c("Repsonse A","Response B"),label = "Choose repsonse",selected="Repsonse A"),
      selectInput("factors", "Choose grouping factor:", 
                  choices = c("No grouping","Block"),selected="No grouping"),
      radioButtons(inputId = "fac",choice=c("factor1 on x-axis","factor2 on x-axis"), "Choose type of x-axis")
      ),
    mainPanel(
      h3(textOutput("caption", container = span)),
      plotOutput("strip")
     )
  )
))

and server.R

library(shiny)
library(datasets)
library(ggplot2)
server<-(function(input, output,clientData, session) {
  df<-data.frame("respondA"=runif(20, min=0,    max=0.4),"respondB"=runif(20, min=0,    max=0.4),"factor1"="A","factor2"="B","replicate"=as.factor(c(1:20)))
  df2<-data.frame("respondA"=runif(20, min=0,    max=0.4),"respondB"=runif(20, min=0,    max=0.4),"factor1"="A","factor2"="B","replicate"=as.factor(c(1:20)))
  df3<-data.frame("respondA"=runif(20, min=0,    max=0.4),"respondB"=runif(20, min=0,    max=0.4),"factor1"="A","factor2"="B","replicate"=as.factor(c(1:20)))

  datasetInput <- reactive({
    switch(input$dataset,
           "exp1" = df,
           "exp2" = df2,
           "exp3" = df3)
  })
  params <- reactive({
    switch(input$param,
           "Repsonse A" = datasetInput()$respondA,
           "Response B" = datasetInput()$respondB)
  })
  factor <- reactive({
    switch(input$factors,
           "No grouping"=NULL,
           "Block" = "replicate")
  })

  output$strip <- renderPlot({
    if (input$fac == "factor1 on x-axis") {
      p<-ggplot(datasetInput(), aes_string(x = factor1, y = params())) + 
        ylab("Mylabel1")+
        xlab("Mylabel2")+
        geom_jitter(position = position_jitter(width = .2),shape=19,size=5,aes_string(colour = factor()))+
        labs(title = "Mytitle")+
        theme_bw() +
        theme(axis.line = element_line(colour = "black"),
              panel.grid.major = element_blank(),
              panel.grid.minor = element_blank(),
              panel.border = element_blank(),
              panel.background = element_blank()) 
      print(p)
    } else if(input$fac == "factor2 on x-axis") {
      q<-ggplot(datasetInput(), aes_string(x = factor2, y = params())) + 
        ylab("Mylabel1")+
        xlab("Mylabel2")+
        geom_jitter(position = position_jitter(width = .2),shape=19,size=5,aes_string(colour = factor()))+
        labs(title = "Mytitle")+
        theme_bw() +
        theme(axis.line = element_line(colour = "black"),
              panel.grid.major = element_blank(),
              panel.grid.minor = element_blank(),
              panel.border = element_blank(),
              panel.background = element_blank()) 
      print(q)
    }
  }) 

})
shinyApp(ui=ui,server=server)

Ps: I want to use ggplot, since I can change the color of the data based on a factor very easy. For some factors I have >20 factor levels and it is not reaonable do to it with stripchart() and add=T


Solution

  • Since your using aes_string for factor1 and factor2 they have to be string. This works for me:

    library(shiny)
    # Define UI for dataset viewer application
    ui<-(fluidPage(
      titlePanel("My project"),
      sidebarLayout(
        sidebarPanel(
          selectInput("dataset", "Choose a dataset:", 
                      choices = c("exp1","exp2", "exp3"),selected="exp1"),
          selectInput(inputId = "param",choices = c("Repsonse A","Response B"),label = "Choose repsonse",selected="Repsonse A"),
          selectInput("factors", "Choose grouping factor:", 
                      choices = c("No grouping","Block"),selected="No grouping"),
          radioButtons(inputId = "fac",choice=c("factor1 on x-axis","factor2 on x-axis"), "Choose type of x-axis")
        ),
        mainPanel(
          h3(textOutput("caption", container = span)),
          plotOutput("strip")
        )
      )
    ))
    library(shiny)
    library(datasets)
    library(ggplot2)
    server<-(function(input, output,clientData, session) {
      df<-data.frame("respondA"=runif(20, min=0,    max=0.4),"respondB"=runif(20, min=0,    max=0.4),"factor1"="A","factor2"="B","replicate"=as.factor(c(1:20)))
      df2<-data.frame("respondA"=runif(20, min=0,    max=0.4),"respondB"=runif(20, min=0,    max=0.4),"factor1"="A","factor2"="B","replicate"=as.factor(c(1:20)))
      df3<-data.frame("respondA"=runif(20, min=0,    max=0.4),"respondB"=runif(20, min=0,    max=0.4),"factor1"="A","factor2"="B","replicate"=as.factor(c(1:20)))
    
      datasetInput <- reactive({
        switch(input$dataset,
               "exp1" = df,
               "exp2" = df2,
               "exp3" = df3)
      })
      params <- reactive({
        switch(input$param,
               "Repsonse A" = datasetInput()$respondA,
               "Response B" = datasetInput()$respondB)
      })
      factor <- reactive({
        switch(input$factors,
               "No grouping"=NULL,
               "Block" = "replicate")
      })
    
      output$strip <- renderPlot({
        if (input$fac == "factor1 on x-axis") {
          p<-ggplot(datasetInput(), aes_string(x = 'factor1', y = params())) + 
            ylab("Mylabel1")+
            xlab("Mylabel2")+
            geom_jitter(position = position_jitter(width = .2),shape=19,size=5,aes_string(colour = factor()))+
            labs(title = "Mytitle")+
            theme_bw() +
            theme(axis.line = element_line(colour = "black"),
                  panel.grid.major = element_blank(),
                  panel.grid.minor = element_blank(),
                  panel.border = element_blank(),
                  panel.background = element_blank()) 
          print(p)
        } else if(input$fac == "factor2 on x-axis") {
          q<-ggplot(datasetInput(), aes_string(x = 'factor2', y = params())) + 
            ylab("Mylabel1")+
            xlab("Mylabel2")+
            geom_jitter(position = position_jitter(width = .2),shape=19,size=5,aes_string(colour = factor()))+
            labs(title = "Mytitle")+
            theme_bw() +
            theme(axis.line = element_line(colour = "black"),
                  panel.grid.major = element_blank(),
                  panel.grid.minor = element_blank(),
                  panel.border = element_blank(),
                  panel.background = element_blank()) 
          print(q)
        }
      }) 
    
    })
    shinyApp(ui=ui,server=server)