我一直在试图创建一个for循环,将通过我的数据框,并为我有每个个人ID作出相同的情节。下面是一些样本数据:for循环创建多个ggplots从单个数据帧
TJID1 <- c("TJ22", "TJ22", "TJ23", "TJ23", "TJ23", "TJ24", "TJ24")
Day <- c("2005-11-22", "2005-11-23", "2006-12-01", "2006-12-02", "2006-12-03","2005-07-08", "2005-07-08")
Mean.Depth <- c (2, 2, 3, 4, 5, 6, 6)
SE.Depth <- c(1, 1, 2, 2, 1, 2, 2)
sample <- cbind(TJID1, Day, Mean.Depth, SE.Depth)
sample <- as.data.frame(sample)
我有每个人的不同TJ编号,然后为每个TJ他们有不同的每日深度的手段。每个人的日子都在变化。我已经能够通过每个TJ TJ22 <- sample [sample$TJID1 == "TJ22", ]
对主数据框进行子集分类。从图中我的代码,然后(使用子集化的数据帧):
DailyMeans_TJ22 <- ggplot(TJ22, aes(x=Day, y=Mean.Depth))+
geom_point()+
geom_line()+
geom_errorbar(aes(ymin=Mean.Depth-1.96*SE.Depth, ymax=Mean.Depth+1.96*SE.Depth), width = 0.5, col="red")
我想创建一个for循环,只是通过每个个体周期,使用户在同样的情节。这是我到目前为止:
var_list = combn(names(sample) [3:4], 3, simplify=FALSE)
plot_list = list()
for (i in unique (sample$TJID1)){
TJ <- sample[sample$TJID1== i,]
p = ggplot(TJ, aes_string(x=var_list[[i]][1], y=var_list[[i]][2])) +
geom_point()+
geom_line()
plot_list[[i]] = p
}
但是,这从字面上只是没有给我什么。任何帮助表示赞赏!
你必须'打印(p)'。为什么不使用'facet_wrap(〜TJID1)'而不是循环? –