自定义轴标签在R中绘制预测 [英] custom axis labels plotting a forecast in R
问题描述
我正在尝试为预测添加一些明智的标签。
I'm trying to get some sensible labels on a forecast.
这是我的代码:
library("forecast")
t <- ts(
c(
4410.0, 6435.0,
4939.0, 6487.0, 25521.0, 18764.0,
12223.0, 18590.0, 36898.0, 28826.0,
20329.0
)
, frequency = 4
, start=c(2011, 7, 1)
)
cast <- meanf(t,h=4)
par(mfrow=c(1,1),xaxt="n")
plot(cast)
ticks <- seq(as.POSIXct("2011-07-01"), by = "3 month", length.out=length(cast) + 4)
axis.Date(1, at = ticks, labels = ticks, format="%Y-%m")
没有错误或警告。 。但是没有轴添加到绘图中:(
There are no errors or warnings... but no axis gets added to the plot :(
如果我使用 xaxt = s
,那么我会获取轴标签,但它们不是2014.0之类的东西(即根本不格式化为日期),理想情况下,我实际上希望更易读,例如 Q3 2014,但我定于2014-07 (该季度的第一个月)。
If I use xaxt="s"
then I do get axis labels, but they're not they're things like 2014.0 (i.e. not formatted as dates at all). Ideally I'd actually like something a bit more readable like "Q3 2014" but I'd settle for 2014-07 (the first month in the quarter).
任何人都知道我在做什么我做错了吗?我在互联网上搜索到的所有内容都建议先禁用默认轴,然后再使用轴或axis.Date函数在其上添加自定义...,但是使用这些方法中的任何一种我都无法获得任何类型的轴:(
Any idea what I'm doing wrong? All of the stuff I've found searching the internet suggests disabling the default axis and then using the axis or axis.Date functions to add a custom on... but I can't get an axis of any sort using either of those methods :(
推荐答案
我不会说有bug,而是功能 forecast :: plot。预测
不适用于 axis.Date
或 axis.POSIXct
(不在包 forecast
)中使用。
I would not say there is a bug, rather the function forecast::plot.forecast
is not designed to be used with axis.Date
or axis.POSIXct
(which are not used in the package forecast
).
在调用函数 axis之前。
和 axis.POSIXct
,必须将时间点作为序列明确传递给 plot
Date
或 POSIXct
对象的值。如果将时间序列绘制为 plot(t)
,则似乎未正确定义上述功能使用的x轴。
Before calling the functions axis.Date
and axis.POSIXct
, the time points must be explicitly passed to plot
as a sequence of Date
or POSIXct
objects. If we plot the time series as plot(t)
then the x-axis does not seem to be properly defined as to be used by the above functions.
请参见下面的代码以及变量时间
已创建并传递到图
。 plot(time,x)
被使用,而不仅仅是 plot(x)
。因此,功能 axis
将能够显示时间轴的标签。 (在此示例中,置信区间看起来不如 forecast :: plot.forecast
显示的那些
好。)
See the code below and how the variable time
is created and passed to plot
. plot(time, x)
is used instead of just plot(x)
. Thus the function axis
will be able to display the labels of the time axis. (In this example, the confidence intervals will not look as nice as those
displayed by forecast::plot.forecast
.)
library("forecast")
require(zoo)
t <- ts(
c(
4410.0, 6435.0,
4939.0, 6487.0, 25521.0, 18764.0,
12223.0, 18590.0, 36898.0, 28826.0,
20329.0
)
, frequency = 4
, start=c(2011, 7, 1)
)
cast <- meanf(t, h=4)
x <- ts(c(t, cast$mean), start = c(start(t), 1), frequency = frequency(t))
time <- as.yearqtr(seq(start(x)[1], end(x)[1] + 1, 1/frequency(x)))
time <- time[seq_along(x)]
time <- as.POSIXct(time)
plot(time, x, type = "n", xaxt = "n", ylim = range(c(cast$lower, cast$upper)))
lines(time[seq_along(t)], t)
lines(time[-seq_along(t)], cast$mean, col = "blue")
lines(time[-seq_along(t)], cast$upper[,2], col = "red")
lines(time[-seq_along(t)], cast$lower[,2], col = "red")
ticks <- seq(as.POSIXct("2011-07-01"), by = "3 month", length.out=length(cast) + 4)
axis(side = 1, at = ticks, labels = ticks)
这篇关于自定义轴标签在R中绘制预测的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!