r - ggplot中图例键之间的间距
问题描述
我在图表顶部有一个图例。我希望图例左对齐并能够设置美学符号(彩色正方形)和文本之间的间距(1)以及(2)文本和下一个美学符号之间的间距。
library(tidyverse)
mtcars %>%
mutate(transmission = ifelse(am, "manual", "automatic")) %>%
ggplot() +
aes(x = transmission, fill = transmission) +
geom_bar() +
labs(fill = NULL) +
theme(
#legend.spacing.x = unit(.5, "char"), # adds spacing to the left too
legend.position = "top",
legend.justification = c(0,0),
legend.title=element_blank(),
legend.margin=margin(c(5,5,5,0)))
解决方案
添加边距进行调整 element_text
mtcars %>%
mutate(transmission = ifelse(am, "manual", "automatic")) %>%
ggplot() +
aes(x = transmission, fill = transmission) +
geom_bar() +
labs(fill = NULL) +
theme(
#legend.spacing.x = unit(.5, "char"), # adds spacing to the left too
legend.position = "top",
legend.justification = c(0, 0),
legend.title = element_blank(),
legend.margin = margin(c(5, 5, 5, 0)),
legend.text = element_text(margin = margin(r = 10, unit = "pt")))
推荐阅读
- excel - 在字符串中查找子字符串,但从右到左查找excel
- python-3.x - 不再能够在 Vscode 中执行 python 代码
- python - 如何找到float64中一列和另一列的两列熊猫数据框之间的相关性是对象(字符串)
- python - 用python解析CSV中的困难字符串
- c++ - 调试器因新的 UWP C++ 解决方案而失败
- matlab - 配对函数的阶复杂度
- deep-learning - 使用 CNN 进行图像分割的骰子系数太高
- html - 剩余可用空间 HTML 的高度
- scala - 查找 sbt 找到插件或 JAR 的存储库
- python - PyInstaller ErroNo2“没有这样的文件或目录:'akespec'”