英文:
Align ggplot y axis labels along common middle line, across facets
问题
当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轴标签在公共轴上居中对齐?
英文:
When y-axis labels are of different length, element_text(hjust = .5)
aligns the labels around a common middle line within facets, rather than across all the labels.
An example:
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)
)
should return
How do I get the y-axis labels to be middle aligned along a common axis?
答案1
得分: 2
你可以将以下的 dplyr::mutate
添加到 tibble 中:mutate(labs = str_pad(labs, max(str_width(labs)), side = "both"))
,以在字符串上添加填充,使它们具有相同的视觉长度。
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)
) %>%
mutate(labs = str_pad(labs, max(str_width(labs)), side = "both"))
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)
)
创建于2023-07-17,使用 reprex v2.0.2
英文:
You could add the following dplyr::mutate
to the tibble: mutate(labs = str_pad(labs, max(str_width(labs)), side = "both"))
to add padding to the strings so they all have the same apparent length.
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)
) |>
mutate(labs = str_pad(labs, max(str_width(labs)), side = "both"))
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)
)
<!-- -->
<sup>Created on 2023-07-17 with reprex v2.0.2</sup>
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论