简体   繁体   English

将POSIXct传递给xlim

[英]Passing POSIXct to xlim

Would some kind soul please explain why xlim does not acknowledge a POSIXct object as such in the following snippet: 请介绍一下以下代码段,为什么xlim不承认POSIXct对象:

y <- rnorm(3)
x <- c("2012-01-01", "2013-01-01", "2014-01-01")
x <- as.POSIXct(x) 
plot(y, xlim=range(x))

This results in an empty plot whose xlims are not dates but numeric equivalents and the following error: 这将导致一个空图,其xlim不是日期,而是数字等价物和以下错误:

Error in plot.window(...) : invalid 'xlim' value

I scoured old posts and some version of the following allegedly works 我搜索了旧帖子,并发现了以下版本的某些作品:

plot(y, xlim=seq(as.POSIXct("2012-01-01"), as.POSIXct("2014-01-01"), by="month")

But I received the same error when I tried. 但是我尝试时遇到了同样的错误。 I'd settle for any way to denominate my xtick labels in dates, preferably months. 我会以任何方式解决在日期(最好是几个月)中命名我的xtick标签的问题。

The snippet above is obviously trivialized to demonstrate my problem but I have verified that my actual x variable is a POSIXct object with dates of the form YYYY-MM-DD and my y is a vector of equal length to x. 上面的代码段显然被微不足道地演示了我的问题,但我已经验证了我的实际x变量是POSIXct对象,其日期格式为YYYY-MM-DD,而我的y是一个与x长度相等的向量。

You may try 你可以试试

 plot(x,y, xaxt='n')
 axis.POSIXct(side=1, x, format='%Y-%m-%d')

Other option is 其他选择是

 library(xts)
 x1 <- xts(y, order.by=x)
 plot(x1, xlim=range(index(x1)), type='p')

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

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