在此示例图上,是否可以将图例符号(红色,绿色或蓝色圆点)与换行的图例文本的第一行对齐? (摘自eipi10 Multiple Lines for Text per Legend Label in 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\nwith wrapping") +
theme(legend.key.height=unit(2, "cm"))
这是一个细节点,但合著者坚持要这样做。
答案 0 :(得分:2)
这是一种解决方案,通过将背景色更改为白色并使用调校来解决。我找不到在框内使点顶部对齐的简单方法...
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\nwith 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创建