[R] Multiple use of par()

Hesen Peng hesen.peng at gmail.com
Sat Apr 4 16:15:44 CEST 2009


Hi,

Thanks a lot for reminding me of this. The original code is too
complicated and stems from several other objects. So I guess this
simplified code may help:

a <- rnorm(100)
class(a) <- "foo"

plot.foo <- function(data){
##  opar<-par()
  par(mfcol=c(1,2))
  hist(data)
  boxplot(data)
##  par(mfcol=c(1,1))
}

par(mfcol=c(2,2))
plot(a)
plot(a)
plot(a)
plot(a)

I'm hoping to have 2x2 plots of the plot.foo result. But the par()
argument above seem to be overwriting each other. I tried the code
suggested by Erin ( now in commented area). But it doesn't seem to
work, either. Anyone has any suggestions? Thanks a lot,

Best wishes,

On Sat, Apr 4, 2009 at 6:06 AM, Dieter Menne
<dieter.menne at menne-biomed.de> wrote:
>
>
> Hesen Peng-2 wrote:
>>
>> I created a plot function which used par(mfcol=c(2,1)) so that I could
>> have two plots together using just one command.
>>
>> For exampe:
>>
>> plot.foo <- function(data){
>>  par(mfcol=c(2,1))
>>  hist(data)
>>  plot(data)
>> }
>>
>> Later I wanted to show 4 of these foo objects in the same picture. So
>> I used par(mfcol=c(2,2)) again at the beginning of the code like:
>>
>> par(mfcol=c(2,2))
>> plot(foo.1)
>> plot(foo.2)
>> plot(foo.3)
>> plot(foo.4)
>>
>> but this time the par() command inside of the functions seem to be
>> overwriting the par() command at the very begining. Can anyone please
>> give me some advise on dealing with this? I guess that I may either
>> need to change the way I plot foo, e.g. using some function rather
>> than par(), or use some parameters at the beginning. Thank you very
>> much,
>>
>
> Your example starts fine, but does not run because is it unclear what foo.1
> etc. means. Please really post complete examples, chances are higher you get
> a reasonable answer.
>
> Reading between the lines, I suspect that you mixed up the concepts of
> trellis plots with those of standard plot(). I think you believed that your
> function returns the plot object, which is approximately true for trellis
> where you could use a list of graphics objects and print() or plot() these
> later in a given arrangement with split().
>
> As an easy solution with standard graphics, I suggest the not-so-elegant one
> below. You should probably adjust the margins a bit to make clear that
> graphs are pairs.
>
> Dieter
>
> data = rnorm(100)
> plot.foo <- function(data){
> # par(mfcol=c(2,1))
>  hist(data)
>  plot(data)
> }
>
>
> par(mfcol=c(4,2))
> plot.foo(data)
> plot.foo(data) # Use other data here
> plot.foo(data)
> plot.foo(data)
>
>
> --
> View this message in context: http://www.nabble.com/Multiple-use-of-par%28%29-tp22876693p22881832.html
> Sent from the R help mailing list archive at Nabble.com.
>
> ______________________________________________
> R-help at 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.
>



-- 
彭河森 Hesen Peng
http://hesen.peng.googlepages.com/




More information about the R-help mailing list