0

I have the following data:

set.seed(1)
df <- data.frame(type=c("A","B","D","A","C","D","B","C","D","E"),
                 count=as.integer(runif(10,100,200)),
                 sample=c(1,1,1,2,2,2,3,3,3,3),stringsAsFactors=F)
df$type <- factor(df$type,levels=unique(sort(df$type)))

which I plot using geom_bar:

require(ggplot2)
ggplot(df,aes(x=sample,y=count,fill=type))+geom_bar(stat="identity",position="dodge")+theme(legend.position="none")

enter image description here

My question is how to add df$type as a label on top of each bar?

Adding:

geom_text(aes(label=type,hjust=0),position=("dodge"))

obviously doesn't work

dan
  • 6,048
  • 10
  • 57
  • 125
  • Possible duplicate of [What is the width argument in position\_dodge?](http://stackoverflow.com/questions/34889766/what-is-the-width-argument-in-position-dodge) – cuttlefish44 Dec 09 '16 at 05:34

1 Answers1

1

This will work:

ggplot(df,aes(x=sample,y=count,fill=type))+
  geom_bar(stat="identity",position="dodge")+
  geom_text(aes(label=type,hjust=0, vjust=-0.1),position=position_dodge(width = 1))+
  theme(legend.position="none") 

enter image description here

Sandipan Dey
  • 21,482
  • 2
  • 51
  • 63