[英]Add slope equation to scatterplot regression in Shiny -R
The below code, taken from here produces an interactive correlation heat map. 从此处获取的以下代码生成一个交互式相关热图。 One can select tiles and see the corresponding scatter plot with regression line. 您可以选择图块,然后查看带有回归线的相应散点图。 I am new to shiny
and I'm wondering how I could get the equation of the slope of the regression and the r-squared value added to the plot? 我是新来的shiny
,我想知道我怎么能拿回归并添加到情节的R平方值的斜率的方程? Thank you 谢谢
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)
Figured it out. 弄清楚了。 I got the regression line's equation using a function found here . 我使用这里找到的函数得到了回归线的方程。 Then included this output in an add_annotations
call within the scatterplot function. 然后将此输出包含在add_annotations
函数内的add_annotations
调用中。 Also added names to the points using add_text
. 还使用add_text
在点上添加名称。
Full code: 完整代码:
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.