使用plot()的R X轴日期标签 [英] R X-axis Date Labels using plot()

查看:735
本文介绍了使用plot()的R X轴日期标签的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

使用R中的plot()函数,我试图从数据框格式的时间序列,横截面房地产销售数据集中生成形式为(SaleDate,SalePrice) = (saldt,sapPr)的点的散点图.我的问题涉及X轴的标签.几乎任何系列的年度标签都足够,例如1999,2000,...,2013或1999-01-01,...,2013-01-01.我现在得到的是,只有一个标签2000贴上了正确的位置,将无法使用.

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. My problem concerns labels for the X-axis. Just about any series of annual labels would be adequate, e.g. 1999,2000,...,2013 or 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.

以下是我对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")

呼出xlimylim以限制要绘制数据的日期和价格范围;票据价格绘制为$ 000s. r12rgr0$saldt实际上日期; str(r12rgr0$saldt)返回:

The xlim and ylim are called out to bound the date and price ranges of the data to be plotted; note prices are plotted as $000s. r12rgr0$saldt really is a date; str(r12rgr0$saldt) returns:

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

我在这里回顾了有关类似问题的多个主题,并看到解决方案可能在于关闭默认的X轴行为并使用axis.date,但是i)以我目前的R技能水平,我还没有确保我能够解决该问题,并且ii)我想知道为什么默认绘图会产生这些(至少对我来说)令人困惑的结果?

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?

Addl观察:Y轴标签分别为100、200,...,600.散点图的总体外观指示正在观察所需的日期范围,并且绘制点的相对位置正确.如上所述,将xlim= ...替换为xlim=c("1999-01-01","2014-01-01")

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. Replacing xlim=... as above with xlim=c("1999-01-01","2014-01-01")

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

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

一个简单的情节

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.

这篇关于使用plot()的R X轴日期标签的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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