关于 r:左对齐图例标签与 ggplot | 珊瑚贝

Left align legend labels with ggplot


我有一个这样的图例:

Legend

这有点令人困惑,例如,” color-a”标签恰好位于其左侧点和右侧点之间的中心。我希望这个标签离它左边的点更近,以便清楚哪个标签与哪个点相关联。<=”” p=”” class=”box-hide box-show”>

我尝试使用 legend.key.width、legend.title.align、legend.spacing.x 到目前为止没有运气…

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

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
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”)

  • 还可以为字符串 data$color <- paste(data$color, ‘ ‘) 添加空间


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

1
2
3
4
5
6
7
8
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
  )

enter

  • 太棒了!谢谢!


来源:https://www.codenong.com/57898144/

微信公众号
手机浏览(小程序)

Warning: get_headers(): SSL operation failed with code 1. OpenSSL Error messages: error:14090086:SSL routines:ssl3_get_server_certificate:certificate verify failed in /mydata/web/wwwshanhubei/web/wp-content/themes/shanhuke/single.php on line 57

Warning: get_headers(): Failed to enable crypto in /mydata/web/wwwshanhubei/web/wp-content/themes/shanhuke/single.php on line 57

Warning: get_headers(https://static.shanhubei.com/qrcode/qrcode_viewid_9970.jpg): failed to open stream: operation failed in /mydata/web/wwwshanhubei/web/wp-content/themes/shanhuke/single.php on line 57
0
分享到:
没有账号? 忘记密码?