简体   繁体   中英

adding frequency of data to bar plot using ggplot

This is my code for creating a bar plot using ggplot.

library(tidyverse)
    dat= data_frame(rating=1:5, No=c(167,82, 132, 182, 200), Yes=c(28, 22, 20, 27, 29))
    dat = dat %>%
      gather(key=color, value=value, -rating)
    d = ggplot(data=dat, aes(x=rating, y=value, fill=color)) + 
      geom_bar(stat='identity', position='dodge')+ labs(fill = "headache", y = "frequency") 

    d = d + scale_fill_manual(values=c("blue", "yellow"))

    d = d  + theme_bw() + theme(panel.border = element_blank(), panel.grid.major = element_blank(),
                                 panel.grid.minor = element_blank(), axis.line = element_line(colour = "black")) 

I need to add frequency of each data to to the top of each bar. Any suggestion?

Try adding this:

d + geom_text(aes(label = value), 
              size = 3, 
              color = "black",
              position = position_dodge(width = 0.9),
              vjust = -2)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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