如何将geom_point()添加到autolayer()行?

时间:2019-04-13 18:08:03

标签: r ggplot2 forecast autolayer

尝试将geom_points添加到autolayer()行(图片中为“ fitted”),这是Rob Hyndmans预测包中ggplot2的autoplot()的wrapper部分(ggplot2中有一个基本的autoplot / autolayer)也是如此,同样适用于此。)

问题是(我不是ggplot2专家,自动绘图程序包装起来比较棘手)geom_point()适用于主调用,但是如何与自动图层类似(适用值)应用?

像普通的geom_line()一样尝试type =“ b”,但它不是autolayer()中的对象参数。

        const int BufferLimit = 10000;
        public static string ReadLine()
        {
            Stream s = Console.OpenStandardInput(BufferLimit);
            byte[] Buffer = new byte[BufferLimit];
            int Length = s.Read(Buffer, 0, BufferLimit);
            return new string(Encoding.UTF7.GetChars(Buffer, 0, Length));
        }

Fitted line needs points

1 个答案:

答案 0 :(得分:1)

这似乎可行:

library(forecast)
library(fpp2)

model.ses <- ets(mdeaths, model="ANN", alpha=0.4)
model.ses.fc <- forecast(model.ses, h=5)

# Pre-compute the fitted layer so we can extract the data out of it with 
# layer_data()
fitted_layer <- forecast::autolayer(model.ses.fc$fitted, series="Fitted")
fitted_values <- fitted_layer$layer_data()

plt <- forecast::autoplot(mdeaths) +
  fitted_layer +
  geom_point() +
  geom_point(data = fitted_values, aes(x = timeVal, y = seriesVal))

enter image description here

也许有一种方法可以使forecast::autolayer直接执行您想要的操作,但是此解决方案可以工作。如果希望图例看起来正确,则需要将输入数据和拟合值合并到单个data.frame中。