【问题标题】:How to plot a certain range of x values of dates and times? Error "invalid xlim value"如何绘制一定范围的日期和时间的 x 值?错误“无效的 xlim 值”
【发布时间】:2014-05-29 06:51:53
【问题描述】:

如果我想放大整个 x 域(时间轴)内的某个时间范围,我在绘制时间序列数据时遇到问题。上下文见以下代码:

date_time <- as.factor(c('8/24/07 17:30','8/24/07 18:00','8/24/07 18:30',
                     '8/24/07 19:00','8/24/07 19:30','8/24/07 20:00',
                     '8/24/07 20:30','8/24/07 21:00','8/24/07 21:30',
                     '8/24/07 22:00','8/24/07 22:30','8/24/07 23:00',
                     '8/24/07 23:30','8/25/07 00:00','8/25/07 00:30'))

U. <- as.factor(c('0.2355','0.2602','0.2039','0.2571','0.1419','0.0778','0.3557',
              '0.3065','0.1559','0.0943','0.1519','0.1498','0.1574','0.1929','0.1407'))

dts<-strptime(as.character(date_time),'%m/%d/%y %H:%M')

plot(dts,as.numeric(U.),xlim=c('2007-08-24 17:30:00','2007-08-24 19:00:00'))

我的问题:为什么 xlim 在这种情况下不起作用以及如何解决?请帮忙。

注意:忽略 as.factoras.numeric,我添加这些是为了在将其导入 R 后模拟实际数据

【问题讨论】:

    标签: r plot


    【解决方案1】:

    您需要告诉 R 您的 xlim 值是日期和时间。所以尝试使用

    xlim=c(as.POSIXct('2007-08-24 17:30:00', format="%Y-%m-%d %H:%M:%S"),
                             as.POSIXct('2007-08-24 19:00:00', format="%Y-%m-%d %H:%M:%S"))
    

    顺便说一句,as.numeric(U.) 可能不会产生您真正想要的结果。您应该改用 as.numeric(as.character(U.)) - 比较结果。

    【讨论】:

      猜你喜欢
      • 2014-03-06
      • 2011-01-26
      • 1970-01-01
      • 2017-11-28
      • 2016-01-10
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多