如何将图例符号与ggplot2上的换行图例文本对齐 [英] How to align legend symbol with wrapped legend text on ggplot2
本文介绍了如何将图例符号与ggplot2上的换行图例文本对齐的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
library(stringr)
library(tidyverse)
# Create long labels to be wrapped
iris$Species = paste(iris$Species,
"random text to make the labels much much longer than the original labels")
ggplot(iris, aes(Sepal.Length, Sepal.Width, colour=str_wrap(Species,20))) +
geom_point() +
labs(colour="Long title shortened
with wrapping") +
theme(legend.key.height=unit(2, "cm"))
这是一个细节问题,但一位合著者坚持这样做。
推荐答案
这里有一种解决方案,通过将背景颜色更改为白色并使用vJust来实现。我找不到一种简单的方法来将框内的点顶部对齐...
library(stringr)
library(tidyverse)
# Create long labels to be wrapped
iris$Species = paste(iris$Species,
"random text to make the labels much much longer than the original labels")
ggplot(iris, aes(Sepal.Length, Sepal.Width, colour=str_wrap(Species,20))) +
geom_point() +
labs(colour="Long title shortened
with wrapping") +
theme(legend.key.height=unit(2, "cm"), legend.key = element_rect(fill = "white")) +
guides(colour = guide_legend(label.vjust = -1, label.position = "right"))
由reprex package(v0.2.1)于2019-01-28创建
这篇关于如何将图例符号与ggplot2上的换行图例文本对齐的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文