ggplot2:如何缩小coord_flip之后的窄宽度条和面板边框之间的空间 [英] ggplot2: how to reduce space between narrow width bars, after coord_flip, and panel border
问题描述
翻转坐标后,如何缩小窄条与面板边框之间的间距?使用数据框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 = "")
我尝试同时使用limits
和expand
参数的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_equal
或coord_fixed
与coord_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屋!