情节回归线R [英] plotly regression line R

查看:101
本文介绍了情节回归线R的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在散点图"散点图中添加回归线的问题. 我已经完成了以下代码:

Problem with adding a regression line to a 'plotly' scatter plot. I've done the following code:

require(plotly)
data(airquality) 

## Scatter plot ##

c <- plot_ly(data = airquality, 
    x = Wind,
    y = Ozone, 
    type = "scatter",
    mode = "markers"
    )
c

## Adding regression line (HERE IS THE PROBLEM) ##

g <- add_trace(c, 
     x = Wind,
     y = fitted(lm(Ozone ~ Wind, airquality)),
     mode = "lines"
     )
g 

推荐答案

我认为这是由于缺少值造成的

I reckon it's caused by the missing values

airq <- airquality %>% 
  filter(!is.na(Ozone))

fit <- lm(Ozone ~ Wind, data = airq)

airq %>% 
  plot_ly(x = ~Wind) %>% 
  add_markers(y = ~Ozone) %>% 
  add_lines(x = ~Wind, y = fitted(fit))

这篇关于情节回归线R的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆