使用 officer R 导出时如何提高 ggplots 的分辨率

How to increase resolution of ggplots when exporting using officer R

我想将图表导出到 PPT,我正在使用 officer 包来实现相同的目的。然而,图表的默认分辨率很低,我想改变它。我目前正在使用以下调用

    ph_with_gg(p1,type = "chart",res = 1200)

其中 p1 是一个 ggplot 对象。在 运行 上,我收到以下错误:

    Error in png(filename = file, width = width, height = height, units = 
"in",  : 
  formal argument "res" matched by multiple actual arguments

非常感谢对此的帮助

将绘图保存到代码中的演示文稿很重要吗?

否则使用:

ggsave(filename = file, p1, width = width, height = height, dpi = dpi)

会给你一个你需要的任何分辨率的png..

(前提是文件名以.png结尾,并且您将宽高和dpi设置为合适的值)

对于 PPT 中的 high-resolution 图,与其使用 png,不如使用 矢量图形

参见extensions:

Vector graphics with package rvg

The package rvg brings an API to produce nice vector graphics that can be embedded in PowerPoint documents or Excel workbooks with officer.

这个包提供函数dml()ph_with()相应的方法来将ggplots导出为矢量图形的.pptx。

示例:

library(ggplot2)
library(officer)
library(rvg)
library(magrittr)
data(iris)

read_pptx() %>%
  add_slide(layout='Title and Content',master='Office Theme') %>%
  ph_with('Iris Sepal Dimensions', location = ph_location_type(type="title")) %>%
  ph_with(dml( ggobj=
                 ggplot(iris, aes(x=Sepal.Length,y=Sepal.Width,col=Species)) +
                 geom_point()), location = ph_location_type(type="body")) %>%
  print('iris_presentation.pptx')

作为一项额外的好处,您将能够在 PowerPoint 中编辑图表。例如,如果您决定将 3 个物种的名称大写,您可以只编辑图表而不是编辑数据和重新生成幻灯片。 (您也可以制作绘图 non-editable,但默认为可编辑。)