繁体   English   中英

时间序列图中 x 轴标签的问题

[英]Issue with x-axis labels in a time-series plot

我想从时间序列对象绘制数据并正确自定义 x 轴中显示的标签。

我绘制的数据对应于 2017 年 1 月至 2017 年 12 月的月度值。

我在这里阅读了关于同一主题的其他类似帖子,我已经尝试了我想象的一切,但我无法完成这项工作:

# Data to be plotted
actual <- c(153.3, 12.5, 52, 23, 11.8, 20.8, 1.9, 26.4, 6, 17, 7.4, 3.5)

# Transform them into a time-series object
ts_actual <- ts(actual, frequency = 12, start = c(2017, 1), end = c(2017, 12))

# Create the labels
labels.chart = seq(as.Date("2017-01-01"), as.Date("2017-12-01"), by = "months")

# My plot
plot(ts_actual, col = "blue", type = "p", ylim = c(-100, 200), xaxt = "n")

# This does not work:
axis(1, labels.chart , format(labels.chart, "%Y-%m"))

# Neither does this:
axis.Date(side = 1, at = labels.chart , format = "%Y-%m")

这就是我得到的: 在此处输入图片说明

关于我缺少什么的任何提示?

我想将我的数据保留为时间序列对象,因为这是时间序列分析的一部分。 默认的 x 轴标签不合适:

# Data to be plotted
actual <- c(153.3, 12.5, 52, 23, 11.8, 20.8, 1.9, 26.4, 6, 17, 7.4, 3.5)

# Transform them into a time-series object
ts_actual <- ts(actual, frequency = 12, start = c(2017, 1), end = c(2017, 12))

# Create the labels
labels.chart = seq(as.Date("2017-01-01"), as.Date("2017-12-01"), by = "months")

# My plot
plot(ts_actual, col = "blue", type = "p", ylim = c(-100, 200))

带有默认标签的绘图: 在此处输入图片说明

那么,我该如何解决这个问题(不使用ggplot2或其他不必要的包)?

如果您在axis函数上指定las=2 ,它将显示垂直于 x 轴的标签

在此处输入图片说明

可重现的代码

# Data to be plotted
actual <- c(153.3, 12.5, 52, 23, 11.8, 20.8, 1.9, 26.4, 6, 17, 7.4, 3.5)
x <- c(1:12)

# Transform them into a time-series object
ts_actual <- ts(actual, frequency = 12, start = c(2017, 1), end = c(2017, 12))

# Create the labels
labels.chart = seq(as.Date("2017-01-01"), as.Date("2017-12-01"), by = "months")

labels.chart


# My plot
plot(x, ts_actual, col = "blue", type = "p", ylim = c(-100, 200))
axis(1, x, format(labels.chart, "%Y-%m"), las=2)

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM