Add values to bars using ggplot2

Hi!
I have this input file with just one column: the gender of the participants
https://www.mediafire.com/file/txtbh2hfb8w2l20/demo.xlsx/file
I'm plotting women in one bar and men in the other and works fine

demog <- read_xlsx("C:/Users/demo.xlsx")
library(ggplot2)
p<-ggplot(demog) + geom_bar(aes(x = demog$GENDER,fill=demog$GENDER), stat = "count")+scale_fill_manual(values=c("blue", "red"))
 p

Now I'm trying to add labels with the values at the top of each bar:
I need to add the 25 value at the top of the men bar and 15 at the top of the women bar
I tried geom_text but can't find the way how to put it in my code

geom_text(aes(label = y), position = "dodge")

Something like this?

demog <- data.frame(stringsAsFactors = FALSE,
                    ID = c(17703, 19060, 19994, 14185, 22006, 22165, 22282, 22749, 15440,
                           14915, 24558, 16776, 17305, 13403, 19785, 19328, 23735, 16787,
                           17300, 22410, 20211, 21602, 20619, 19420, 17440, 23588, 22201,
                           23625, 22164, 4626, 9899, 13102, 23056, 20498, 18521, 9263,
                           13144, 20216, 851, 18999),
                    GENDER = c("W", "W", "M", "W", "M", "M", "W", "W", "W", "W",
                               "M", "M", "M", "M", "M", "M", "M", "M", "W", "W",
                               "W", "W", "W", "M", "M", "M", "W", "M", "M", "M", "M",
                               "M", "W", "M", "M", "M", "M", "W", "W", "M"))

library(ggplot2)
library(dplyr)

demog %>% 
    count(GENDER) %>% 
    ggplot(aes(x = GENDER, y = n, fill = GENDER)) +
    geom_col() +
    geom_text(aes(label = n),
              nudge_y = 2) +
    scale_fill_manual(values=c("blue", "red"))

Thanks a lot @andresrcs !!!

This topic was automatically closed 7 days after the last reply. New replies are no longer allowed.