在ggplot2中绘制facet时间序列中的数据子集

时间:2014-04-03 21:23:59

标签: r ggplot2

在绘制月度分面时间序列时,构面仅绘制相应月份的数据集,但x轴显示全年的整个时间序列。导致非常压缩的小平面图。

以下是复制问题的完整代码:

library(chron)
library(ggplot2)
library(scales)

t1 <- chron("1/1/2011", "00:00:00", )
t2 <- chron("12/31/2011", "00:00:01") # 1 sec is to counter floating point errors
DateTime <- seq(t1, t2, by = times("06:00:00"))
GHI <- sample(0:1000, 1457, replace=T)
df <- data.frame(DateTime = as.POSIXct (DateTime), GHI)
# Upto now df represents my actual dataset. Month column is extracted from the DateTime    column.

df$Month <- format(df$DateTime, "%m")

ggplot(df, aes(DateTime, GHI)) + 
       geom_line() + 
       scale_x_datetime(breaks = '10 days', labels = date_format("%d")) + 
       xlab("") + 
       facet_grid(. ~ Month)

正如您所看到的,facet应该显示相应月份的数据和比例。但它只显示了整年的规模和本月的情节。

另一个问题是,对于方面,我必须创建一个月列。有没有办法我不必这样做,因为该信息已在DateTime列中提供

0 个答案:

没有答案