简体   繁体   English

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

[英]R X-axis Date Labels using plot()

Using the plot() function in R, I'm trying to produce a scatterplot of points of the form (SaleDate,SalePrice) = (saldt,sapPr) from a time-series, cross-section real estate sales dataset in dataframe format.使用 R 中的plot()函数,我试图从数据帧格式的时间序列横截面房地产销售数据集中生成(SaleDate,SalePrice) = (saldt,sapPr)形式的点的散点图。 My problem concerns labels for the X-axis.我的问题涉及 X 轴的标签。 Just about any series of annual labels would be adequate, eg 1999,2000,...,2013 or 1999-01-01,...,2013-01-01.几乎任何系列的年度标签就足够了,例如 1999,2000,...,2013 或 1999-01-01,...,2013-01-01。 What I'm getting now, a single label, 2000, at what appears to be the proper location won't work.我现在得到的是,一个标签,2000,在看起来合适的位置是行不通的。

The following is my call to plot() :以下是我对plot()调用:

plot(r12rgr0$saldt, r12rgr0$salpr/1000, type="p", pch=20, col="blue", cex.axis=.75, 
     xlim=c(as.Date("1999-01-01"),as.Date("2014-01-01")),
     ylim=c(100,650), 
     main="Heritage Square Sales Prices $000s 1990-2014",xlab="Sale Date",ylab="$000s")

The xlim and ylim are called out to bound the date and price ranges of the data to be plotted;调用xlimylim来限定要绘制的数据的日期和价格范围; note prices are plotted as $000s.注意价格绘制为 000 美元。 r12rgr0$saldt really is a date; r12rgr0$saldt真的一个约会; str(r12rgr0$saldt) returns: str(r12rgr0$saldt)返回:

Date[1:4190], format: "1999-10-26" "2013-07-06" "2003-08-25" NA NA "2000-05-24"  xx 

I have reviewed several threads here concerning similar questions, and see that the solution probably lies with turning off the default X-axis behavior and using axis.date, but i) At my current level of R skill, I'm not sure I'd be able to solve the problem, and ii) I wonder why the plotting defaults are producing these rather puzzling (to me, at least) results?我在这里查看了几个关于类似问题的线程,并看到解决方案可能在于关闭默认的 X 轴行为并使用axis.date,但是我)在我目前的 R 技能水平上,我不确定我d 能够解决问题,并且 ii) 我想知道为什么绘图默认值会产生这些令人费解的(至少对我而言)结果?

Addl Observations: The Y-axis labels are just fine 100, 200,..., 600. The general appearance of the scatterplot indicates the called-for date ranges are being observed and the relative positions of the plotted points are correct. Addl Observations:Y 轴标签正好是 100、200、...、600。散点图的一般外观表明正在观察要求的日期范围,并且绘制点的相对位置是正确的。 Replacing xlim= ... as above with xlim=c("1999-01-01","2014-01-01")xlim= ... 替换为xlim=c("1999-01-01","2014-01-01")

or或者

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

or或者

xlim=c(as.POSIXct("1999-01-01", format="%Y-%m-%d"),as.POSIXct("2014-01-01", format="%Y-%m-%d"))

all result in error messages.都导致错误消息。

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))
)

A simple plot with一个简单的情节

with(dd, plot(saldt, salpr))

produces a few year marks产生几年标记

在此处输入图片说明

If i wanted more control, I could use axis.Date as you alluded to如果我想要更多的控制,我可以使用你提到的axis.Date

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

which gives这使

在此处输入图片说明

note that xlim will only zoom in parts of the plot.请注意, xlim只会放大绘图的一部分。 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"))

is the correct way to zoom the plot.是缩放绘图的正确方法。 No need for conversion to numeric or POSIXct.无需转换为数字或 POSIXct。

If you are running a plot in real time and don't mind some warnings, you can just pass, eg, format = "%Y-%m-%d" in the plot function.如果您正在实时运行绘图并且不介意一些警告,则可以在绘图函数中传递例如format = "%Y-%m-%d" For instance:例如:

plot(seq((Sys.Date()-9),Sys.Date(), 1), runif(10), xlab = "Date", ylab = "Random")

yields:产量: while:尽管:

plot(seq((Sys.Date()-9), Sys.Date(), 1), runif(10), format = "%Y-%m-%d", xlab = "Date", ylab = "Random")

yields:产量: with lots of warnings about format not being a graphical parameter.有很多关于format不是图形参数的警告。

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

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