r - 将标签添加到 R 中的次轴

标签 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"))

生成此图:

enter image description here

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

编辑:使用 RStudio。

最佳答案

一个选项是指定 line 参数给 mtext()。在下面的示例中,我使用 par() 在图的右侧 (side = 4) 添加了几行,然后我使用 绘制了三个标签>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)

enter image description here

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

需要一点点调整边距线的数量(在 par(mar = x) 中设置)以及使用 mtext() 要在哪条线上绘制>,但稍加尝试和错误就可以得到您想要的东西。

另请注意,您不需要line 参数指定整数 值。您也可以指定行的分数:line = 2.5

关于r - 将标签添加到 R 中的次轴,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31011701/

相关文章:

r - 无法更改数据框列名称

string - 在字符串中的特定位置插入字符

html - R Shiny : how to give an inputID to an actionButton created via HTML

r - 如何防止小数位数::百分比加小数

r - 如何在R中高效地实现合并

vb.net - 图表 : Show more value descriptions on X-Axis

java - 如何在 java 中将轴放在 .png 文件上?

r - 修改现有ggplot对象中的比例而不替换比例

d3.js:在轴上的刻度之间对齐文本标签

删除 R Markdown 中的幻灯片编号