R - 轴调用错误:(列表)对象无法强制键入'double'

时间:2013-06-29 20:23:32

标签: r plot label posixct

我想创建一个自定义的x轴标签,就像在这里完成的那样: http://blog.earlh.com/index.php/2009/07/plotting-with-custom-x-axis-labels-in-r-part-5-in-a-series/

但是,当我尝试使用:

设置x轴标签时
at <- format(SubDateTime$DateTime, "%H") %in% c("00", "06", "12", "18")
axis(side = 1,at = SubDateTime$DateTime[at],
      labels = format(SubDateTime$DateTime[at], "%a-%H"))

我收到以下错误:

Error in axis(side = 1, at = SubDateTime$DateTime[at], 
              labels = format(SubDateTime$DateTime[at],  
           : (list) object cannot be coerced to type 'double'

SubDateTime $ DateTime是一个POSIXlt类,有两个日期(2013-06-01和2013-06-02)和每小时时间。我想在x轴上添加“Sun-00”,“Sun-06”,“Sun-12”等。

让我感到困惑的是,当我从上面的链接做示例时,它工作正常。

Thx

编辑:

 plot(x = SubDateTime$DateTime,
 y = SubConsumption$Load.MW,
 type = "l",
 lwd = 2,
 ylim = c(0, max(SubConsumption$Load.MW, na.rm = FALSE)*1.2),
 main = "Spot Price June 01 to June 02",
 xlab = "",
 ylab = "",
 bty = "l",
 xaxt = "n")

您需要什么关于SubDateTime $ DateTime的信息?

1 个答案:

答案 0 :(得分:0)

看起来是正确的(如果它应用于POSIXct向量)。正如我在评论中所说,POSIXlt格式化的列实际上是列表,如果您尝试对它们进行操作,它们将为您提供奇怪的错误报告。试试这个:

dtime <- as.POSICct(SubDateTime$DateTime)
at <- format(SubDateTime$DateTime, "%H") %in% c("00", "06", "12", "18")
axis(side = 1,at = SubDateTime$DateTime[at],
      labels = format(SubDateTime$DateTime[at], "%a-%H"))

对比:

dtime <- seq(as.POSIXct("2013-06-01 00:00:00") ,  
             as.POSIXct("2013-06-02 00:00:00"), by="5 min")

dtime.lt <- as.POSIXlt(dtime)
at <- format(dtime.lt, "%H") %in% c("00", "06", "12", "18")
axis(side = 1,at = dtime.lt[at],
       labels = format(dtime.lt[at], "%a-%H"))
Error in axis(side = 1, at = dtime.lt[at], labels = format(dtime.lt[at],  : 
  (list) object cannot be coerced to type 'double'