我有三个家(每小时的时间序列数据H1, H2, H3
为创建为连续5天)
library(xts)
library(ggplot2)
set.seed(123)
dt <- data.frame(H1 = rnorm(24*5,200,2),H2 = rnorm(24*5,150,2),H3 = rnorm(24*5,50,2)) # hourly data of three homes for 5 days
timestamp <- seq(as.POSIXct("2016-01-01"),as.POSIXct("2016-01-05 23:59:59"), by = "hour") # create timestamp
dt$timestamp <- timestamp
现在我想情节方面表单数据homewise; 因此我融化数据帧作为
tempdf <- reshape2::melt(dt,id.vars="timestamp") # melt data for faceting
colnames(tempdf) <- c("time","var","val") # rename so as not to result in conflict with another melt inside geom_line
在每个小(每个家庭),我希望看到的所有线图的形式五天(每个面应包含对应于不同天5行)的值。 因此,
ggplot(tempdf) + facet_wrap(~var) +
geom_line(data = function(x) {
locdat <- xts(x$val,x$time)# create timeseries object for easy splitting
sub <- split.xts(locdat,f="days") # split data daywise of considered home
sub2 <- sapply(sub, function(y) return(coredata(y))) # arrange data in matrix form
df_sub2 <- as.data.frame(sub2)
df_sub2$timestamp <- index(sub[[1]]) # forcing same timestamp for all days [okay with me]
df_melt <- reshape2::melt(df_sub2,id.vars="timestamp") # melt to plot inside each facet
#return(df_melt)
df_melt
}, aes(x=timestamp, y=value,group=variable,color=variable),inherit.aes = FALSE)
我已经迫使一个家,使绘制简单的日子相同的时间戳。 有了上面的代码中,我得到的情节一样
只有上述情节的问题是,它在所有方面绘制相同的数据。 理想情况下, H1
小应包含的只有1家和数据H2
方面应该包含我知道,我不能在传递homewise数据的家里2.数据geom_line()
任何人都可以帮助正确的方式去做。