Skip to content

Multiple use of par()

5 messages · Dieter Menne, Hesen Peng, jim holtman

#
Hi all,

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,

Best wishes,
#
Hesen Peng-2 wrote:
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)
#
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:

  
    
#
To get what you want with the basic graphics, you probably want
something like this (tuned to you data):

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(4,2))

plot(a)
plot(a)
plot(a)
plot(a)
On Sat, Apr 4, 2009 at 10:15 AM, Hesen Peng <hesen.peng at gmail.com> wrote:

  
    
#
Hesen Peng wrote:
This comes close to the code I suggested. What you want sounds reasonable,
but old style graphics was not build to easily handle the nested situation
you want. May I suggest to use trellis graphics for this? It gives much
nicer pictures, and it is build on grid, which is nothing else than the
nesting idea (of viewport) carried to the glorious end.

Dieter