本文介绍了R图形:在堆叠的条形图中添加标签的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我正在寻找一种使用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.
谢谢!
推荐答案
barplot
将返回小节的x中间位置,所以您可以这样做
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"))
,重新阅读您的问题,我想这就是您想要的(或者可能不是,但我还是会写它:D)
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)
这篇关于R图形:在堆叠的条形图中添加标签的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!