r - 如何使用多行文本在分组条形图的条形上添加值?

标签 r plot

如何使用 mtext 在柱上写入值?

# Grouped Bar Plot
counts <- table(mtcars$vs, mtcars$gear)
barplot(counts, main="Car Distribution by Gears and VS",xlab="Number of Gears", col=c("darkblue","red"),legend = rownames(counts), beside=TRUE, horiz=TRUE)

mtext(counts )   # But position is not at each bar. 

如何在每个条形图上放置相应的值?

最佳答案

那么,mtext通常用于边距。您是否有不想使用的原因 text ?

 bplt <- barplot(counts, 
                 main="Car Distribution by Gears and VS", xlab="Number of Gears", 
                 col=c("darkblue","red"), legend = rownames(counts), 
                 beside=TRUE, horiz=TRUE)

# variable 'bplt' is now a matrix of vertical bar positions on the y-axis

text(x= counts+0.3, y= bplt, labels=as.character(counts), xpd=TRUE)
# Needed to use xpd=TRUE because the xlimits were too narrow.

enter image description here

关于r - 如何使用多行文本在分组条形图的条形上添加值?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11911506/

相关文章:

python - 将文本标签放置在不依赖于数据的 3D 坐标中

R:如何将结果合并为唯一的输出?

python - python ggplot 还在开发中吗?

r - 如何在我用类型 ='s' 绘制的线下填充

r - R图中文本边界的范围

python - 改变 pyplot 图的 x 值的方向

r - 如何使用 ggarrange() 或类似方法手动调整合并图的高度

r - 如何在 R Shiny 中显示带有输入的简单数据框

regex - 在 R 中计算 strsplit 的输出

r - 使用(如果可能)函数方法修改基于正则表达式 (regex) 向量的向量