题
我正在寻找一种添加标签的方法,即使用 R 的基本绘图函数将绝对值转换为堆积条形图。标签应位于堆叠条形内部。
谢谢你!
解决方案
barplot
将返回酒吧的中期x位置,所以你可以做
mydata <- matrix(c(10, 21, 22, 33, 45, 23, 22, 43, 33), nrow=3)
# b will contain the x midpoints of the bars
b <- barplot(mydata)
# This will write labels in the middle of the bars, horizontally and vertically
text(b, colMeans(mydata), c("Label1", "Label2", "Label3"))
# This will write labels in the middle of the middle block
text(b, mydata[1,]+mydata[2,]/2, c("LabelA", "LabelB", "LabelC"))
编辑:重新阅读你的问题,我想这是你想要的(或者也许没有,但我会写它反正:d)
# Find the top y position of each block
ypos <- apply(mydata, 2, cumsum)
# Move it downwards half the size of each block
ypos <- ypos - mydata/2
ypos <- t(ypos)
text(b, ypos, mydata)
其他提示
怎么样简单的功能 text()
?
你可以简单地添加一串在任何你想要的,例如:
text (x = ..., y = ..., labels = c("foo bar 1000"))
也许您可以使用或检查 巴普 的功能 情节 包裹
不隶属于 StackOverflow