【问题标题】:How to add R^2 using plotly in R?如何在 R 中使用 plotly 添加 R^2?
【发布时间】:2016-05-12 18:26:28
【问题描述】:

我想知道如何使用 plotly 在绘图上添加回归线方程和 R^2 值。

library(plotly)

x=c(1518,1655,3000,1720,1998,3455,2329,2868,2674,3207,3044,3301,3606,4700,3724,5111,3684,5586,4534,3313,3000,4340,5269, 6568)
y=c(1609,1776,2999,1908,2024,3489,2424,2931,2779,3224,3098,3356,3571,4861,3632,5522,3722,5698,4361,3399,3000,4086,5063,6246)
reg = lm(y ~ x)
modsum = summary(reg)
R2 = summary(reg)$r.squared

# plot
plot_ly(x = x, y = y, type = "scatter", mode = "markers", themes="Catherine")
add_trace(y = reg$fitted.values, type = "scatter", mode = "lines", name = "reg", line = list(width = 2))

【问题讨论】:

    标签: r plotly r-plotly


    【解决方案1】:

    这个呢?

    plot_ly(x = x, y = y, type = "scatter", mode = "markers", themes="Catherine")
    add_trace(y = reg$fitted.values, type = "scatter", mode = "lines", name = "reg", line = list(width = 2)) %>%
      layout(
        showlegend = F,
        annotations = list(x = x, y = y, text = "R2=0.9870744", showarrow = T)
      )
    

    【讨论】:

      猜你喜欢
      • 2021-05-31
      • 2018-11-17
      • 2017-11-03
      • 2013-12-28
      • 2018-09-14
      • 1970-01-01
      • 2022-07-01
      • 2016-02-14
      • 2020-06-03
      相关资源
      最近更新 更多