繁体   English   中英

ggplot2 - geom_text 如何 position 中间的标签

[英]ggplot2 - geom_text how to position the labels in the middle of the bars

对于 RStudio/ggplot2 的初学者,我需要一些帮助。 我用以下代码制作了一个条形图

ggplot(match_player, aes(x = civ, fill = winner, group = winner), show.legend = T) +
  geom_bar() +
  geom_text(aes(label = stat(count)), stat = "count", color = "black", size = 3, position = "dodge")

条形图图像

在此处输入图像描述

我想将标签保留在所属栏的中间。

您已经接近了,您只需要更改文本层中的position参数即可。 以下标准数据集的示例:

library(ggplot2)

ggplot(mpg, aes(class, fill = as.factor(drv))) +
  geom_bar() +
  geom_text(stat = "count", position = position_stack(vjust = 0.5),
            aes(label = after_stat(count)))

reprex package (v0.3.0) 于 2021 年 2 月 2 日创建

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM