从ggplot扩展图以进行预测

时间:2017-05-21 02:27:23

标签: r ggplot2

我正在尝试预测未来特定日期的收入水平。我所做的是在我的情节中应用平滑曲线。代码就是这个

ggplot(timeseries, aes(x=Weekstarting, y=PHCumm))+
geom_line()+
stat_smooth(method = glm, fullrange =TRUE, formula= y~poly(x,5))

我试图变得聪明并在最后添加这个小片段     xlim = c(as.POSIXct(&#39; 2017-01-01 00:00:00&#39;,格式=&#34;%Y-%m-%d%H:%M:%S&#34 ), + as.POSIXct(&#39; 2017-07-01 00:00:00&#39;,格式=&#34;%Y-%m-%d%H:%M:%S&#34;)< / p>

我收到错误。

在unclass(e2)中出错:无法取消环境

有人可以帮助我吗?

1 个答案:

答案 0 :(得分:1)

我认为您需要的是scale_x_date(或scale_x_datetime,具体取决于您的数据):

library(tidyverse)
set.seed(42)

# sample data
timeseries <- data_frame(
    Weekstarting = 
        seq.Date(
            from = as.Date("2017-01-02"),
            by = "week",
            length.out = 250
        ),
    PHCumm = cumsum(rnorm(250))
    )

timeseries %>% ggplot(aes(x = Weekstarting, y = PHCumm)) +
    geom_line() +
    stat_smooth(method = glm, fullrange =TRUE, formula= y~poly(x,5)) +
    scale_x_date(limits = c(as.Date("2017-01-02"), as.Date("2022-06-02")))

enter image description here