2016-05-29 118 views
2

以下代碼繪製了4條線圖,每個種類一個,每種處理顯示不同的線條(請參閱鏈接「Plot」)。將facet_wrap與for循環一起使用

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) 

圖不收斂。我認爲它與循環使用這個函數有關。

DATA:

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)) 
+2

你的代碼是遠遠不能播放的當前形式 – hrbrmstr

+0

@hrbrmstr我如何能清理有什麼建議? –

+1

for循環應該在'multiplot'之前結束。這是一個錯誤 – Divi

回答

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