ggplot2:如何在coord_flip和面板边框之后减小窄宽度条之间的空间

时间:2015-08-09 15:38:33

标签: r ggplot2 width

当您翻转坐标时,如何减少窄条和面板边框之间的空间?使用数据框df和ggplot命令,底栏和刻度线之间有很多空白区域(同样是"供应商"栏上方的宽阔空间)。

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 = "")

enter image description here

我尝试使用scale_x_discretelimits参数expand无效,position = position dodge同样无效。

question提供coord_equal来更改宽高比,从而减少或消除额外空间,但请注意解决方案不适用于coord_flip

1 个答案:

答案 0 :(得分:8)

我想我找到了解决方案。您可以从width移除geom_bar并引入theme(aspect.ratio = .2),然后您可以使用该比率来查找所需的宽度。与coord_equalcoord_fixed不同,coord_flip与<{1}}兼容。

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

enter image description here