簡體   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