将文本添加到 R 中的水平条形图,不同比例的 y 轴? [英] add text to horizontal barplot in R, y-axis at different scale?

查看:24
本文介绍了将文本添加到 R 中的水平条形图,不同比例的 y 轴?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试在与每个条形图相同高度的水平条形图的右侧添加一些文本,但是 text() 和 axis() 似乎都没有在对应的高度上绘制它每个酒吧.

I'm trying to add some text to the right hand side of a horizontal barplot at the same heights as each bar, however, both text() and axis() don't seem to plot this at the heights corresponding to each bar.

这是一个类似的条形图

x <- runif(10, 0,1)
y <- matrix(c(x, 1-x), nrow=2, ncol=10, byrow=TRUE)
barplot(y, horiz=TRUE, beside=FALSE, names.arg=seq(1,10,1), las=1, xlim=c(0, 1.2))

这两个选项都没有正确对齐,缩放在这里是如何工作的?

Neither of these two options align properly, how does the scaling work here?

axis(4, at=seq(1,10,1), labels=seq(1,10,1))
text(1.1, seq(1,10,1), labels=seq(1, 10, 1))

推荐答案

通过查 barplot 的文档,你可以看到它有一个不可见的返回值:条形的中点.您可以使用它们为绘图添加其他信息.

By chacking the documentation of barplot, you can see that it has an invisible return value: the midpoints of the bars. You can use those to add additional information to the plot.

x <- runif(10, 0,1) 
y <- matrix(c(x, 1-x), nrow=2, ncol=10, byrow=TRUE) 
bp <- barplot(y, horiz=TRUE, beside=FALSE, names.arg=seq(1,10,1), las=1, 
              xlim=c(0, 1.2)) 
text(x, bp, signif(x,2), pos=4)
bp

这篇关于将文本添加到 R 中的水平条形图,不同比例的 y 轴?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆