Reputation: 3148
I've created a figure with two y-axes, and use mtext() to label the right-hand axis.
# generate some data to plot
x <- 1:5
y1 <- rnorm(5)
y2 <- rnorm(5,20)
# set margins
par(mar=c(5,4,4,5)+.1)
# plot first x/y line
plot(x,y1,type="l",col="red")
#plot second x/y line
par(new=TRUE)
plot(x, y2,,type="l",col="blue",xaxt="n",yaxt="n",xlab="",ylab="")
axis(4)
mtext("y2",side=4,line=3)
This works great by itself. However, if you place this into a figure with multiple plots:
# create a 3x3 figure for multiple plots
par(mfrow = c(3, 3))
# generate some data to plot
x <- 1:5
y1 <- rnorm(5)
y2 <- rnorm(5,20)
# set margins
par(mar=c(5,4,4,5)+.1)
# plot first x/y line
plot(x,y1,type="l",col="red")
#plot second x/y line
par(new=TRUE)
plot(x, y2,,type="l",col="blue",xaxt="n",yaxt="n",xlab="",ylab="")
axis(4)
mtext("y2",side=4,line=3)
Here, the left-hand y-axis label gets smaller, while the right-hand axis does not.
I am aware that the source of this behavior is that the cex
parameter in mtext()
is NOT relative to par("cex")
; what I'd like is some way around this.
Upvotes: 3
Views: 1284
Reputation: 1594
The best way to solve this is to use the par()$cex
attribute. So your code would be:
x <- 1:5
y1 <- rnorm(5)
y2 <- rnorm(5,20)
par(mfrow = c(3, 3), mar=c(5,4,4,5)+.1)
plot(x,y1,type="l",col="red")
par(new=TRUE)
plot(x, y2,,type="l",col="blue",xaxt="n",yaxt="n",xlab="",ylab="")
axis(4)
mtext("y2",side=4,line=3, cex=par()$cex)
Upvotes: 2