阴谋不创建线性趋势线 [英] plotly not creating linear trend line
问题描述
在为散点图创建趋势线时,我使用add_trace
添加线性趋势线.
In creating a trend line for a scatter plot, I am using add_trace
to add a linear trend line.
当数据只有一个系列"数据时,即只有一组坐标,下面的代码可以正常工作.但是,当我介绍多个系列时,趋势线"如下所示:
When the data only has one "series" of data, i.e. there is only one group of coordinates, the code below works fine. However, when I introduce a number of series, the "trend line" looks like this:
这是代码的相关部分:
p <- plot_ly(filteredFull(), x=Relative.Time.Progress, y=cumul.ans.keystroke,
mode='markers', color=KeystrokeRate, size=KeystrokeRate,
marker=list(sizeref=100), type='scatter',
hoverinfo='text', text=paste("token: ",Token, "Keystrokes: ",
KeystrokeCount)) %>%
layout(
xaxis=list(range=c(0,1)),
yaxis=list(range=c(0,max(filteredFull()$cumul.ans.keystroke)))
)
lm.all <- lm(cumul.ans.keystroke ~ Relative.Time.Progress,
data=df)
observe(print(summary(lm.all)))
p <- add_trace(p, y=fitted(lm.all), x=Relative.Time.Progress,
mode='lines') %>%
layout(
xaxis= list(range = c(0,1))
)
p
如有必要,我可以添加更多代码,或尝试制作一个最小的工作示例.但是,我希望这是一个熟悉的问题,从代码中可以明显看出.
I can add more code, or try to make a minimal working example, if necessary. However, I'm hoping that this is a famililar problem that is obvious from the code.
推荐答案
我认为您需要在add_trace(p, y=fitted(lm.all), x=Relative.Time.Progress, mode='lines')
中指定data = ...
参数.
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.
filteredFull()
中的Relative.Time.Progress
与df
之间可能不匹配.
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屋!