使用R在时间序列图中标记X轴 [英] Label X Axis in Time Series Plot using R

查看:434
本文介绍了使用R在时间序列图中标记X轴的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我对R有点陌生,总体上对绘图的经验有限.我已经能够使用Zoo在R中将数据作为时间序列对象获取,但是我很难将xaxis正确地标记(如果全部).

I am somewhat new to R and have limited experience with plotting in general. I have been able to work get my data as a time series object in R using zoo, but I am having a hard time having the xaxis be labeled correctly, if it all.

当我绘制动物园物体时

plot(z)

x轴仅显示一个标签,即2010年.该系列每周从2009年4月到2010年10月.

The x-axis only shows one label, the year 2010. when the series is weekly spanning from April 2009 to October 2010.

我试图将系列转换回ts对象,甚至转换为数据框(仅一列,不包括日期).

I tried to convert my series back to a ts object, and even a data frame (only one column and doesn't include the dates).

简而言之,我该如何一般地控制x轴标签以及时间序列对象?

Simply, how can I control the x-axis labels generally, and with time series objects?

提前谢谢!

推荐答案

从示例开始:

x.Date <- as.Date(paste(rep(2003:2004, each = 12), rep(1:12, 2), 1, sep = "-"))
x <- zoo(rnorm(24), x.Date)
plot(x)

如果我们想要不同的刻度位置,我们可以取消默认的坐标轴绘制并添加自己的坐标:

If we want different tick locations, we can suppress the default axis plotting and add our own:

plot(x, xaxt = "n")
axis(1, at = time(x), labels = FALSE)

或将它们组合在一起:

plot(x)
axis(1, at = time(x), labels = FALSE)

您需要指定刻度的位置,因此,如果需要每月,每周等值(而不是上面的观察时间),则需要自己创建相关的位置(日期):

You need to specify the locations for the ticks, so if you wanted monthly, weekly, etc values (instead of observations times above), you will need to create the relevant locations (dates) yourself:

## weekly ticks
plot(x)
times <- time(x)
ticks <- seq(times[1], times[length(times)], by = "weeks")
axis(1, at = ticks, labels = FALSE, tcl = -0.3)

有关更多详细信息,请参见?axis.Date,此外?plot.zoo包含大量此类示例.

See ?axis.Date for more details, plus ?plot.zoo has plenty of examples of this sort of thing.

这篇关于使用R在时间序列图中标记X轴的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆