geom_label_repel 文本对齐
geom_label_repel text justification and alignment
在下面的示例中,所有文本都以正数 nudge_x
放置的情况下,是否有可能的解决方法来左对齐由 geom_label_repel
(或 geom_text_repel
)创建的文本标签direction
参数中的值和仅调整位置?目前,默认行为是居中对齐文本:
library(ggplot2)
library(ggrepel)
ggplot(mtcars, aes(x=factor(gear), y=mpg, colour=factor(gear))) +
geom_point(size=3) +
facet_wrap(~cyl, labeller=label_both) +
scale_x_discrete(expand=c(0, 1.5)) +
geom_label_repel(aes(label=rownames(mtcars)),
size=3, segment.size=0.25, nudge_x=0.5, direction="y")
我希望通过设置 hjust=0
来模拟 geom_label
(或 geom_text
)中可能的左对齐,如下例所示,同时能够自动排斥y 方向的标签:
ggplot(mtcars, aes(x=factor(gear), y=mpg, colour=factor(gear))) +
geom_point(size=3) +
facet_wrap(~cyl, labeller=label_both) +
scale_x_discrete(expand=c(0, 1.5)) +
geom_label(aes(label=rownames(mtcars)), size=3, nudge_x=0.2, hjust=0)
已编辑:作为 hack,是否可以将 hjust(和 vjust)构建到 ggrepel 中?
自 OP 发布此问题以来的 4 年里,hjust=
似乎已添加到 ggrepel
包中:
library(ggplot2)
library(ggrepel)
ggplot(mtcars, aes(x=factor(gear), y=mpg, colour=factor(gear))) +
geom_point(size=3) +
facet_wrap(~cyl, labeller=label_both) +
scale_x_discrete(expand=c(0, 1.5)) +
geom_label_repel(
aes(label=rownames(mtcars)), hjust=0,
size=3, segment.size=0.25, nudge_x=0.5, direction="y")
在下面的示例中,所有文本都以正数 nudge_x
放置的情况下,是否有可能的解决方法来左对齐由 geom_label_repel
(或 geom_text_repel
)创建的文本标签direction
参数中的值和仅调整位置?目前,默认行为是居中对齐文本:
library(ggplot2)
library(ggrepel)
ggplot(mtcars, aes(x=factor(gear), y=mpg, colour=factor(gear))) +
geom_point(size=3) +
facet_wrap(~cyl, labeller=label_both) +
scale_x_discrete(expand=c(0, 1.5)) +
geom_label_repel(aes(label=rownames(mtcars)),
size=3, segment.size=0.25, nudge_x=0.5, direction="y")
我希望通过设置 hjust=0
来模拟 geom_label
(或 geom_text
)中可能的左对齐,如下例所示,同时能够自动排斥y 方向的标签:
ggplot(mtcars, aes(x=factor(gear), y=mpg, colour=factor(gear))) +
geom_point(size=3) +
facet_wrap(~cyl, labeller=label_both) +
scale_x_discrete(expand=c(0, 1.5)) +
geom_label(aes(label=rownames(mtcars)), size=3, nudge_x=0.2, hjust=0)
已编辑:作为 hack,是否可以将 hjust(和 vjust)构建到 ggrepel 中?
自 OP 发布此问题以来的 4 年里,hjust=
似乎已添加到 ggrepel
包中:
library(ggplot2)
library(ggrepel)
ggplot(mtcars, aes(x=factor(gear), y=mpg, colour=factor(gear))) +
geom_point(size=3) +
facet_wrap(~cyl, labeller=label_both) +
scale_x_discrete(expand=c(0, 1.5)) +
geom_label_repel(
aes(label=rownames(mtcars)), hjust=0,
size=3, segment.size=0.25, nudge_x=0.5, direction="y")