Increase space between bars in ggplot
Asked Answered
T

1

7

I have a bar plot:

p <- ggplot(data=df, aes(x=Gene, y=FC, fill=expt, group=expt))
p <- p + geom_bar(colour="black", stat="identity", position = position_dodge(width = 0.9))
p <- p + geom_errorbar(aes(ymax = FC + se, ymin = FC, group=expt),
                position = position_dodge(width = 0.9), width = 0.25)
p

I want to increase the spacing between bars (for each bin). I've tried messing around with the position_dodge(width = ...) but it skews my error bars:

enter image description here

There are several other questions that relate to this:

  • This question has an answer that seems to do the trick (but difficult to implement)
  • When I use the answer to this question I get the following:

enter image description here

i.e it seems to increase the between bins, but at the expense of overlapping with the neighbouring bar

Tailorbird answered 16/7, 2015 at 15:31 Comment(0)
G
16

You can adjust the width outside of the position_dodge as well (in geom_bar),

ggplot(data=df, aes(x=Gene, y=FC, fill=expt, group=expt)) +
  geom_bar(colour="black", stat="identity", position = position_dodge(width = 0.8), width=0.5) +
  geom_errorbar(aes(ymax = FC + se, ymin = FC, group=expt),
                position = position_dodge(width = 0.8), width = 0.25)

enter image description here

or

dodge <- position_dodge(width = 0.5)

ggplot(data=df, aes(x=Gene, y=FC, fill=expt, group=expt)) +
  geom_bar(colour="black", stat="identity", position=dodge, width=0.5) +
  geom_errorbar(aes(ymax = FC + se, ymin = FC, group=expt),
                position = dodge, width = 0.25)

enter image description here

Grapnel answered 16/7, 2015 at 16:23 Comment(3)
Thanks, but it's still not clear to me what controls the space between factors and the space between treatments.Tailorbird
i.e how would I adjust your code to keep the factors together but slightly decrease the spacing between them? Essentially your second illustration but with the spaces reducedTailorbird
@Tailorbird just increase both widths (position and bar) to, say, 0.7Grapnel

© 2022 - 2024 — McMap. All rights reserved.