1

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?

aosmith
  • 34,856
  • 9
  • 84
  • 118
Mary
  • 1,142
  • 1
  • 16
  • 37
  • Potential dupes: https://stackoverflow.com/questions/2551921/show-frequencies-along-with-barplot-in-ggplot2 ; https://stackoverflow.com/questions/19835987/display-frequency-instead-of-count-with-geom-bar-in-ggplot – Mike H. Oct 24 '17 at 20:00

1 Answers1

3

Try adding this:

d + geom_text(aes(label = value), 
              size = 3, 
              color = "black",
              position = position_dodge(width = 0.9),
              vjust = -2)
tyluRp
  • 4,678
  • 2
  • 17
  • 36