ggtext 格式被 ggsave 弄乱了

ggtext formatting getting messed up with ggsave

我正在使用 ggtext::element_textbook_simple 在绘图中包含一些填充文本,因为它在长字符串的自动换行方面具有强大的功能。

当我直接在 markdown 中 运行 代码时,我得到了一个漂亮的情节,所有单词之间的间距均匀:

```{r fig.width = 6, fig.height = 4}
library(dplyr)
library(ggplot2)
library(ggtext)

p1 <- mtcars %>% 
  ggplot(aes(x = wt, y = hp)) +
  geom_point() +
  labs(title = "This is a Generic Title",
       subtitle = "The theme song and opening sequence set the premise of the show. Will Smith is a street-smart teenager, West Philadelphia born and raised. While playing street basketball, Will misses a shot and the ball hits a group of gang members, causing a confrontation that frightens his mother, who sends him to live with his wealthy aunt and uncle in the opulent neighborhood of Bel Air, Los Angeles. Will's working class background ends up clashing in various humorous ways with the upper class world of the Banks family – Will's uncle Phil and aunt Vivian and their children, Will's cousins: spoiled Hilary, pompous Carlton, and impressionable Ashley.") + 
  theme(plot.title.position = "plot",
        plot.subtitle = element_textbox_simple(size = 10, lineheight = 1, padding = margin(5, 1, 5, 1)))

p1
```

但是,当我使用 ggsave 导出具有相同尺寸的图时,突然间出现很多间距错误,其中包含以下文字:

ggsave("plot1.png", p1, width = 6, height = 4)

有谁知道这是为什么 case/how 我可以防止这种情况发生?

可能是图形设备。我无法重现这个问题。尝试聚合设备。

library(ragg)

agg_png("plot1.png", width = 6, height = 4, units = "in", res = 300)
print(p1)
dev.off()