首页 文章

如何快速(和优雅地)在时间序列对象`ts`和R中的日期帧之间迭代ggplot2绘图?

提问于
浏览
0

我正在寻求关于如何在R中的时间序列对象和日期帧之间快速迭代的指导,以便我在 ggplot2 中绘图,但允许对时间序列进行一般分析为 ts() .

例如,以下感觉非常笨重:

library(ggplot2)
library(lubridate)
library(forecast)

AP <- AirPassengers
df <- data.frame(date=as.Date(time(AP)), Y=as.matrix(AP))

ggplot(df, aes(x=factor(month(date)), y=Y)) +
  geom_boxplot()

此外,我放松了(?)这种方式利用_1235727的能力?

The essence of the question :如何在代码 with ggplot2 中快速绘制图形的结果,理想情况下可以使用x轴的月标签,同时希望跳过更少的环?

我意识到我可以使用:

boxplot(AP ~ cycle(AP))

但我想使用 ggplot2 来获得更大的灵活性 .

1 回答

  • 1

    好吧,这似乎有效 .

    library(xts)
    library(ggplot2)
    AP <- AirPassengers
    df <- data.frame(date=as.Date(time(AP)), Y=as.matrix(AP))
    ggplot(df)+geom_boxplot(aes(x=format(date,"%m"),y=Y))+
      scale_x_discrete("",labels=unique(format(df$date,"%b")))
    

相关问题