首页 > 解决方案 > 如何在 R 中的直方图上垂直居中数字?

问题描述

如何在列上居中数字?

bar <- barplot(VADeaths)

text(rep(bar,each=nrow(VADeaths)),as.vector(apply(VADeaths,2,cumsum)), 
 labels=as.vector(apply(VADeaths,2,cumsum)),
 pos=1,cex=1,adj = c(0,0),col="orange",offset = 0.2)

这是它现在的样子:

1

我需要让它像这里一样:

2

我该怎么做呢?谢谢!

标签: rplothistogram

解决方案


您只需将y值调整为条高的一半 - 并使用adj它来居中...

bar <- barplot(VADeaths)

text(rep(bar, each=nrow(VADeaths)),
     as.vector(apply(VADeaths, 2, cumsum) - 0.5 * VADeaths),  #subtract half of bar height
     labels=as.vector(apply(VADeaths, 2, cumsum)),
     cex = 1, adj = c(0.5, 0.5), col = "orange")

在此处输入图像描述


推荐阅读