将文本添加到R中的水平条形图中,y轴在不同的尺度上?

时间:2022-10-19 09:54:30

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.

我试着将一些文本添加到与每个bar相同高度的水平barplot的右侧,但是,text()和axis()都不会在对应于每个bar的高度上绘制这个图。

Here's a similar barplot

这是一个类似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))

1 个解决方案

#1


14  

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.

通过对barplot的文档进行排序,可以看到它有一个不可见的返回值:条的中点。您可以使用它们向情节添加额外的信息。

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

#1


14  

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.

通过对barplot的文档进行排序,可以看到它有一个不可见的返回值:条的中点。您可以使用它们向情节添加额外的信息。

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