Pau*_*idt 13 r justify ggplot2 ggtext
我想证明(不仅仅是对齐!)我的 ggplots 中的多行文本,即主要是标题、副标题、说明文字和注释geom_text()等。
需要明确的是,“对齐文本”是指在单词之间添加空格,以便每行的两个边缘都与两个边距对齐。段落中的最后一行左对齐。在下图中,它将是右侧的格式,以黄色突出显示:
下面是一个可重现的示例,尝试同时使用{ggplot2}和{ggtext}。正如您所看到的,我只能想到使用hjust=(和halign=with {ggtext}),但它们只能对齐而不对齐多行文本。
library(ggtext)
library(tidyverse)
my_text <- "Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua."
my_subtitle <- paste(rep(my_text, 5), collapse = " ")
df <- tibble(
my_text = rep(my_text, 3),
x = c(1, 2, 3),
y = c(2, 1, 0),
hjust = c(0, 0.5, 1),
vjust = c(0, 0.5, 1)
)
ggplot() +
ggtitle("ggplot2::geom_text() with line breaks inserted via str_wrap()") +
ggplot2::geom_text(data = df,
aes(
label = str_wrap(my_text, 45),
x = vjust,
y = hjust,
hjust = hjust,
vjust = vjust
)) +
labs(subtitle = str_wrap(my_subtitle, 100)) +
theme(plot.subtitle = element_text(hjust = 0))
Run Code Online (Sandbox Code Playgroud)

ggplot() +
ggtitle("ggtext::geom_richtext() with automatic word-wrapping") +
ggtext::geom_textbox(
data = df,
width = unit(2, "inch"),
box.color = NA,
fill = NA,
aes(
label = my_text,
x = vjust,
y = hjust,
hjust = hjust,
vjust = vjust,
halign = hjust,
valign = vjust
)
) +
labs(subtitle = my_subtitle) +
theme(plot.subtitle = ggtext::element_textbox_simple(hjust = 0, halign = 0))
Run Code Online (Sandbox Code Playgroud)

创建于 2022-08-23,使用reprex v2.0.2
您可以在此处对 GitHub 上的功能请求问题 +1 。