2016-07-26 98 views
0

'プロット'散布図に回帰直線を追加する問題。 私は、次のコードをやった:私はそれが欠落値によって引き起こされます数えるplotly回帰直線R

require(plotly) 
data(airquality) 

## Scatter plot ## 

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

enter image description here

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

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

enter image description here

+0

[このスレッドを見る](http://stackoverflow.com/questions/37218319/plotly-not-creating-linear-trend-line) –

答えて

0

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)) 

enter image description here