简体   繁体   中英

Add a regression line to my scatterplot in r shiny

So I have built a shiny app that allows you to create a scatterplot based on any combination of my variables from my dataset. It also outputs a summary of a regression model on a separate tab.

Basically I want an option to add a regression line to my scatterplot. This would be based on the inputs chosen from the UI to create the scatterplot. I have made a few attempts but nothing successful. Here is the working code I have so far:

  pageWithSidebar(
    titlePanel("Plotting weather trends 2010 - 2014"),
    sidebarPanel(
      selectInput("xCol", "Please select an x variable", names(DF2)),
      selectInput("yCol", "Please select a y variable", names(DF2)),
      checkboxInput("line", "Show regression line?", value = TRUE),
      selectInput("plot_type", "Select a secodary plot for X variable", choices = c("Boxplot", "Histogram"))),
    mainPanel(tabsetPanel(type = "tabs",
                          tabPanel("Plots", (plotOutput("plot1")), plotOutput("plot2")),
                          tabPanel("Regression Model Summary", verbatimTextOutput(outputId = "RegSum"))
    )
    )
  ))


server = function(input, output){ 

  DF3 = reactive({DF2[, c(input$xCol, input$yCol)]})



  output$plot1 = renderPlot({plot(DF3(),
                                  main = "Histogram of select variables")})



  output$plot2 = renderPlot({ 
    if (input$plot_type == "Boxplot") {boxplot(DF2[,input$xCol], main = "boxplot of X variable")}
    if (input$plot_type == "Histogram") {hist(as.numeric(unlist(DF2[,input$xCol])),main = "Histogram of X variable", xlab = "X variable")}
  })

  lm1 <- reactive({lm(reformulate(input$xCol, input$yCol), data = DF2)})
  output$RegSum <- renderPrint({summary(lm1())})

}

shinyApp(ui = ui, server = server)

Previous attempts have included adding an abline(lm) function intaking the inputs from the UI but to no avail.

Any help would be appreciated.

You were on the right way with abline . I guess your problem was how you specified the lm function: If a user selects two variables "xCol" and "yCol", these are passed in quotes to shiny's server. However, the lm function expects a formula notation of the form y ~ x . To get around this problem I would write lm(get(input$yCol) ~ get(input$xCol), data=DF3()) . This way R searches within the data set rather than the global environment.

Here is a reproducible example base on the built-in mtcars dataset:

library(shiny)

ui <- fluidPage(
  selectInput("xCol", "Please select an x variable", names(mtcars)),
  selectInput("yCol", "Please select a y variable", names(mtcars)),
  checkboxInput("line", "Show regression line?", value = TRUE),
  plotOutput("plot")
)

server <- function(input, output, session) {

  data <- reactive({mtcars[, c(input$xCol, input$yCol)]})

  output$plot <- renderPlot({
    plot(data())
    if(input$line) {
      model <- lm(get(input$yCol) ~ get(input$xCol), data=data())
      abline(model)
    }
  })
}

shinyApp(ui, server)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM