0

我想创建一个自定义 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 是一个具有两个日期(2013-06-01 和 2013-06-02)和每小时时间的 POSIXlt 类。我想在 x 轴上添加“Sun-00”、“Sun-06”、“Sun-12”等。

令我困惑的是,当我从上面的链接中执行示例时,它工作正常。

谢谢

编辑:

 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 的哪些信息?

4

1 回答 1

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'
于 2013-06-30T00:19:24.907 回答