11

dodged bar plot in ggplot again has me stumped. I asked about annotating text above bars on here a few weeks back (LINK) and got a terrific response to use + stat_bin(geom="text", aes(label=..count.., vjust=-1)). I figured since I already have the counts I'll just supply them with out the .. before and after and I told stat_bin that the position was dodge. It lines them up over the center of the group and adjusts up and down. Probably something minor. Please help me to get the text over the bars.

enter image description here

mtcars2 <- data.frame(type=factor(mtcars$cyl), 
    group=factor(mtcars$gear))
library(plyr); library(ggplot)
dat <- rbind(ddply(mtcars2,.(type,group), summarise,
    count = length(group)),c(8,4,NA))

p2 <- ggplot(dat,aes(x = type,y = count,fill = group)) + 
    geom_bar(colour = "black",position = "dodge",stat = "identity") +
    stat_bin(geom="text", aes(position='dodge', label=count, vjust=-.6)) 
Community
  • 1
  • 1
Tyler Rinker
  • 108,132
  • 65
  • 322
  • 519

2 Answers2

12

I was having trouble getting the position dodges to line up, so I ended up creating a position_dodge object (is that the right terminology?), saving it to a variable, and then using that as the position for both geoms. Somewhat infuriatingly, they still seem to be a little off centre.

dodgewidth <- position_dodge(width=0.9)
ggplot(dat,aes(x = type,y = count, fill = group)) + 
  geom_bar(colour = "black", position = dodgewidth ,stat = "identity") +
  stat_bin(geom="text", position= dodgewidth, aes(x=type, label=count), vjust=-1)

enter image description here

Marius
  • 58,213
  • 16
  • 107
  • 105
  • 1
    MYaseen's method is centered. Wonder if there's a fix to stat bin. – Tyler Rinker Apr 26 '12 at 04:50
  • 2
    Yeah, I've just tried it with `dodgewidth <- position_dodge(width=0.9) `, and it's nicely centered. I have no idea why 0.9 would be the magic number through – Marius Apr 26 '12 at 04:52
  • I don't think ther's anything special about 0.9. In the example below change the value for width in both the geom_bar and geom_text to see the effect. I've just noticed the example is almost the same as MYaseen208 – Sandy Muspratt Apr 26 '12 at 05:18
  • @SandyMuspratt With your example, using `geom_text`, the widths and dodges behave sensibly- you set the width of the bar, and then dodge the text by that amount. I still can't figure out how dodging works with `stat_bin` though, except I guess if 0.9 is the default width of a `stat_bin` bar and you can't really change it. `geom_text` is probably the better option in this case though, as you aren't really 'binning' the observations. – Marius Apr 26 '12 at 05:41
  • @Marius I'm not sure why dodging is not working for `stat_bin`. I've always used geom_text. Incidently in the geom_text example, there are other effects to be had. For instance, let the arguments for geom_bar() be: geom_bar(colour = "black",position = position_dodge(width = .8), width = .6) – Sandy Muspratt Apr 26 '12 at 05:51
  • @Marius Another incidently - your answer doesn't work - misplaced ! before the closing bracket. – Sandy Muspratt Apr 26 '12 at 06:34
  • @SandyMuspratt Thanks, I think it had snuck in at some point when I was adding the image to the post. All fixed up nicely now. – Marius Apr 26 '12 at 07:14
11

Updated geom_bar() needs stat = "identity"

I think this does what you want as well.

mtcars2 <- data.frame(type = factor(mtcars$cyl), group = factor(mtcars$gear))
library(plyr); library(ggplot2)
dat <- rbind(ddply(mtcars2, .(type, group), summarise, count = length(group)), c(8, 4, NA))

p2 <- ggplot(dat, aes(x = type,y = count,fill = group)) + 
  geom_bar(stat = "identity", colour = "black",position = "dodge", width = 0.8) +
  ylim(0, 14) +
  geom_text(aes(label = count, x = type, y = count), position = position_dodge(width = 0.8), vjust = -0.6)
p2

enter image description here

Community
  • 1
  • 1
Sandy Muspratt
  • 31,719
  • 12
  • 116
  • 122