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

标签 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_discrete与两者 limitsexpand论据无济于事以及position = position dodge ,同样没有效果。

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

最佳答案

我想我已经找到了解决办法。您可以删除 width来自 geom_bar并介绍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 = "")

enter image description here

关于r - ggplot2:如何在 coord_flip 和面板边框之后减少窄条之间的空间,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31905926/

相关文章:

r - 当有一个角色作为 ID 的变量时,为什么部署带有香根草的 tidymodel 会抛出错误?

r - 如何在全局文件中找到像素(单元格)的 4 个坐标(lat-long)?

r - 使用 stat_summary_hex 以离散色标显示最频繁的值

css - 更改 p 列宽度

css - 如何使用CSS设置背景图片宽度?

R 如何使用日期创建自定义 x 轴

r - Tex Proof 环境未在 R Markdown 中定义,amsthm 未在生成的 Tex 文件中

r - 将带有线型的变量传递给 ggplot 线型

r - geom_text_repel 中的条件尺寸和面孔

html - 无论内容长度如何,均等宽度的 flex 元素