ggplot x 轴标签太长,需要换行
ggplot x axis label too long and need line break
我正在尝试缩短我的 x 轴标签。
我想用两行更改 x 标签,文本长度相同
如何缩短我的文字并显示为两行?
您可以使用 word
来获取前几个单词,并使用 str_wrap
来控制每行的字符数,如下所示:
library(tidyverse)
tribble(
~label, ~value,
"my_very_long_label", 10,
"even_longer_label_with more_words", 10,
"really_quite_verbose_label", 10
) |>
mutate(label = str_replace_all(label, "_", " "),
label = word(label, 1, 3), # Use only the first 3 words
label = str_wrap(label, 15) # Only 15 characters per line
) |>
ggplot(aes(label, value)) +
geom_col()
由 reprex package (v2.0.1)
于 2022-04-28 创建
我正在尝试缩短我的 x 轴标签。
我想用两行更改 x 标签,文本长度相同
如何缩短我的文字并显示为两行?
您可以使用 word
来获取前几个单词,并使用 str_wrap
来控制每行的字符数,如下所示:
library(tidyverse)
tribble(
~label, ~value,
"my_very_long_label", 10,
"even_longer_label_with more_words", 10,
"really_quite_verbose_label", 10
) |>
mutate(label = str_replace_all(label, "_", " "),
label = word(label, 1, 3), # Use only the first 3 words
label = str_wrap(label, 15) # Only 15 characters per line
) |>
ggplot(aes(label, value)) +
geom_col()
由 reprex package (v2.0.1)
于 2022-04-28 创建