r - 使用为多个图缩放的边距文本绘制?

标签 r plot

我创建了一个具有两个 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 轴标签变小了,而右侧的轴则没有。

我知道这种行为的根源是 cex mtext() 中的参数与 par("cex") 无关;我想要的是解决这个问题。

最佳答案

解决这个问题的最好方法是使用 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)

关于r - 使用为多个图缩放的边距文本绘制?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/8541919/

相关文章:

R:为什么我在将列转换为因子后没有得到类型或类 "factor"?

r - R中的符号导数和简化

r - 无法将 R 连接到 MonetDB (MonetDB.R)

python - 如何在matplotlib中设置轴乘数的值?

matlab - MATLAB 中 voronoi 图的彩色无界单元

r - 错误消息 : Error in fn(x, ...):已更新的 VtV 不是正定的

r - 在 R 中使用约束

matlab - 我怎样才能让我的堆栈溢出?

matlab - 在matlab中用不同颜色在同一个图中绘制多个直方图

r - R 中可以绘制有理函数吗?