1

The code below creates a PowerPoint slide with a vector based ggplot-object, this works great.

Now I want the same slide, with graph as image (e.g. PNG, un-editable). I think this was possible using ph_with_gg(plot, resolution) etc., but that's now deprecated.

It can be achieved with copy/paste as image in PowerPoint, but that's tedious. My main reason is large datasets and powerpoint slowing down with too many individual objects.

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')
help-info.de
  • 6,695
  • 16
  • 39
  • 41
Christian
  • 11
  • 2

1 Answers1

3

In that case, you only need to not call 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')

You can read the documentation here: https://davidgohel.github.io/officer/articles/offcran/powerpoint.html#ggplot-objects-1

David Gohel
  • 9,180
  • 2
  • 16
  • 34
  • I actually missed that a plot can be used directly and will end up as an image. But great, thanks. I have a tendency to overthink things :-) – Christian Aug 25 '20 at 10:57
  • no problem, see https://davidgohel.github.io/officer/reference/ph_with.html for a complete list of supported objects with `ph_with` – David Gohel Aug 25 '20 at 12:22