Quantcast
Channel: Active questions tagged r - Stack Overflow
Viewing all articles
Browse latest Browse all 206126

GGPlot2 called twice inside a function only produces one plot

$
0
0

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?


Viewing all articles
Browse latest Browse all 206126

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>