2

I would like to label every column with the count value of Fill_factor for that specific column, but i can not get more than one label per column.

mydata.df <- data.frame(Fill_factor = c("Fill1", "Fill3", "Fill1", "FILL2","Fill1","Fill1","FILL2", "FILL2","FILL3" ), Count = c("AA", "BB", "AA", "AA","CC","BB","AA", "BB", "CC" ))
q <- qplot(Count, data= mydata.df, fill= Fill_factor, geom="bar", position="fill") # base sub by gene names 
q + theme(axis.text.x=element_text(angle=-90, hjust = 1), text = element_text(size=10)) 

table(mydata.df$Count,mydata.df$Fill_factor)

 Fill1 FILL2 Fill3 FILL3
AA     2     2     0     0
BB     1     1     1     0
CC     1     0     0     1

I have use also the function ddply but i don´t know how to get the right position

mydata.df.count <- ddply(mydata.df, .(Fill_factor, Count), nrow)
colnames(mydata.df.count)  <- c("Fill_factor", "Count", "Frequency")

mydata.df.count <- ddply(mydata.df.count, .(Count), transform, pos = cumsum(Frequency)/5) # incorrect position

p = ggplot(mydata.df.count, aes(x = Count, y = Frequency), stat="identity") + geom_bar(aes(fill = Fill_factor), position="fill") + geom_text(aes(label = Frequency, y = pos), size = 5)
p

How can I place text labels in the right position (multiple text per column) using the position="fill" argument? and how could i sort the levels of Count to order for the highest Fill factor == "fill1"counts?

Thanks!

Jaap
  • 81,064
  • 34
  • 182
  • 193
user3300849
  • 189
  • 2
  • 13
  • There's quite a few nice answers on this topic on SO: http://stackoverflow.com/search?q=[ggplot2]+label+stacked+bars+geom_bar – Henrik Feb 17 '14 at 12:24
  • Here's how to label a stacked barplot http://stackoverflow.com/questions/6644997/showing-data-values-on-stacked-bar-chart-in-ggplot2 – lukeA Feb 17 '14 at 12:24
  • I don´t know but I always get missing y and ymax argument, I don´t know how to get the position argument and the y value – user3300849 Feb 17 '14 at 13:01

1 Answers1

3

You have to relate the cumulative frequency to the total frequency and then vertically adjust the labels.

The code:

mydata.df.count <- ddply(mydata.df.count, .(Count), transform, pos = cumsum(Frequency)/sum(Frequency))

ggplot(mydata.df.count, aes(x = Count, y = Frequency, fill = Fill_factor)) + 
  geom_bar(stat="identity", position="fill") + 
  geom_text(aes(y = pos, label = Frequency), vjust = 3)

The result: enter image description here

Jaap
  • 81,064
  • 34
  • 182
  • 193