ggtext 格式被 ggsave 搞砸了

Posted

技术标签:

【中文标题】ggtext 格式被 ggsave 搞砸了【英文标题】:ggtext formatting getting messed up with ggsave 【发布时间】:2021-08-10 03:04:28 【问题描述】:

我正在使用 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 ***ing 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)

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

【问题讨论】:

【参考方案1】:

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

library(ragg)

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

【讨论】:

以上是关于ggtext 格式被 ggsave 搞砸了的主要内容,如果未能解决你的问题,请参考以下文章

JQuery 定义被 TypeScript 0.9 搞砸了

Vagrant 搞砸了代码?

php时间搞砸了

会话在外部主机上搞砸了

Heroku django - 搞砸了项目结构

框架中的移动网站搞砸了