[R] par(new = TRUE) - overplotting

Dieter Menne dieter.menne at menne-biomed.de
Wed Oct 8 17:52:48 CEST 2008


Antje <niederlein-rstat <at> yahoo.de> writes:

> I want to create some boxplots (as png) within an lapply method. To get 
> nice  gridlines behind the boxplot, I plotted it twice and therefore I 
> set par(new=TRUE).
> This works nicely for the first plot but the second does plot on the 
> first plot 
> too and creates a mess...
> How can I force to start with a blank plot again???
> 
> lapply(c(1,2), FUN=function(i) {
> 	png(filename = "test.png", width = 450, height = 600)
> 	gridlines <- seq(0.1,2.0,0.1)
> 	par(mar=c(12, 4, 5, 2))
> 	bpars <- list(yaxt = "n", las = 2 )
> 	boxplot(mydata[i], pars= bpars )
> 	abline(h = gridlines, col="lightgray", lty=2)
> 	abline(h = 1, col="red", lwd=3)
> 	par(new=TRUE)
> 	boxplot(mydata[i], pars= bpars, main = "title")
> 	dev.off()
> })

I do not fully understand what you want to do, but in each case you overwrite
your files. Try something like:

png(filename = paste("test",i,".png"), width = 450, height = 600)

and think over again why you need the par(new=TRUE). And please, make your
examples self-running, for example by adding

mydata = rnorm(100)

even if you probably have more complex data.

Dieter



More information about the R-help mailing list