简体   繁体   English

在我的散点图中添加一条回归线

[英]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.这将基于从 UI 中选择的输入来创建散点图。 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.之前的尝试包括添加一个 abline(lm) 函数,从 UI 获取输入,但无济于事。

Any help would be appreciated.任何帮助,将不胜感激。

You were on the right way with abline .你对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.我猜你的问题是你如何指定lm函数:如果用户选择两个变量“xCol”和“yCol”,它们会在引号中传递给闪亮的服务器。 However, the lm function expects a formula notation of the form y ~ x .但是, lm函数需要y ~ x形式的公式符号。 To get around this problem I would write lm(get(input$yCol) ~ get(input$xCol), data=DF3()) .为了解决这个问题,我会写lm(get(input$yCol) ~ get(input$xCol), data=DF3()) This way R searches within the data set rather than the global environment.这样 R 在数据集中而不是全局环境中搜索。

Here is a reproducible example base on the built-in mtcars dataset:这是基于内置 mtcars 数据集的可重现示例:

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)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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