plotly not creating linear trend line

丶灬走出姿态 提交于 2019-12-06 13:22:04

I think you'll need to specify the data = ... argument in add_trace(p, y=fitted(lm.all), x=Relative.Time.Progress, mode='lines').

The first trace seems to be a subset but the second trace uses the regression fitted values which are obtained by fitting a regression model to the entire dataset.

There might be a mismatch between Relative.Time.Progress in filteredFull() vs df.

Here's an example. Hopefully helps...

library(plotly)
df <- diamonds[sample(1:nrow(diamonds), size = 500),]

fit <- lm(price ~ carat, data = df)

df1 <- df %>% filter(cut == "Ideal")

plot_ly(df1, x = carat, y = price, mode = "markers") %>% 
  add_trace(x = carat, y = fitted(fit), mode = "lines")

plot_ly(df1, x = carat, y = price, mode = "markers") %>% 
  add_trace(data = df, x = carat, y = fitted(fit), mode = "lines")

It changed now a bit, the following code should work fine:

df <- diamonds[sample(1:nrow(diamonds), size = 500),]
fit <- lm(price ~ carat, data = df)
df1 <- df %>% filter(cut == "Ideal")
plot_ly() %>%
    add_trace(data = df1, x = ~carat, y = ~price, mode = "markers") %>% 
    add_trace(data = df, x = ~carat, y = fitted(fit), mode = "lines")

Need to start with empty plotly and add traces.

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!