Hi Dieter,

thanks a lot for looking inside my code though it was not executable...(sorry for that). Finally, I found a rather stupid mistake. My original code did not use the variable i for the second boxplot. So the second round actually plotted two different data at the two calls...
so it had nothing to do with the par(new...)

here some executable bug-free code :-)



mydata <- list(rnorm(100, mean=0.5, sd=0.1), rnorm(100, mean=0.7, sd=0.15))

lapply(c(1,2), FUN=function(i) {
        windows(7,7)
        use_col <- c("blue")
        gridlines <- seq(0.1,2.0,0.1)
        par(mar=c(12, 4, 5, 2))
        bpars <- list(yaxt = "n", las = 2 )
        boxplot(data.frame(mydata[[i]]), col=use_col, pars= bpars )
        abline(h = gridlines, col="lightgray", lty=2)
        abline(h = 1, col="red", lwd=3)
        par(new=TRUE)
        boxplot(data.frame(mydata[i]), col=use_col, pars= bpars)
})

ciao,
Antje





Dieter Menne schrieb:
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

______________________________________________
R-help@r-project.org mailing list
https://stat.ethz.ch/mailman/listinfo/r-help
PLEASE do read the posting guide http://www.R-project.org/posting-guide.html
and provide commented, minimal, self-contained, reproducible code.


______________________________________________
R-help@r-project.org mailing list
https://stat.ethz.ch/mailman/listinfo/r-help
PLEASE do read the posting guide http://www.R-project.org/posting-guide.html
and provide commented, minimal, self-contained, reproducible code.

Reply via email to