繁体   English   中英

R图形:向堆积条形图添加标签

[英]R graphics: Add labels to stacked bar chart

我正在寻找一种方法,使用R的基本绘图功能将标签(即绝对值)添加到堆积条形图中。标签应位于堆叠条形图内。

谢谢!

barplot将返回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"))

也许你可以使用或检查plotrix包的barp功能

暂无
暂无

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

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