I use this example dataset :
gene smp1_A smp1_B smp2_A smp2_B smp3_A smp3_B smp4_A smp4_B
geneA 10 12 30 33 26 22 44 42
geneB 15 13 11 16 15 16 21 26
I would like to plot smp1_A
vs smp1_B
, smp2_A
vs smp2_B
... = 4 plots
I would like to have a PDF with 2 pages, on the first page plot1
and plot2
and on the 2nd page plot3
et and plot4
.
(Of course, I have a lot more plots in my real dataset).
library(ggplot2)
library(ggpubr)
data = read.table('test_data.txt',header=T)
samples = list('smp1','smp2','smp3','smp4')
for (i in 1:length(samples)){
smp = samples[i]
smpA = paste(smp,"A",sep="_")
smpB = paste(smp,"B",sep="_")
plot = ggplot(data, aes(x=data[,smpA], y=data[,smpB])) + geom_point()
# I can't add the plot to a PDF in a loop, I have to generate it at the end
# so I need to create a new variable each iteration to not overwrite the previous one
# I do it with assign
nam <- paste("plot", i, sep = "")
assign(nam, plot)
}
# at this point, if I try to plot my 4 plots separately, it's working fine.
# I have this 4 variables in my env : plot1, plot2, plot3, plot4
# But now when I try to create my PDF I get 4 times the same plot and I can't figure out which one is it.
page1 = ggarrange(plot1,plot2, ncol=2, nrow=1)
page2 = ggarrange(plot3,plot4, ncol=2, nrow=1)
plots = list(page1, page2)
pdf('test_plots.pdf')
plots
dev.off()
Like I said inside my code, when I print my plots separately it's working, but when I combine them in a PDF I have 4 times the same plot.
I don't understand where is my mistake.