Whitehot
Whitehot

Reputation: 497

GGPlot2 called twice inside a function only produces one plot

I am fairly new to ggplot2, so this may be a simple mistake that I am not aware of.

I am using this function to create violin plots of some data of mine. The data in question is Log fold change between two conditions of ChIP-seq reads for two proteins which we suspect follow RNAPII during transcription, but I don't think it's really relevant. The only thing is, I'd like the function to create two plots: one for genes that show an increase of RNAPII in the treatment condition, and one for genes that show a decrease.

Vioplot_LogFC = function(quant1, quant2, f, info = c("Quantif 1", "Quantif 2", "Factor")){


quant1_s = sample(quant1, length(f$top))
quant1_f = quant1[f$top]
quant2_s = sample(quant2, length(f$top))
quant2_f = quant2[f$top]

LogFC = c(quant1_s, quant1_f, quant2_s, quant2_f)
Factor = c(rep(info[1], length(quant1_s)+length(quant1_f)), rep(info[2], length(quant2_s)+length(quant2_f)))
Group = c(rep("Random", length(quant1_s)), rep("PolII increase", length(quant1_f)), rep("Random", length(quant2_s)), rep("PolII increase", length(quant2_f)))

toplot=data.frame(LogFC, Factor, Group)

p1 = floor(-1*log10(wilcox.test(quant1_f, quant1_s)$p.value))
p2 = floor(-1*log10(wilcox.test(quant2_f, quant2_s)$p.value))

label1 = paste0(info[1],"; p<10^-", p1)
label2 = paste0(info[2],"; p<10^-", p2)

plot1 = ggplot(toplot, aes(x=Factor, y=LogFC, fill=Group)) +
    geom_violin() +
    scale_x_discrete(labels=c(label1, label2)) +
    scale_y_continuous(name="Log Fold Change") +
    scale_fill_discrete(name="Gene Group") +
    theme_bw()

##################################
##################################
##################################

quant1_s = sample(quant1, length(f$bot))
quant1_f = quant1[f$bot]
quant2_s = sample(quant2, length(f$bot))
quant2_f = quant2[f$bot]

LogFC = c(quant1_s, quant1_f, quant2_s, quant2_f)
Factor = c(rep(info[1], length(quant1_s)+length(quant1_f)), rep(info[2], length(quant2_s)+length(quant2_f)))
Group = c(rep("Random", length(quant1_s)), rep("PolII decrease", length(quant1_f)), rep("Random", length(quant2_s)), rep("PolII decrease", length(quant2_f)))

toplot=data.frame(LogFC, Factor, Group)

p1 = floor(-1*log10(wilcox.test(quant1_f, quant1_s)$p.value))
p2 = floor(-1*log10(wilcox.test(quant2_f, quant2_s)$p.value))

label1 = paste0(info[1],"; p<10^-", p1)
label2 = paste0(info[2],"; p<10^-", p2)
plot2 = ggplot(toplot, aes(x=Factor, y=LogFC, fill=Group)) +
    geom_violin() +
    scale_x_discrete(labels=c(label1, label2)) +
    scale_y_continuous(name="Log Fold Change") +
    scale_fill_discrete(name="Gene Group") +
    theme_bw()

plot1
plot2


}

I always use this function inside pdf(...) dev.off(), and yet the pdf contains only one plot.

Is it some issue due to using the same data & names twice? Is there a clear() function I should use after creating the first plot to be able to create a new one?

Upvotes: 0

Views: 224

Answers (1)

Michael Roswell
Michael Roswell

Reputation: 1482

As @StupidWolf noted, R functions will return the last line by default (and not previous objects). You can adjust your function to return whatever you want, though, using return.

ggplot objects may be stored in a list and retrieved. Thus instead of

plot1
plot2

in which case only plot2 is returned, you could end your function with

 return(list(plot1, plot2))

Now the output of your function is a list with 2 elements, plot1 and plot2. as @StupidWolf noted, you can print and save these plots by calling them from the list using the double square brackets, so if you did

 PLOTS<-Vioplot_logFC(...)

you could retrieve, print, or manipulate plot1 using

 PLOTS[[1]]]

As an example of "manipulate" you could change the "theme" post-hoc, e.g.

 PLOTS[[1]]+theme_classic() 

Upvotes: 4

Related Questions