2011-12-17 15 views
3

我创建了一个带有两个y轴的图形,并使用mtext()来标记右轴。用多幅土地的比例尺文字绘制?

# 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) 

这个工程本身很好。但是,如果你把这个与多条曲线的人物:

# 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) 

这里,左手y轴的标签变小,而右侧轴没有。

我知道这种行为的根源是,在mtext()cex参数不是相对于par("cex");我想要的是解决这个问题。

+1

用`par(mfrow = ...)`做多个图是一个痛苦的世界。尝试使用`ggplot2`或`lattice`库。 – 2011-12-17 01:27:17

回答

2

解决此问题的最佳方法是使用par()$cex属性。所以你的代码是:

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) 
+0

谢谢!奇迹般有效。 – ChadBDot 2011-12-17 02:52:24