如何更改先知图中的线型? [英] How to change type of line in prophet plot?
问题描述
Facebook的Prophet in R(还有Python版本)用于生成时间序列预测.
Facebook's Prophet in R (there's also a Python version) is used to generate time series forecasts.
模型m
由以下人员创建:
m <- prophet(df)
future <- make_future_dataframe(m, periods = 365)
forecast <- predict(m, future)
plot(m, forecast)
这将返回格式非常精美的图形,例如:
Which returns a very nicely formatted graph, like:
我想更改线型,而不是点,而是通常的细线.
I would like to change the line type, to get not dots but a usual thin line.
我尝试过
lines(m$history$y,lty=1)
但出现错误
In doTryCatch(return(expr), name, parentenv, handler)
有没有建议如何将这些点转换成线?
Are there are any suggestions how to convert those dots into a line?
推荐答案
用于prophet
对象的plot
方法使用ggplot2
,因此像lines()
这样的基本R图形功能将不起作用.您可以使用ggplot2::geom_line()
来添加 行,但是目前我还没有一种简单的方法来按行逐行替换 点...
The plot
method for prophet
objects uses ggplot2
, so base R graphics functions like lines()
won't work. You can use ggplot2::geom_line()
to add lines, but at the moment I don't see an easy way to replace the points by lines ...
来自?prophet
的示例:
history <- data.frame(ds = seq(as.Date('2015-01-01'), as.Date('2016-01-01'), by = 'd'),
y = sin(1:366/200) + rnorm(366)/10)
m <- prophet(history)
future <- make_future_dataframe(m, periods = 365)
forecast <- predict(m, future)
pp <- plot(m,forecast)
添加行:
library(ggplot2)
pp + geom_line()
此问题提供了一种(hacky)前进的方式:
This question provides a (hacky) way forward:
pp2 <- pp + geom_line()
qq2 <- ggplot_build(pp2)
qq2$data[[2]]$colour <- NA
plot(ggplot_gtable(qq2))
但是很明显,hack出了点问题.最好的选择是查看plot方法(prophet:::plot.prophet
)并将其修改为想要的行为……这是基本版本:
But obviously something went wrong with the hack. The better bet would be to look at the plot method(prophet:::plot.prophet
) and modify it to behave as you want ... Here is the bare-bones version:
df <- prophet:::df_for_plotting(m, forecast)
gg <-ggplot(df, aes(x = ds, y = y)) + labs(x = "ds", y = "y")
gg <- gg + geom_ribbon(ggplot2::aes(ymin = yhat_lower,
ymax = yhat_upper), alpha = 0.2, fill = "#0072B2",
na.rm = TRUE)
## replace first geom_point() with geom_line() in next line ...
gg <- gg + geom_line(na.rm = TRUE) + geom_line(aes(y = yhat),
color = "#0072B2", na.rm = TRUE) + theme(aspect.ratio = 3/5)
尽管...我可能已经去除了您的数据/预测中存在的某些组件.
I may have stripped out some components that exist in your data/forecast, though ...
这篇关于如何更改先知图中的线型?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!