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

时间:2018-04-26 00:39:22

标签: r shiny plotly heatmap

以下代码取自here,生成交互式相关热图。可以选择切片并使用回归线查看相应的散点图。我是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)

1 个答案:

答案 0 :(得分:2)

想出来。我使用找到的函数here得到了回归线的等式。然后将此输出包含在scatterplot函数内的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)