ggplot2:如何在 coord_flip 之后的窄条之间和面板边框之间减少 space

ggplot2: how to reduce space between narrow width bars, after coord_flip, and panel border

翻转坐标后,如何减少窄条与面板边框之间的 space?使用数据框 df 和 ggplot 命令,底部条和刻度线之间有很多白色 space(并且 "vendor" 条上方有一个宽 space) .

df <- data.frame(x = c("firm", "vendor"), y = c(50, 20))

ggplot(df, aes(x = x, y = y)) + 
  geom_bar(stat = "identity", width = 0.4) + 
  theme_tufte() +  coord_flip() +
  labs(x = "", y = "")

我用 limitsexpand 参数试过 scale_x_discrete 无济于事, position = position dodge 同样没有效果。

question 提供 coord_equal 更改纵横比,从而减少或消除额外的 space,但请注意解决方案 不适用于coord_flip.

我想我找到了解决办法。您可以从 geom_bar 中删除 width 并引入 theme(aspect.ratio = .2),然后您可以调整比例以找到所需的宽度。并且不像 coord_equalcoord_fixed 兼容 coord_flip.

ggplot(df, aes(x = x, y = y)) + 
  geom_bar(stat = "identity") + 
  theme_tufte() + theme(aspect.ratio = .2) +
  coord_flip() +
  labs(x = "", y = "")