【问题标题】:Spacing between legend keys in ggplotggplot中图例键之间的间距
【发布时间】:2018-08-10 07:04:35
【问题描述】:

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

【问题讨论】:

标签: r ggplot2


【解决方案1】:

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

【讨论】:

  • 对我的情况适得其反(垂直图例):在文本中添加边距会使文本与键的关系...
猜你喜欢
  • 2019-11-17
  • 1970-01-01
  • 2011-04-10
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2021-12-06
  • 2023-03-06
  • 1970-01-01
相关资源
最近更新 更多