[R] small first graph of par(3,2), other 5 are correct

Scot W McNary smcnary at charm.net
Mon Jul 11 18:43:37 CEST 2005


Hi,

I'm trying to produce 6 graphs on a single page using code I've borrowed 
from an example by Paul Murrell:

(http://www.stat.auckland.ac.nz/~paul/RGraphics/custombase-xmastree.R).

It involves placing 6 horizontal barplots on one page and adding common 
labels.

The problem is the first graph in my figure (the one in the (1,1) 
position) is smaller than the other 5.  A toy example is included below.

When I compare the par() options set after producing each graph, all of 
the parameters that change look like what I would expect:

> names(after.g1[after.g1%in%after.g2=="FALSE"])
[1] "fig" "mai" "mar" "mfg" "plt"

> what.chgd<-names(after.g1[after.g1%in%after.g2=="FALSE"])
>
> after.g1[what.chgd]
$fig
[1] 0.0000000 0.5000000 0.6666667 1.0000000

$mai
[1] 0.0309375 0.0618750 0.3093750 0.3093750

$mar
[1] 0.5 1.0 5.0 5.0

$mfg
[1] 1 1 3 2

$plt
[1] 0.02357143 0.88214285 0.01437097 0.85629032

>
> after.g2[what.chgd]
$fig
[1] 0.5000000 1.0000000 0.6666667 1.0000000

$mai
[1] 0.0309375 0.3093750 0.3093750 0.0618750

$mar
[1] 0.5 5.0 5.0 1.0

$mfg
[1] 1 2 3 2

$plt
[1] 0.11785715 0.97642857 0.01437097 0.85629032



One other aspect of this is that the problem does not occur if after I 
create all 6 graphs once, I rerun the code again, but omit only the 
"par(mfrow=c(3,2))" statement.  If I don't reset par(), then the first 
graph is then the identical size of the other five.

Up to now I've gotten around this by simply running the code twice, 
omitting the "par(mfrow=c(3,2))" the second time and printing the result. 
Now, however, I'm sending the graphs to a pdf file and must set the 
"par(mfrow=c(3,2))" after opening the pdf device, so the problem shows up 
with each time.

Any help on fixing this problem so that all 6 graphs appear the same size 
on the figure would be most welcome.

Thanks,

Scot


####


Here is the toy example that shows the layout and plots in my figure:

par(mfrow=c(3,2))

groups<-LETTERS[1:5]

### for graph 1 ###

# data
leftci <- c(1:5)
rightci <- c(2:6)

# left column graph so:
par(mar=c(0.5, 1, 5, 5))
# right column graph so:
#par(mar=c(0.5, 5, 5, 1))

plot.new()
title(main = "Graph 1")
plot.window(xlim=c(0, 8), ylim=c(-1.5, 5.5))

ticks <- seq(0, 8, 1)
y <- 1:5  # how many spaces on y axis: 1 for each group
h <- 0.2  # height?  a function of y?

segments(0, y, 8, y, lty="dotted")  # dotted segments on which bar lies 
(like a grid)
rect(leftci, y-h, rightci, y+h, col="dark grey")
#mtext(groups, at=y, adj=1, side=2, las=2, cex=.75)
par(cex.axis=1.0, mex=0.5)
axis(1, at=ticks, labels=abs(ticks), pos=0, )

box("inner", col="grey")

after.g1<-show(par())

### for graph 2 ###

# data:
leftci <- c(1:5)
rightci <- c(2:6)

# left column graph so:
#par(mar=c(0.5, 1, 5, 5))
# right column graph so:
par(mar=c(0.5, 5, 5, 1))

plot.new()
title(main = "Graph 2")

plot.window(xlim=c(0, 8), ylim=c(-1.5, 5.5))

ticks <- seq(0, 8, 1)
y <- 1:5  # how many spaces on y axis: 1 for each group
h <- 0.2  # height?  a function of y?

segments(0, y, 8, y, lty="dotted")  # dotted segments on which bar lies 
(like a grid)
rect(leftci, y-h, rightci, y+h, col="dark grey")
mtext(groups, at=y, adj=.5, side=2, las=2, cex=.75, line = 5) # this line 
only for right column graphs
par(cex.axis=1.0, mex=0.5)
axis(1, at=ticks, labels=abs(ticks), pos=0, )

box("inner", col="grey")

# end graph 2

after.g2<-show(par())

### graph 3###

# data
leftci <- c(1:5)
rightci <- c(2:6)

# left column graph so:
par(mar=c(0.5, 1, 5, 5))
# right column graph so:
#par(mar=c(0.5, 5, 5, 1))

plot.new()
title(main = "Graph 3")
plot.window(xlim=c(0, 8), ylim=c(-1.5, 5.5))

ticks <- seq(0, 8, 1)
y <- 1:5  # how many spaces on y axis: 1 for each group
h <- 0.2  # height?  a function of y?

segments(0, y, 8, y, lty="dotted")  # dotted segments on which bar lies 
(like a grid)
rect(leftci, y-h, rightci, y+h, col="dark grey")
#mtext(groups, at=y, adj=1, side=2, las=2, cex=.75)
par(cex.axis=1.0, mex=0.5)
axis(1, at=ticks, labels=abs(ticks), pos=0, )

box("inner", col="grey")

after.g3<-show(par())

### for graph 4 ###

# data:
leftci <- c(1:5)
rightci <- c(2:6)

# left column graph so:
#par(mar=c(0.5, 1, 5, 5))
# right column graph so:
par(mar=c(0.5, 5, 5, 1))

plot.new()
title(main = "Graph 4")

plot.window(xlim=c(0, 8), ylim=c(-1.5, 5.5))

ticks <- seq(0, 8, 1)
y <- 1:5  # how many spaces on y axis: 1 for each group
h <- 0.2  # height?  a function of y?

segments(0, y, 8, y, lty="dotted")  # dotted segments on which bar lies 
(like a grid)
rect(leftci, y-h, rightci, y+h, col="dark grey")
mtext(groups, at=y, adj=.5, side=2, las=2, cex=.75, line = 5) # this line 
only for right column graphs
par(cex.axis=1.0, mex=0.5)
axis(1, at=ticks, labels=abs(ticks), pos=0, )

box("inner", col="grey")

after.g4<-show(par())

# end graph 4

### graph 5###

# data
leftci <- c(1:5)
rightci <- c(2:6)

# left column graph so:
par(mar=c(0.5, 1, 5, 5))
# right column graph so:
#par(mar=c(0.5, 5, 5, 1))

plot.new()
title(main = "Graph 5")
plot.window(xlim=c(0, 8), ylim=c(-1.5, 5.5))

ticks <- seq(0, 8, 1)
y <- 1:5  # how many spaces on y axis: 1 for each group
h <- 0.2  # height?  a function of y?

segments(0, y, 8, y, lty="dotted")  # dotted segments on which bar lies 
(like a grid)
rect(leftci, y-h, rightci, y+h, col="dark grey")
#mtext(groups, at=y, adj=1, side=2, las=2, cex=.75)
par(cex.axis=1.0, mex=0.5)
axis(1, at=ticks, labels=abs(ticks), pos=0, )

box("inner", col="grey")

after.g5<-show(par())

### for graph 6 ###

# data:
leftci <- c(1:5)
rightci <- c(2:6)

# left column graph so:
#par(mar=c(0.5, 1, 5, 5))
# right column graph so:
par(mar=c(0.5, 5, 5, 1))

plot.new()
title(main = "Graph 6")

plot.window(xlim=c(0, 8), ylim=c(-1.5, 5.5))

ticks <- seq(0, 8, 1)
y <- 1:5  # how many spaces on y axis: 1 for each group
h <- 0.2  # height?  a function of y?

segments(0, y, 8, y, lty="dotted")  # dotted segments on which bar lies 
(like a grid)
rect(leftci, y-h, rightci, y+h, col="dark grey")
mtext(groups, at=y, adj=.5, side=2, las=2, cex=.75, line = 5) # this line 
only for right column graphs
par(cex.axis=1.0, mex=0.5)
axis(1, at=ticks, labels=abs(ticks), pos=0, )

box("inner", col="grey")

after.g6<-show(par())

# end graph 6




> version
          _
platform i386-pc-mingw32
arch     i386
os       mingw32
system   i386, mingw32
status
major    2
minor    1.1
year     2005
month    06
day      20
language R




--
   Scot W. McNary  email:smcnary at charm.net




More information about the R-help mailing list