Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
527 views
in Technique[技术] by (71.8m points)

r - Increase space between bars in ggplot

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

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

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


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...