Left align legend labels with ggplot
我有一个这样的图例:
这有点令人困惑,例如,” 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. # Plot |
- 还可以为字符串 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 ) |
- 太棒了!谢谢!
来源:https://www.codenong.com/57898144/