【发布时间】:2021-06-20 05:24:43
【问题描述】:
我可以在一个空的 ggplot2 图中创建一个阴影文本作为described here。我想创建大量这样的地块,其中:
- 文本大小会自动调整,以防文本太长而无法适应空白图的宽度
- 文本自动在空白图的中间居中
考虑以下示例:
library("ggrepel") # Load packages
library("ggplot2")
my_text <- c("Text 1", # Three different text elements
"Loooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooong Text 2",
"Some Other Text 3")
for(i in 1:length(my_text)) {
ggp <- ggplot() + # Draw ggplot2 plot with text only
theme_void() +
geom_text_repel(mapping = aes(1, 1, label = my_text[i]),
color = "blue",
bg.color = "red",
bg.r = 0.05)
ggsave(ggp, # Save ggplot2 plot
filename = paste0("my_text_", i, ".png"),
bg = "transparent",
width = 101.6,
height = 57.15,
scale = 0.05,
dpi = 1000)
}
前面R代码的输出如下:
如您所见,我们创建了三个带有阴影文本的不同图像。但是,这些文本元素都没有在图的中间完全对齐。此外,第二个文本太长,不适合绘图区域,因此应该使用较小的字体大小。
如果需要,如何自动调整字体大小,如何自动将每个文本居中?
【问题讨论】: