繁体   English   中英

将图例标签与ggplot左对齐

[英]Left align legend labels with ggplot

我有一个传说,看起来像这样:

图例

令人困惑的是,例如“ color-a”标签恰好位于其左侧的点和其右侧的点之间的中心。 我希望此标签离其左侧的点更近,以使哪个标签与哪个点相关联更为明显。

我到目前为止一直没有运气尝试使用legend.key.widthlegend.title.alignlegend.spacing.x ...

这是一个最小的可重现示例:

library(tidyverse)

# Test data, it does not matter.
data <- tibble(
  color = c(rep('color-a', 5), rep('color-b', 5), rep('color-c', 5), rep('color-d', 5)),
  x = rep(seq(0, 4, 1), 4),
  y = c(seq(0, .4, 0.1), seq(0, .4, 0.1) + 0.1, seq(0, .4, 0.1) + 0.3, seq(0, .4, 0.1) + 0.4)
)

# Plot
ggplot(data, aes(x = x, y = y, color = color)) +
  scale_color_discrete(guide="legend") +
  geom_point() +
  theme_minimal() +
  theme(legend.position = "bottom")

您不能让它们比现有的对齐方式更多。 但是,您可以设置margin ,以在文本的右侧之间创建更多的空间:

ggplot(data, aes(x = x, y = y, color = color)) +
  scale_color_discrete(guide='legend') +
  geom_point() +
  theme_minimal() +
  theme(
    legend.position = "bottom", 
    legend.text = element_text(margin = margin(0, 50, 0, 0))) ## <- here
  )

在此处输入图片说明

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM