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

查看:102
本文介绍了在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天全站免登陆