ggplot中图例键之间的间距

sha*_*roz 4 r ggplot2

我在图的顶部有一个图例。我希望图例保持左对齐,并能够设置美学符号(彩色正方形)和文本之间的间距(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)))
Run Code Online (Sandbox Code Playgroud)

在此处输入图片说明

mpa*_*nco 6

添加边距进行调整 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")))
Run Code Online (Sandbox Code Playgroud)

在此处输入图片说明