在R中为辅助轴添加标签

tra*_*mot 2 r axis-labels

我有这个代码:

# Plotting everything
plot( p1, col= "lightgreen", xlim=c(-2.5,4.5), ylim=c(0, 700), main="Daily Total Precipitation for AR and Oct-May", xlab="ln(x)" , ylab="Frequency", xaxt = "n")  # first histogram
plot( p2, col="red", xlim=c(-2.5,4.5), ylim=c(0, 700), xaxt = "n" , add=T)
# Adding in text labels on top of the bars
text(x, y, paste(round(percents,2),"%"), cex=0.50, pos=3, offset=0.3, col="black")
axis(side=1, at=breaks)     # new x-axis
# parameter that needs to be set to add a new graph on top of the other ones
par(new=T)
plot(x, percents, xlim=c(-2.5,4.5), type="l", col="yellow", lwd=3.0, axes=F, ylab=NA, xlab=NA)
axis(side=4, at=seq(0,100,by=10), col="yellow", col.axis="yellow")     # additional y-axis
mtext("Percent", side=4, col="yellow")
# legend settings
legend("topleft", c("AR", "Oct-May"), lwd=10, col=c("red", "lightgreen"))
Run Code Online (Sandbox Code Playgroud)

这产生了这个图:

在此输入图像描述

我似乎无法弄清楚如何让二级y轴标签显示在正确的位置.非常感谢任何帮助或建议.

编辑:使用RStudio.

Rei*_*son 7

一种选择是指定line参数mtext().在下面的示例中,我使用了更多的行添加到side = 4绘图的右边()边缘par(),然后使用mtext()默认(line = 0),第3行(line = 3)和第-3 行()绘制三个标签line = -3:

op <- par(mar = c(5,4,4,4) + 0.1)
plot(1:10)
mtext("line0", side = 4)
mtext("line3", side = 4, line = 3)
mtext("line-3", side = 4, line = -3)
par(op)
Run Code Online (Sandbox Code Playgroud)

在此输入图像描述

请注意,行数远离绘图区域增加,负值line移动到绘图区域,或绘图区域右边界的左侧.

它需要稍微玩一下边缘线的数量(如设置par(mar = x))以及你想要使用哪条线mtext(),但是一点试验和错误应该可以得到你想要的.

还需要注意的是,你并不需要指定整数的价值观line争论.您也可以指定行的分数:line = 2.5.