繁体   English   中英

将斜率方程式添加到Shiny -R的散点图回归中

[英]Add slope equation to scatterplot regression in Shiny -R

此处获取的以下代码生成一个交互式相关热图。 您可以选择图块,然后查看带有回归线的相应散点图。 我是新来的shiny ,我想知道我怎么能拿回归并添加到情节的R平方值的斜率的方程? 谢谢

library(plotly)
library(shiny)

# compute a correlation matrix
correlation <- round(cor(mtcars), 3)
nms <- names(mtcars)

ui <- fluidPage(
  mainPanel(
    plotlyOutput("heat"),
    plotlyOutput("scatterplot")
  ),
  verbatimTextOutput("selection")
)

server <- function(input, output, session) {
  output$heat <- renderPlotly({
    plot_ly(x = nms, y = nms, z = correlation, 
            key = correlation, type = "heatmap", source = "heatplot") %>%
      layout(xaxis = list(title = ""), 
             yaxis = list(title = ""))
  })

  output$selection <- renderPrint({
    s <- event_data("plotly_click")
    if (length(s) == 0) {
      "Click on a cell in the heatmap to display a scatterplot"
    } else {
      cat("You selected: \n\n")
      as.list(s)
    }
  })

  output$scatterplot <- renderPlotly({
    s <- event_data("plotly_click", source = "heatplot")
    if (length(s)) {
      vars <- c(s[["x"]], s[["y"]])
      d <- setNames(mtcars[vars], c("x", "y"))
      yhat <- fitted(lm(y ~ x, data = d))
      plot_ly(d, x = ~x) %>%
        add_markers(y = ~y) %>%
        add_lines(y = ~yhat) %>%
        layout(xaxis = list(title = s[["x"]]), 
               yaxis = list(title = s[["y"]]), 
               showlegend = FALSE)
    } else {
      plotly_empty()
    }
  })

}

shinyApp(ui, server)

弄清楚了。 我使用这里找到的函数得到了回归线的方程。 然后将此输出包含在add_annotations函数内的add_annotations调用中。 还使用add_text在点上添加名称。

完整代码:

library(plotly)
library(shiny)
library(magrittr)

# compute a correlation matrix
correlation <- round(cor(mtcars), 3)
nms <- names(mtcars)

ui <- fluidPage(
mainPanel(
plotlyOutput("heat"),
plotlyOutput("scatterplot")
),
verbatimTextOutput("selection")
)

server <- function(input, output, session) {
output$heat <- renderPlotly({
plot_ly(x = nms, y = nms, z = correlation, 
key = correlation, type = "heatmap", source = "heatplot") %>%
layout(xaxis = list(title = ""), 
yaxis = list(title = ""))
})

output$selection <- renderPrint({
s <- event_data("plotly_click")
if (length(s) == 0) {
"Click on a cell in the heatmap to display a scatterplot"
} else {
cat("You selected: \n\n")
as.list(s)
}
})

lm_eqn <- function(df){
g<-as.character("y = a + b x, R2= r2 ");
m <- lm(y ~ x, df);
eq <- g %<>%
gsub("a", format(coef(m)[1], digits = 2), .) %>%
gsub("b", format(coef(m)[2], digits = 2), .) %>%
gsub("r2", format(summary(m)$r.squared, digits = 3), .);                 
}

output$scatterplot <- renderPlotly({
s <- event_data("plotly_click", source = "heatplot")
if (length(s)) {
vars <- c(s[["x"]], s[["y"]])
d <- setNames(mtcars[vars], c("x", "y"))
yhat <- fitted(lm(y ~ x, data = d))
plot_ly(d, x = ~x, text= rownames(mtcars)) %>%
add_markers(y = ~y) %>%
add_lines(y = ~yhat) %>%
add_text(y=~y, textposition='top right')%>%
add_annotations(x=-1,y=-1,text=lm_eqn(d))%>%
layout(xaxis = list(title = s[["x"]]), 
yaxis = list(title = s[["y"]]), 
showlegend = FALSE)
} else {
plotly_empty()
}
})

}

shinyApp(ui, server)

暂无
暂无

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

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