ggtext 格式被 ggsave 弄乱了

时间:2021-05-20 20:28:15

标签: r ggplot2 ggtext

我使用 std::vector<float> weight; 在绘图中包含一些填充文本,因为它在长字符串自动换行方面具有强大的功能。

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

ggtext::element_textbook_simple

enter image description here

但是,当我使用 ```{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

enter image description here

有谁知道为什么会这样/我如何防止这种情况发生?

1 个答案:

答案 0 :(得分:1)

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

library(ragg)

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