【问题标题】:How do I vertically center numbers on an histogram plot in R?如何在 R 中的直方图上垂直居中数字?
【发布时间】:2020-03-13 01:11:24
【问题描述】:

如何使列中的数字居中?

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)

这就是现在的样子:

我需要像这里一样:

我该怎么做?谢谢!

【问题讨论】:

    标签: r plot histogram


    【解决方案1】:

    文本功能中的offset 可以帮助您将标签文本置于中心位置。 给你一个建议,尝试操纵cex,使标签的大小根据条形图变短。

    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=0.6,adj = c(0,0),col="orange",offset = 1.5)
    

    【讨论】:

    • 我试过了,但它并没有真正帮助,因为如果我增加偏移量,如果我尝试操纵 cex,字体大小会很小,即使我操纵 cex,文本也不适合。对不起我的英语。
    • 你也可以试试ggplot()
    【解决方案2】:

    您只需将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")
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2019-03-03
      • 1970-01-01
      • 2010-12-30
      • 2013-10-15
      • 2017-04-05
      • 2018-02-26
      相关资源
      最近更新 更多