对齐 ggplot y 轴标签沿着公共中线,在各个 facet 中。

huangapple go评论69阅读模式
英文:

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)
  )

应该返回如下结果:

对齐 ggplot y 轴标签沿着公共中线,在各个 facet 中。

如何使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 &lt;- tibble(
  labs = c(
    &quot;A short label&quot;,
    &quot;Second short label&quot;,
    &quot;This is a medium length label&quot;,
    &quot;This is also a medium length&quot;,
    &quot;Now, this label is actually quite long, really long&quot;,
    &quot;This label is also actually quite long, really long&quot;
    ) %&gt;% 
    fct_inorder %&gt;% 
    fct_rev,
  facs = str_c(
    &quot;facet&quot;, 1:3
    ) %&gt;% 
    rep(each = 2),
  x = runif(6)
  )

t1 %&gt;% 
  ggplot(
    aes(
      x, labs
    )
  ) +
  geom_point() +
  facet_grid(
    vars(facs), 
    scales = &quot;free_y&quot;
  ) +
  labs(
    x = &quot;&quot;,
    y = &quot;&quot;
  ) +
  theme(
    axis.text.y = element_text(hjust = .5)
  )

should return

对齐 ggplot y 轴标签沿着公共中线,在各个 facet 中。

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)
  )

对齐 ggplot y 轴标签沿着公共中线,在各个 facet 中。

创建于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 = &quot;both&quot;)) to add padding to the strings so they all have the same apparent length.

library(tidyverse)

t1 &lt;- tibble(
  labs = c(
    &quot;A short label&quot;,
    &quot;Second short label&quot;,
    &quot;This is a medium length label&quot;,
    &quot;This is also a medium length&quot;,
    &quot;Now, this label is actually quite long, really long&quot;,
    &quot;This label is also actually quite long, really long&quot;
  ) %&gt;% 
    fct_inorder %&gt;% 
    fct_rev,
  facs = str_c(
    &quot;facet&quot;, 1:3
  ) %&gt;% 
    rep(each = 2),
  x = runif(6)
) |&gt; 
  mutate(labs = str_pad(labs, max(str_width(labs)), side = &quot;both&quot;))

t1 %&gt;% 
  ggplot(
    aes(
      x, labs
    )
  ) +
  geom_point() +
  facet_grid(
    vars(facs), 
    scales = &quot;free_y&quot;
  ) +
  labs(
    x = &quot;&quot;,
    y = &quot;&quot;
  ) +
  theme(
    axis.text.y = element_text(hjust = .5)
  )

对齐 ggplot y 轴标签沿着公共中线,在各个 facet 中。<!-- -->

<sup>Created on 2023-07-17 with reprex v2.0.2</sup>

huangapple
  • 本文由 发表于 2023年7月17日 14:06:46
  • 转载请务必保留本文链接:https://go.coder-hub.com/76701864.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定