R图形: Add labels to stacked bar chart

标签 r label bar-chart stacked

我正在寻找一种使用 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)

关于R图形: Add labels to stacked bar chart,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/3626324/

相关文章:

c# - 将水平条形图更改为垂直条形图

r - ggplot删除特定的x轴标签

r - 将列表转换为 Tibble 并添加带有列表名称的列

r - 如何在R中执行大型数据集的完全外部联接?

google-maps-api-3 - 带标签的标记不出现

Python matplotlib 减小颜色条标签的大小

r - 条形图中的百分比标签

reactjs - 如何防止 Nivo 刻度轴文本(条形图)中的文本截断

javascript - 使用 D3.js 创建图表时出现 TypeError : svg.append(...).attrs 不是函数

r - filter() 但保留没有值的组