对齐单个 ggrepel 标签中的文本行
Justifying lines of text within individual ggrepel labels
我有一个情节,我在其中用多行 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),
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
注释来做这件事,我会这样做,这会产生我想要的 left/right 理由:
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
标签实现此目的?
谢谢!
这已在 ggrepel
的开发版本(版本 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
标签标记元素。我想在其中一些中右对齐,在另一些中左对齐。参见示例:
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
注释来做这件事,我会这样做,这会产生我想要的 left/right 理由:
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
标签实现此目的?
谢谢!
这已在 ggrepel
的开发版本(版本 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"))