gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-12-04 09:32:19 26 4
gpt4 key购买 nike

使用 plot() R 中的函数,我试图生成 (SaleDate,SalePrice) = (saldt,sapPr) 形式的点的散点图来自数据帧格式的时间序列、横截面房地产销售数据集。我的问题涉及 X Axis 的标签。几乎任何系列的年度标签就足够了,例如1999,2000,...,2013 或 1999-01-01,...,2013-01-01。我现在得到的是,一个标签,2000,在看起来合适的位置是行不通的。

以下是我给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被调用以限制要绘制的数据的日期和价格范围;注意价格绘制为 000 美元。 r12rgr0$saldt真的是约会; str(r12rgr0$saldt)返回:
Date[1:4190], format: "1999-10-26" "2013-07-06" "2003-08-25" NA NA "2000-05-24"  xx 

我在这里查看了几个关于类似问题的线程,并看到解决方案可能在于关闭默认的 X Axis 行为并使用axis.date,但是我)在我目前的 R 技能水平上,我不确定我d 能够解决问题,并且 ii) 我想知道为什么绘图默认值会产生这些令人费解的(至少对我而言)结果?

Addl Observations:Y Axis 标签正好是 100、200、...、600。散点图的一般外观表明正在观察要求的日期范围,并且绘制点的相对位置是正确的。更换 xlim= ...如上 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"))

都导致错误消息。

最佳答案

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

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

产生几年标记

enter image description here

如果我想要更多控制,我可以使用 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")

这使

enter image description here

请注意 xlim只会放大部分情节。它不直接连接到 Axis 标签,但 Axis 标签会调整以提供一个“漂亮”的范围来覆盖绘制的数据。做只是
xlim=c(as.Date("1999-01-01"),as.Date("2014-01-01"))

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

关于R X Axis 日期标签使用 plot(),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24481176/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com