当y轴标签的长度不同时,element_text(hjust = .5)
将标签围绕小平面 * 内的公共中间线 * 对齐,而不是跨越所有标签对齐。
举个例子:
library(tidyverse)
t1 <- tibble(
labs = c(
"A short label",
"Second short label",
"This is a medium length label",
"This is also a medium length",
"Now, this label is actually quite long, really long",
"This label is also actually quite long, really long"
) %>%
fct_inorder %>%
fct_rev,
facs = str_c(
"facet", 1:3
) %>%
rep(each = 2),
x = runif(6)
)
t1 %>%
ggplot(
aes(
x, labs
)
) +
geom_point() +
facet_grid(
vars(facs),
scales = "free_y"
) +
labs(
x = "",
y = ""
) +
theme(
axis.text.y = element_text(hjust = .5)
)
字符串
应返回
的数据
如何使y轴标签沿着公共轴居中对齐?
1条答案
按热度按时间pgky5nke1#
您可以将以下
dplyr::mutate
添加到tibble:mutate(labs = str_pad(labs, max(str_width(labs)), side = "both"))
为字符串添加填充,使它们都具有相同的表观长度。字符串
x1c 0d1x的数据
创建于2023-07-17带有reprex v2.0.2