增加ggplot中栏之间的空间

时间:2015-07-16 15:31:06

标签: r ggplot2

我有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

我想增加条形之间的间距(对于每个bin)。我已经尝试过使用position_dodge(width = ...),但它会使我的错误栏出现偏差:

enter image description here

还有其他几个与此相关的问题:

  • This question有一个似乎可以解决问题的答案(但很难实现)
  • 当我使用this question的答案时,我得到以下内容:

enter image description here

即似乎增加了箱子之间的间隔,但代价是与相邻的栏重叠

1 个答案:

答案 0 :(得分:10)

您也可以调整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) ),

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

gap:["PluginManager","startup","PluginManager307573941"]

enter image description here