Mäsä
Mäsä

Reputation: 13

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

I need some help for a beginner with RStudio/ggplot2. I made a barplot with following code

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")

barplot image

enter image description here

I would like to keep the labels in the middle of the belonging bar.

Upvotes: 1

Views: 2161

Answers (1)

teunbrand
teunbrand

Reputation: 37913

You're close, you just need to change the position argument in the text layer. Example with standard dataset below:

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)))

Created on 2021-02-02 by the reprex package (v0.3.0)

Upvotes: 2

Related Questions