0

I am outputting plots as png based on grouping according to the dataframe vector called "chr". This generates lots of plots but I would like to have them all in one png. I am using the plot function in r rather than ggplot2.

My code so far:

for(jj in ind){
png(paste("/Users/sebastianzeki/Desktop/SequencingScripts/Plots/",jj,".png"))
indic = which(ret$chr == jj)
plot(ret$binRight[indic],ret$SummedZScore[indic],pch=19,xlab="Locus",ylab="Summed    ZScore",type="h",lwd=20, space=0)

dev.off()

How can I get all the plots on one png (or pdf if thats easier)?

Sebastian Zeki
  • 6,690
  • 11
  • 60
  • 125

1 Answers1

1

Suppose length(ind) = 10

png(paste("/Users/sebastianzeki/Desktop/SequencingScripts/Plots/",jj,".png"))
par(mfrow=c(5,2))

for(jj in ind){
  indic = which(ret$chr == jj)
  plot(ret$binRight[indic],ret$SummedZScore[indic],pch=19,xlab="Locus",ylab="Summed ZScore",type="h",lwd=20, space=0)
}
dev.off()

This can make one png file or if you want to make a pdf file

How to print R graphics to multiple pages of a PDF and multiple PDFs?

Look at the above thread for help.


A simple example :

png("temp.png", width = 600, height = 2000)
par(mfrow=c(8,3), mar = rep(0.5, 4), oma = rep(0.5, 4))

for (i in 1:24) {
  hist(runif(20), main = NULL)
}
dev.off()
Community
  • 1
  • 1
won782
  • 696
  • 1
  • 4
  • 13
  • So I have about 25 plots- although your method works it seems to squash all the plots it can onto one page and it cant display any more than 5 even in a single column of plots. Is there another way- allow the page size to be as big as the number of plots – Sebastian Zeki Aug 07 '14 at 16:17
  • 1
    @user3632206 I would recommend the following if you want to keep as png. Set margin by mar = c(a,b,c,d), oma = c(a,b,c,d), which is a vector of inner and outer margin of plots. Then simply export to larger png resoultion, which can be supplied to png() as an argument. (with width = 0000, height = 0000). – won782 Aug 07 '14 at 16:20
  • OK thanks. The png doesnt seem any larger and all the graphs are cramped on to one page. Is there a way of having a long png that accomodates as many graphs as I would like? height= doesnt seem to change anything – Sebastian Zeki Aug 07 '14 at 21:03
  • 1
    That is weird. I actually edited above code so that png() is called first and dev.off() is called at the end to store it. Try run above example and it should produce 600 by 2000 pixel output. – won782 Aug 07 '14 at 22:01
  • OK great, so in the end I used this code which seemed to work: ind = unique(as.character(df$chr)) png("/Users/sebastianzeki/Desktop/SequencingScripts/Plots/temp.png", width = 600, height = 2000) par(mfrow=c(8,3), mar = rep(0.5, 4), oma = rep(0.5, 4)) for(jj in ind){ indic = which(ret$chr == jj) plot(ret$binRight[indic],ret$SummedZScore[indic],pch=19,xlab="Locus",ylab="Summed ZScore",type="h",main=jj,lwd=2) } dev.off() – Sebastian Zeki Aug 08 '14 at 07:03