ggplot中图例键之间的间距

时间:2018-08-10 07:04:35

标签: 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)))

enter image description here

1 个答案:

答案 0 :(得分:3)

adjust 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")))

enter image description here