Shiny Apps 中的 qqplot 改变轴和条件图的问题

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

我有一些问题,将 ggplot2 实现到闪亮的应用程序中

  1. 我无法让下面给出的示例中的代码正常工作。出于某种原因,当我使用 aes_string 时它找不到 factor1,而当我使用 aes 时它找不到 params()。在包含我的数据的文件中,此步骤有效。对不起。

  2. 我想用qqplot2绘制我的数据 项目。当我获取数据并在闪亮的应用程序之外绘制它们时, 一切都好。当我 在闪亮的应用程序中实现代码,它从阶乘改变了我的轴 一个到一个有争议的。

  3. 我正在改变 x 轴根据用户输入 with with "radioButtons"。当我 使用 stripchart(),效果很好。当我改用 ggplot 时,它 首先工作正常,但是当我更改因子(更改 radioButton 的值)时,它找不到输入对象 (input$strain).

抱歉这么久 post。非常感激您的帮忙。

这是我的代码:

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")
     )
  )
))

和 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:我想使用 ggplot,因为我可以很容易地根据一个因素更改数据的颜色。对于某些因素,我有 >20 个因素水平,使用 stripchart()add=T

是不合理的

由于您对 factor1 和 factor2 使用 aes_string,因此它们必须是字符串。这对我有用:

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)