条的不同宽度并删除 geom_bar() 中的白色 space

Different width of bars and remove white space in geom_bar()

我有一个带有误差条的条形图。我希望条形具有不同的宽度。是否可以删除条形之间的白色 space?如果可能的话,我还想删除绘图区域与 x 轴和 y 轴之间的白色 space。

示例:

require(ggplot2)

df <- data.frame(order=c(1,2,3), 
      rate=c(28.6, 30.75, 25.25), 
      lower=c(24.5, 28.94, 22.86), 
      upper=c(31.26, 33.1, 28.95), 
      width=c(.25,1.25,.5))

plot <- ggplot(data=df, aes(x=order, y=rate, width=width)) + 
        geom_bar(aes(fill=as.factor(order)), stat="identity") + 
        geom_errorbar(aes(x=order, ymin=lower, ymax=upper), width=.1)

感谢大家的建议。我设法使用 facet_grid() 找到了解决方法。这不是最优雅的解决方案,但它仍然有效。

require(ggplot2)

df <- data.frame(order=c(1,2,3), 
  rate=c(28.6, 30.75, 25.25), 
  lower=c(24.5, 28.94, 22.86), 
  upper=c(31.26, 33.1, 28.95), 
  width=c(.25,1.25,.5))

plot <- ggplot(data=df, aes(x=order, y=rate, width=width)) + 
  geom_bar(aes(fill=as.factor(order)), stat="identity") + 
  geom_errorbar(aes(x=order, ymin=lower, ymax=upper), width=.1) +
  facet_grid(~as.factor(order), scales='free_x', space='free', switch="x") + 
  scale_y_continuous(expand=c(0, 0)) + 
  scale_x_continuous(expand=c(0, 0)) + 
  theme_bw() + 
  theme(panel.border=element_blank(),
        panel.spacing.x=unit(0, "lines"), 
        strip.background=element_blank(), 
        strip.placement="outside",
        axis.text.x=element_blank(),
        axis.ticks.x=element_blank(),
        legend.position="none")

主要问题是如何正确对齐 geom_errorbar() 和 geom_bar()。 facet_grid 选项保持对齐。添加 panel.spacing.x=unit(0,"lines") 通过更改每个面板之间的 space 来删除条形之间的白色 space。关键是宽度必须至少为 1。此解决方案确实会导致 x 轴刻度线的对齐出现问题,但就我的目的而言,这不是问题。