简体   繁体   English

在R,y轴以不同比例添加文本到水平条形图?

[英]add text to horizontal barplot in R, y-axis at different scale?

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. 我正在尝试将一些文本添加到与每个条形相同高度的水平条形图的右侧,但是,text()和axis()似乎都没有在与每个条形对应的高度处绘制这些文本。

Here's a similar barplot 这是一个类似的情节

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))

By chacking the documentation of barplot , you can see that it has an invisible return value: the midpoints of the bars. 通过chacking的文档barplot ,你可以看到它有一个无形的返回值:条形的中点。 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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM