Reputation: 113
I wanted to loop a piece of code that produces graph by ggplot2. My data set looks like the example "prot", juts prot is one accession and in the original data I have many more accessions. For single accession it looks nice. Just with the looping, I thought that it will place one iteration per one page in .pdf but it doesn't. This single plot already creates combined plots, so I don't know now how and where to place facet_wrap or facet_grid? or maybe there is other solution? help, please help.
'library(ggplot2)
ggplot(prot, aes(factor(genotype), value, fill = Light)) +
geom_bar(stat="identity", position = "dodge") +
scale_fill_brewer(palette = "Set1")
'> prot
Accession genotype variable value Light
966 AT1G01050 WT ML_WT_Dejan_05 219971.1 ML
2828 AT1G01050 WT ML_WT_Dejan_06 286308.6 ML
4690 AT1G01050 WT ML_WT_Dejan_14 1177873.5 ML
6552 AT1G01050 m ML_m_Dejan_08 861982.0 ML
8414 AT1G01050 m ML_m_Dejan_10 3786163.0 ML
10276 AT1G01050 m ML_m_Dejan_11 1289267.7 ML
12138 AT1G01050 f ML_f_Dejan_01 400419.3 ML
14000 AT1G01050 f ML_f_Dejan_04 929297.2 ML
15862 AT1G01050 f ML_f_Dejan_09 12245991.9 ML
17724 AT1G01050 ntrc ML_ntrc_Dejan_02 785773.5 ML
19586 AT1G01050 ntrc ML_ntrc_Dejan_03 971133.1 ML
21448 AT1G01050 ntrc ML_ntrc_dejan7 592207.0 ML
23310 AT1G01050 ntrc ML_ntrc_Dejan_12R 347127.5 ML
25204 AT1G01050 WT FL_WT_Dejan_20 131817.0 FL
27134 AT1G01050 WT FL_WT_Dejan_39 560424.7 FL
29064 AT1G01050 WT FL_WT_Dejan_33 9304183.7 FL
30994 AT1G01050 WT FL_WT_Dejan_34 647452.4 FL
32924 AT1G01050 m FL_m_Dejan_21 712381.5 FL
34854 AT1G01050 m FL_m_Dejan_26 6089158.8 FL
36784 AT1G01050 m FL_m_Dejan_28 11341334.1 FL
38714 AT1G01050 f FL_f_Dejan_19 13140258.2 FL
40644 AT1G01050 f FL_f_Dejan_31 11256554.9 FL
42574 AT1G01050 f FL_f_Dejan_35 1621509.9 FL
44504 AT1G01050 f FL_f_Dejan37 392228.2 FL
46434 AT1G01050 ntrc FL_ntrc_Dejan_30 9069074.8 FL
48364 AT1G01050 ntrc FL_ntrc_Dejan_38 562403.6 FL
50294 AT1G01050 ntrc FL_ntrc_Dejan29 175258.6 FL
79347 AT1G01050 WT LL_WT_Dejan_41 2443625.6 LL
81783 AT1G01050 WT LL_WT_Dejan_43 8529143.7 LL
84219 AT1G01050 WT LL_WT_Dejan_49 11054552.6 LL
86655 AT1G01050 m LL_m_Dejan_44 14325152.0 LL
89091 AT1G01050 m LL_m_Dejan_45 13114486.4 LL
91527 AT1G01050 m LL_m_Dejan_54 8250430.1 LL
93963 AT1G01050 f LL_f_Dejan_47 12431354.5 LL
96399 AT1G01050 f LL_f_Dejan_48 11884118.5 LL
98835 AT1G01050 f LL_f_Dejan_53 8408509.1 LL
101271 AT1G01050 ntrc LL_ntrc_Dejan_46 12214783.1 LL
103707 AT1G01050 ntrc LL_ntrc_Dejan_50 1286828.3 LL
106143 AT1G01050 ntrc LL_ntrc_Dejan_42 1819043.9 LL
plots<- list()
pdf("TEST_boxplot.pdf")
IDs<-unique(prot$Accession)
for (i in 1:length(IDs)){
temp <- prot[(prot$Accession)==IDs[i],]
p<- ggplot(temp, aes(factor(genotype), value, fill = Light)) +
geom_bar(stat="identity", position = "dodge") +
scale_fill_brewer(palette = "Set1")+
ggtitle(as.character(i))
plots[[i]] <- p
#plots[[paste(i)]] = p
#multiplot(plotlist = plots, cols = 1)
}
dev.off()
Upvotes: 0
Views: 83
Reputation: 24262
I generated a prot
toy dataset with two levels for Accession
.
The code below prints 2 graphs on two pages of the TEST_boxplot.pdf
file.
Here is the file generated.
library(ggplot2)
prot1 <- read.table(text="
n Accession genotype variable value Light
966 AT1G01050 WT ML_WT_Dejan_05 219971.1 ML
2828 AT1G01050 WT ML_WT_Dejan_06 286308.6 ML
4690 AT1G01050 WT ML_WT_Dejan_14 1177873.5 ML
6552 AT1G01050 m ML_m_Dejan_08 861982.0 ML
8414 AT1G01050 m ML_m_Dejan_10 3786163.0 ML
10276 AT1G01050 m ML_m_Dejan_11 1289267.7 ML
12138 AT1G01050 f ML_f_Dejan_01 400419.3 ML
14000 AT1G01050 f ML_f_Dejan_04 929297.2 ML
15862 AT1G01050 f ML_f_Dejan_09 12245991.9 ML
17724 AT1G01050 ntrc ML_ntrc_Dejan_02 785773.5 ML
19586 AT1G01050 ntrc ML_ntrc_Dejan_03 971133.1 ML
21448 AT1G01050 ntrc ML_ntrc_dejan7 592207.0 ML
23310 AT1G01050 ntrc ML_ntrc_Dejan_12R 347127.5 ML
25204 AT1G01050 WT FL_WT_Dejan_20 131817.0 FL
27134 AT1G01050 WT FL_WT_Dejan_39 560424.7 FL
29064 AT1G01050 WT FL_WT_Dejan_33 9304183.7 FL
30994 AT1G01050 WT FL_WT_Dejan_34 647452.4 FL
32924 AT1G01050 m FL_m_Dejan_21 712381.5 FL
34854 AT1G01050 m FL_m_Dejan_26 6089158.8 FL
36784 AT1G01050 m FL_m_Dejan_28 11341334.1 FL
38714 AT1G01050 f FL_f_Dejan_19 13140258.2 FL
40644 AT1G01050 f FL_f_Dejan_31 11256554.9 FL
42574 AT1G01050 f FL_f_Dejan_35 1621509.9 FL
44504 AT1G01050 f FL_f_Dejan37 392228.2 FL
46434 AT1G01050 ntrc FL_ntrc_Dejan_30 9069074.8 FL
48364 AT1G01050 ntrc FL_ntrc_Dejan_38 562403.6 FL
50294 AT1G01050 ntrc FL_ntrc_Dejan29 175258.6 FL
79347 AT1G01050 WT LL_WT_Dejan_41 2443625.6 LL
81783 AT1G01050 WT LL_WT_Dejan_43 8529143.7 LL
84219 AT1G01050 WT LL_WT_Dejan_49 11054552.6 LL
86655 AT1G01050 m LL_m_Dejan_44 14325152.0 LL
89091 AT1G01050 m LL_m_Dejan_45 13114486.4 LL
91527 AT1G01050 m LL_m_Dejan_54 8250430.1 LL
93963 AT1G01050 f LL_f_Dejan_47 12431354.5 LL
96399 AT1G01050 f LL_f_Dejan_48 11884118.5 LL
98835 AT1G01050 f LL_f_Dejan_53 8408509.1 LL
101271 AT1G01050 ntrc LL_ntrc_Dejan_46 12214783.1 LL
103707 AT1G01050 ntrc LL_ntrc_Dejan_50 1286828.3 LL
106143 AT1G01050 ntrc LL_ntrc_Dejan_42 1819043.9 LL
", header=T)
prot2 <- prot1
prot2$Accession <- "AT3G53620"
prot <- rbind(prot1,prot2)
plots <- list()
pdf("TEST_boxplot.pdf", onefile=T)
IDs<-unique(prot$Accession)
for (i in 1:length(IDs)){
temp <- prot[(prot$Accession)==IDs[i],]
p<- ggplot(temp, aes(factor(genotype), value, fill = Light)) +
geom_bar(stat="identity", position = "dodge") +
scale_fill_brewer(palette = "Set1")+
ggtitle(as.character(i))
plots[[i]] <- p
print(p)
}
dev.off()
Upvotes: 1