如何将图例符号与ggplot2上的换行图例文本对齐 [英] How to align legend symbol with wrapped legend text on ggplot2

查看:18
本文介绍了如何将图例符号与ggplot2上的换行图例文本对齐的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

有没有办法将图例符号(红、绿或蓝点)与本示例图上换行的图例文本的第一行对齐?(摘自eipi10Multiple 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
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屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆