R 中如何将分解图的 x 轴刻度设为整数年份?
数据阿明
可以先用 xaxt = "n" 隐藏默认 x 轴,再通过 axis() 在整数年份处添加刻度。msts 时间序列的横坐标通常以年份小数表示,因此整数年份可以直接作为刻度位置。
comp <- components(fit)
plot(comp, nc = 1, xaxt = "n")
years <- seq(
ceiling(min(time(msts_spain))),
floor(max(time(msts_spain))),
by = 1
)
axis(1, at = years, labels = as.character(years))
xaxt = "n" 会隐藏默认横轴,axis(1, ...) 则在底部坐标轴上按 years 指定的位置绘制刻度,并显示整数年份标签。刻度会根据序列的起止时间覆盖其中的完整年份。
如果 plot() 使用的 S3 绘图方法不接受或不转发 xaxt 参数,可以在绘图前暂时关闭默认横轴,再手动添加刻度:
old_par <- par(xaxt = "n")
plot(comp, nc = 1)
par(old_par)
axis(1, at = years, labels = as.character(years))
刻度位置需要与图中使用的时间坐标一致。如果绘图方法转换了时间轴,应根据绘图对象的坐标范围调整 at 的值。
备注:内容仅供参考。
