2015-08-27 148 views
1

我想减少酒吧之间的距离较窄,但是,position_dodge似乎并没有在调整酒吧之间的距离工作。ggplot geom_bar with position_dodge不调整酒吧之间的距离

下面是我用不同宽度的一个0.7和一个带5,但巴之间的宽度不受影响两个输出的代码示例...:

library(ggplot2) 
library(grid) 
library(ggthemes) 
library(scales) 
library(gridExtra) 

variables = c('a','b','c','d','e') 
values = c(0.2,0.4,0.6,0.8,1.0) 
std = c(0.05,0.06,0.03,0.08,0.09) 

Data = data.frame(variables, values, std) 

f3 = ggplot(data = Data, aes(x = variables, y = values, group = variables)) + 
    geom_bar(stat='identity',width=0.6,position=position_dodge(width = 0.7),fill=c('#FF7F0E','#2CA02C','#D62728', '#00008B', '#B23AEE')) + 
    coord_flip() + 
    geom_errorbar(aes(ymin=values-std, ymax=values+std), 
       width=.2,size=0.3) + 
    scale_y_continuous("Variable Importance", expand = c(0,0),limits = c(0, 1.1), breaks=seq(0, 1.1, by = 0.1)) + # rescale Y axis slightly 
    scale_x_discrete("Variables", limits = c('a','b',"c","d","e")) + 
    theme_bw() + # make the theme black-and-white rather than grey (do this before font changes, or it overrides them) 
    theme(
    line = element_line(size=0.3), 
    plot.title = element_blank(), # use theme_get() to see available options 
    axis.title.x = element_text(family='sans',size=13), 
    axis.title.y = element_text(family='sans',size=13, angle=90), 
    axis.text.x = element_text(family='sans',vjust=0.4,size=11), 
    axis.text.y = element_text(family='sans',size=11), 
    panel.grid.major = element_blank(), # switch off major gridlines 
    panel.grid.minor = element_blank(), # switch off minor gridlines 
    legend.position = 'none', # manually position the legend (numbers being from 0,0 at bottom left of whole plot to 1,1 at top right) 
    legend.title = element_blank(), # switch off the legend title 
    legend.text = element_blank(), 
    legend.key.size = unit(1.5, "lines"), 
    legend.key = element_blank(), # switch off the rectangle around symbols in the legend 
    panel.border=element_blank(), 
    axis.line=element_line(size=0.3) 
) 

plot(f3) 

sample plot generated with position_dodge(witdt = 0.7)

sample plot generated with position_dodge(witdt = 5), these two plots are exactly identical..

任何帮助将不胜感激! 谢谢,

+2

我建议'geom_pointrange'呈现这类数据。 –

+0

为什么'geom_bar()'中有'width = 0.6'的参数,这里没有:http://prntscr.com/89m3ah – user2673238

+0

@Gregor你的评论真的回答了这个问题。可以将它作为答案发布? – tonytonov

回答

1

position_dodge当你在一个位置上有多个酒吧使用,就像如果你有几间酒吧绘制在"a"你的“变量”轴。见,例如,例如,从?position_dodge

ggplot(mtcars, aes(x=factor(cyl), fill=factor(vs))) + 
    geom_bar(position="dodge") 

这里,X轴是由cyl定义,但将填充颜色由vs定义,所以我们必须在每个x由填充颜色区分多条位置。该位置闪避说,把酒吧彼此相邻(而不是在彼此顶部position = "identity"或堆叠position = "stack")。

您在每个位置只有单条,因此position_dodge不会执行任何操作。完全摆脱position_dodge并使用geom_bar的宽度参数(您已将其设置为0.6)。

这是一个很好的可重现的例子,但我鼓励你在将来使你的堆栈溢出问题更多最小。所有的theme调用你是不相关的问题,尽管加载5包,唯一的一个你实际使用的是ggplot2。下面是一些精简的代码。我删除position_dodge讨论,摆脱你scale_x_continuous,因为它没有改变任何东西离开默认值,我移动fill里面aes()并添加scale_fill_manual ---这只是我喜欢的风格,我摆脱了主题定制为16行代码,这对于问题的清晰度无关紧要。

f3 = ggplot(data = Data, 
      aes(x = variables, y = values, 
       group = variables, fill = variables)) + 
    geom_bar(stat = 'identity', 
      width = 0.6) +  ## adjust this width 
    coord_flip() + 
    geom_errorbar(aes(ymin = values - std, ymax = values + std), 
        width = .2, size = 0.3) + 
    scale_y_continuous("Variable Importance", 
         expand = c(0,0), 
         limits = c(0, 1.1), 
         breaks = seq(0, 1.1, by = 0.1)) + 
    scale_fill_manual(values = c('#FF7F0E','#2CA02C','#D62728', '#00008B', '#B23AEE'), 
         guide = FALSE) + 
    theme_bw() 

f3 

作为调节宽度的一个例子(尽管这是overplotting没有更换,所以如果你想使宽度小编辑原始F3定义)。其他

f3 + geom_bar(stat = "identity", width = 0.9) 

有两点需要注意:

  1. 你的大部分主题的修改似乎是复制theme_classic() ---这也被内置于ggplot2。使用theme_classic()而不是theme_bw()可能会让您更接近您的目标。

    f3 + theme_classic() 
    
  2. 由于罗马在评论中指出,geom_pointrange可能更适合这个数据。在酒吧顶部的置信区间往往难以看到和比较。这里有一个例子:

    ggplot(data = Data, 
         aes(x = variables, y = values, color = variables)) + 
        geom_pointrange(aes(ymin = values - std, ymax = values + std), size = 1) + 
        scale_y_continuous("Variable Importance", 
            expand = c(0,0), 
            limits = c(0, 1.1), 
            breaks = seq(0, 1.1, by = 0.1)) + 
        scale_color_manual(values = c('#FF7F0E','#2CA02C','#D62728', '#00008B', '#B23AEE'), 
            guide = FALSE) + 
        coord_flip() + 
        theme_classic() 
    

    enter image description here

+0

感谢您的详细解释。这非常有帮助! – user32147