对齐单个ggrepel标签内的文本行 [英] Justifying lines of text within individual ggrepel labels
本文介绍了对齐单个ggrepel标签内的文本行的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我有一个图,其中用多行 ggrepel
标签标记元素.我想在其中一些中右对齐,在其他一些中左对齐.参见示例:
I have a plot where I'm labelling elements with multi-line ggrepel
labels. I'd like to right-justify within some of these and left-justify within others. See example:
p <- ggplot() +
coord_cartesian(xlim=c(0,1), ylim=c(0,1)) +
theme_void()
p
labelInfo <- data.frame(x=c(0.45,0.55), y=c(0.5,0.5),
g=c("I'd like very much to be\nright justified","And I'd like to be\nleft justified"))
p + geom_label_repel(data=labelInfo, aes(x,y,label=g),
box.padding = 0.5, point.padding = 0.75,
nudge_x = c(-0.05,0.05), nudge_y = 0, direction="x",
arrow=arrow(length=unit(2,"mm"), ends="last", type="closed"))
如果我使用 ggplot2
批注进行此操作,我会这样做,这样就产生了我想要的左右对齐方式:
If I were doing this with ggplot2
annotations, I'd do it like this, which yields what I want in terms of the left/right justification:
p + annotate("text", label="I'd like very much to be\nright justified", x=0.45, y=0.5, hjust=1) +
annotate("text", label="And I'd like to be\nleft justified", x=0.55, y=0.5, hjust=0)
如何使用 ggrepel
标签实现此目标?
How do I achieve this with ggrepel
labels?
谢谢!
推荐答案
现在已在 ggrepel
(版本0.8.1.9000)的开发版本中解决了此问题.
This has now been addressed in the development version of ggrepel
(version 0.8.1.9000).
library(ggplot2)
devtools::install_github("slowkow/ggrepel")
p <- ggplot() +
coord_cartesian(xlim=c(0,1), ylim=c(0,1)) +
theme_void()
p
labelInfo <- data.frame(x=c(0.45,0.55), y=c(0.5,0.5),
g=c("I'd like very much to be\nright justified","And I'd like to be\nleft justified"))
p + geom_label_repel(data=labelInfo, aes(x,y,label=g), hjust=c(1,0),
box.padding = 0.5, point.padding = 0.75,
nudge_x = c(-0.05,0.05), nudge_y = 0, direction="x",
arrow=arrow(length=unit(2,"mm"), ends="last", type="closed"))
这篇关于对齐单个ggrepel标签内的文本行的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文