军官:幻灯片以ggplot作为图像

问题描述

下面的代码使用基于矢量的ggplot对象创建PowerPoint幻灯片效果很好。

现在,我要使用与图像相同的幻灯片 (例如,PNG,不可编辑)。我认为可以使用ph_with_gg(图,分辨率)等来实现,但这已被弃用。

可以将复制/粘贴作为PowerPoint中的图像来实现,但这很繁琐。我的主要原因是大型数据集和Powerpoint随单个对象过多而变慢。

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')

解决方法

在这种情况下,您无需致电rvg::dml

library(ggplot2)
library(officer)
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(
    ggplot(iris,aes(x=Sepal.Length,y=Sepal.Width,col=Species)) + geom_point(),location = ph_location_type(type="body")) %>%
  print('iris_presentation.pptx')

您可以在此处阅读文档:https://davidgohel.github.io/officer/articles/offcran/powerpoint.html#ggplot-objects-1