position_dodge and nudge_y together

Viewed 2423

I am trying to add labels (letters) above a barplot using ggplot2 function geom_text. My bars are separated using position=position_dodge() and so I need to apply the same for the new labels. However I would like to use also nudge_y to separate the labels from the bar. If I try to use both together R complains that I can use only one of either options. I'd like to do something like this:

Tukey.labels <- geom_text(data=stats,
                    aes(x=factor(Treatment2), y=x.mean, 
                        label=Tukey.dif),
                    size=4, nudge_y=3,            # move letters in Y
                    position=position_dodge(0.5)) # move letters in X

To create something like this image Does anybody knows a possibility to shift all my labels the same distance in Y while doing position_dodge at the same time? I could not find answer for this in other posts

1 Answers

Hard to troubleshoot without a reproducible example. Hopefully this helps:

library(dplyr); library(ggplot2)
ggplot(mtcars %>% rownames_to_column("car") , 
       aes(as.factor(cyl), mpg, group = car)) + 
  geom_col(position = position_dodge(0.9)) +
  geom_errorbar(aes(ymin = mpg - wt,
                    ymax = mpg + wt),
                position = position_dodge(0.9)) +
  geom_text(aes(label = gear, y = mpg + wt), vjust = -0.5,
            position = position_dodge(0.9))

enter image description here

Related