Alignment of numbers on the individual bars

The error is from the scale_y_continuous call. Formatting of labels is now handled by the labels argument. See the ggplot2 0.9.0 transition guide for more details.

There was another problem with the labels not lining up correctly; I fixed that by adding a group=B to the aesthetics for the geom_text; I’m not quite sure why this is necessary, though. I also took out x=A from the geom_text aesthetics because it was not needed (it would be inherited from the ggplot call.

library("ggplot2")
library("scales")

ggplot(data=df, aes(x=A, y=Freq))+
    geom_bar(aes(fill=B), position = position_dodge()) + 
    geom_text(aes(label = paste(sprintf("%.1f", Freq*100), "%", sep=""),
                  y = Freq+0.015, group=B),
              size = 3, position = position_dodge(width=0.9)) +
    scale_y_continuous(labels = percent) +
    theme_bw()

enter image description here

Leave a Comment