Using the following simulated time series:
使用以下模拟时间序列:
n=70
m1 = matrix(rnorm(n), ncol=7)
m2 = matrix(rnorm(n, 0,4), ncol=7)
d = data.frame(rbind(m1,m2), cl=rep(c(1,2), each=5))
(first 7 columns represent the time point, last column the class)
(前7列代表时间点,最后一列代表该类)
Is it possible to construct a faceted time series that includes the mean curve in each plot, using ggplot2?
是否可以使用ggplot2构建包含每个图中平均曲线的刻面时间序列?
The results should look something like this:
结果应如下所示:
1 个解决方案
#1
12
It might not be the most beautiful code, but I believe it gets you what you are looking for,
它可能不是最漂亮的代码,但我相信它可以满足您的需求,
n=70
m1 = matrix(rnorm(n), ncol=7)
m2 = matrix(rnorm(n, 0,4), ncol=7)
d = data.frame(rbind(m1,m2), cl=rep(c(1,2), each=5))
d <- cbind(paste("d", 1:NROW(d), sep = ""), d)
names(d)[1] <- "id.var"
library(reshape)
longDF <- melt(d, id=c("cl", "id.var"))
library(ggplot2)
p <- ggplot(data = longDF, aes(x = variable, y = value, group = id.var))
p + geom_line() + stat_smooth(aes(group = 1), method = "lm",
se = FALSE, colour="red") + facet_grid(cl ~ .)
Please don't hesitate to improve my code.
请不要犹豫,改进我的代码。
#1
12
It might not be the most beautiful code, but I believe it gets you what you are looking for,
它可能不是最漂亮的代码,但我相信它可以满足您的需求,
n=70
m1 = matrix(rnorm(n), ncol=7)
m2 = matrix(rnorm(n, 0,4), ncol=7)
d = data.frame(rbind(m1,m2), cl=rep(c(1,2), each=5))
d <- cbind(paste("d", 1:NROW(d), sep = ""), d)
names(d)[1] <- "id.var"
library(reshape)
longDF <- melt(d, id=c("cl", "id.var"))
library(ggplot2)
p <- ggplot(data = longDF, aes(x = variable, y = value, group = id.var))
p + geom_line() + stat_smooth(aes(group = 1), method = "lm",
se = FALSE, colour="red") + facet_grid(cl ~ .)
Please don't hesitate to improve my code.
请不要犹豫,改进我的代码。