且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

使用plot()的R X轴日期标签

更新时间:2023-11-22 08:29:10

使用绘图很难在没有样本数据的情况下重现结果.这是我要使用的示例

With plots it's very hard to reproduce results with out sample data. Here's a sample I'll use

dd<-data.frame(
  saldt=seq(as.Date("1999-01-01"), as.Date("2014-01-10"), by="6 mon"),
  salpr = cumsum(rnorm(31))
)

一个简单的情节

with(dd, plot(saldt, salpr))

产生几年的标记

如果我想获得更多控制权,可以使用您提到的axis.Date

If i wanted more control, I could use axis.Date as you alluded to

with(dd, plot(saldt, salpr, xaxt="n"))
axis.Date(1, at=seq(min(dd$saldt), max(dd$saldt), by="30 mon"), format="%m-%Y")

给出

请注意,xlim仅会放大部分绘图.它未直接连接到轴标签,但会调整轴标签以提供一个漂亮"范围来覆盖所绘制的数据.只是做

note that xlim will only zoom in parts of the plot. It is not directly connected to the axis labels but the axis labels will adjust to provide a "pretty" range to cover the data that is plotted. Doing just

xlim=c(as.Date("1999-01-01"),as.Date("2014-01-01"))

是缩放图的正确方法.无需转换为数字或POSIXct.

is the correct way to zoom the plot. No need for conversion to numeric or POSIXct.