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

发布于 2024-09-17 09:08:06 字数 73 浏览 7 评论 0原文

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

谢谢你!

I am looking for a way to add labels, i.e. absolute values, into a stacked bar chart using the basic plot functions of R. The labels should be inside the stacked bars.

Thank you!

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

国际总奸 2024-09-24 09:08:06

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)

barplot will return the mid x position of the bars, so you could do

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

EDIT: re-reading your question, I think this is what you want (or maybe not, but I'll write it anyways :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)
屌丝范 2024-09-24 09:08:06

简单的函数 text() 怎么样?

您可以简单地在任意位置添加字符串,例如:

text (x = ..., y = ..., labels = c("foo bar 1000"))

How about the simple function text()?

You can simply add a string where ever you want, eg:

text (x = ..., y = ..., labels = c("foo bar 1000"))
好多鱼好多余 2024-09-24 09:08:06

也许您可以使用或检查 plotrix

Maybe you can use or inspect the barp function of the plotrix package

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文