使用带有for循环的facet_wrap

时间:2016-05-29 03:13:23

标签: r plot ggplot2 facet-wrap

以下代码绘制了4个线图,每个物种一个,每个处理显示不同的线(参见链接“Plot”)。

library(ggplot2)
library(Rmisc)
library(reshape2)
melt <- melt(allcl, id=c("TREATMENT", "TIME"), value.name="Surface_area", variable.name = "Species")
x <- summarySE(melt, measurevar = "Surface_area", groupvars = c("TIME", "TREATMENT", "Species") )
p<-list()
for(i in 3:ncol(allcl)){
  graph <- subset(x, Species == colnames(allcl)[i])
p[[i]]<- ggplot(graph, aes(x=TIME, y=Surface_area, group=TREATMENT,colour=TREATMENT)) +
geom_line(size=1) +
facet_grid(~Species)+
geom_point()+
geom_errorbar(aes(ymin=Surface_area-se, ymax=Surface_area+se), width=.2,
            position=position_dodge(0.05))+
ggtitle(unique(graph$Species))+
theme_classic()+
theme(panel.border = element_blank(),
      axis.line.x = element_line(size = 0.5, linetype = "solid", colour = "black"),
      axis.line.y = element_line(size = 0.5, linetype = "solid", colour = "black"),
      axis.text.x  = element_text(angle=0, vjust=0.5))+
scale_x_continuous(breaks=seq(0,2,1),
                 labels=c("T0", "T1","T2"))
}
multiplot(p[[3]], p[[4]], p[[5]], p[[6]], 
      cols=2) 

Plots

然而,由于它们共享相同的轴,我想使用facet_wrap在组合轴上绘制它们。问题是当我添加

+ facet_wrap(~Species)

图表不会收敛。我认为它与循环使用此函数有关。

数据:

structure(list(TREATMENT = structure(c(1L, 1L, 1L, 2L, 2L, 2L, 
3L, 3L, 3L, 4L, 4L, 4L, 1L, 1L, 1L, 2L, 2L, 2L, 3L, 3L, 3L, 4L, 
4L, 1L, 1L, 1L, 2L, 2L, 2L, 3L, 3L, 3L, 4L, 4L), 
.Label = c("TREAT1", "TREAT2", "TREAT3", "TREAT4"), class = "factor"), 
TIME = c(0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 1L, 1L, 1L, 1L, 1L, 
1L, 1L, 1L, 1L, 1L, 1L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 
2L), Species1 = c(32.57575758, 36.79525223, 29.47368421, 26.51757188, 
32.73542601, 22.56097561, 38.94080997, 28.93772894, 32.47863248, 
38.44221577, 43.40425532, 33.48017621, 64.17112299, 53.40314136, 
46.88995215, 44.12811388, 60.58823529, 58.05243446, 70.24793388, 
69.91525424, 74.13793103, 78.88198758, 60.40268456, 77.27272727, 
70.52631579, 63.28125, 61.14285714, 68.42105263, 70.90909091, 
91.38576779, 98.80597015, 99, 87.12871287, 80.14705882), 
Species2 = c(29.48328267, 28.82352941, 30.74433657, 42.85714286, 30.56379822, 
31.46417445, 21.77121771, 32.89036545, 24.7311828, 33.62689661, 33.99339934, 
33.26039387, 39.1025641, 39.61352657, 45, 32.95454545, 40, 59.80392157, 
61.74863388, 63.30275229, 68.6440678, 48.87892377, 56.15942029, 
34.30656934, 51.51515152, 57.95454545, 42.59259259, 31.64556962, 
47.52475248, 85.16129032, 97.07112971, 100, 60.62176166, 55.17241379), 
Species3 = c(45.16129032, 25.72178478, 32.05479452, 25.22255193, 
28.9276808, 44.09937888, 25.44378698, 43.08510638, 43.24324324, 
26.07313196, 26.47058824, 25.67567568, 69.87951807, 35.51020408, 
39.86486486, 45.03546099, 41.01796407, 67.37288136, 47.24137931, 
75.87939698, 75.55555556, 50.93333333, 42.08754209, 68.87966805, 
45.39473684, 53.53535354, 49.25373134, 40.38461538, 47.48201439, 
81.96078431, 99.13043478, 92.68292683, 70.42253521, 41.93548387), 
Species4 = c(41.05263158, 42.31974922, 31.34920635, 44.56521739, 
41.57782516, 30.44982699, 43.24324324, 34.21052632, 28.45188285, 
36.91508875, 38.40304183, 35.42713568, 72.72727273, 73.75886525, 
62.38095238, 56.50969529, 62.80487805, 81.32780083, 66.66666667, 
82.57261411, 80.43478261, 73, 64.375, 83.52272727, 83.58208955, 
88.49206349, 78.89908257, 59.14634146, 87.82608696, 88, 98.8317757, 
97.61904762, 82.60869565, 61.99376947)), 
.Names = c("TREATMENT", "TIME", "Species1", "Species2", "Species3", "Species4"), 
class = "data.frame", row.names = c(NA, -34L))

1 个答案:

答案 0 :(得分:4)

一旦您melt数据集并生成摘要统计信息,您就可以在不使用for循环的情况下绘制它。

p <- ggplot(x, aes(x=TIME, y=Surface_area, group=TREATMENT,colour=TREATMENT)) +
  geom_line(size=1) +
  facet_wrap(~Species)+
  geom_point()+
  geom_errorbar(aes(ymin=Surface_area-se, ymax=Surface_area+se), width=.2,
                position=position_dodge(0.05))+
  ggtitle(unique(graph$Species))+
  theme_classic()+
  theme(panel.border = element_blank(),
        axis.line.x = element_line(size = 0.5, linetype = "solid", colour = "black"),
        axis.line.y = element_line(size = 0.5, linetype = "solid", colour = "black"),
        axis.text.x  = element_text(angle=0, vjust=0.5))+
  scale_x_continuous(breaks=seq(0,2,1),
                     labels=c("T0", "T1","T2"))
p

enter image description here