ggplot2:如何缩小coord_flip之后的窄宽度条和面板边框之间的空间 [英] ggplot2: how to reduce space between narrow width bars, after coord_flip, and panel border

查看:109
本文介绍了ggplot2:如何缩小coord_flip之后的窄宽度条和面板边框之间的空间的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

翻转坐标后,如何缩小窄条与面板边框之间的间距?使用数据框df和ggplot命令,在底部栏和刻度线之间有很多空白(并且类似地在供应商"栏上方有很大的空间).

When you have flipped coordinates, how do you reduce the space between bars that are narrow and the panel border? Using the data frame df and the ggplot commands, there is much white space between the bottom bar and the tick marks (and similarly a wide space above the "vendor" bar).

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均无济于事.

I tried scale_x_discrete with both limits and expand arguments to no avail as well as position = position dodge, likewise with no effect.

问题coord_equal提供了更改长宽比,从而减少或消除多余的空间,但请注意,解决方案不适用于coord_flip .

This question offers coord_equal to change the aspect ratio, and thereby reduce or eliminate the extra space, but notes that the solution does not work with coord_flip.

推荐答案

我想我已经找到了解决方案.您可以从geom_bar中删除width并引入theme(aspect.ratio = .2),然后可以按比例进行播放以找到所需的宽度.并且不同于coord_equalcoord_fixedcoord_flip兼容.

I think I have found a solution. You can remove width from geom_bar and introduce theme(aspect.ratio = .2), then you can play with the ratio to find the desired width. And unlike coord_equal or coord_fixed is compatible with coord_flip.

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

这篇关于ggplot2:如何缩小coord_flip之后的窄宽度条和面板边框之间的空间的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆