3

I'm trying to plot a ggplot2 graph in a power point slide with the officer package. I can do it actually (printing the ggplot2 directly in the ppt), but as I need to increase the size of the ggplot2 graph (for the ppt slide), and I have understood that ggplot2 graphs are dependent on the size of the window (in RStudio) or whatever you set it as if you are exporting it, I'm looking for a way to (1) export the ggplot2 graph with a given size (for example: height=5, width=8), (2) importing/reading from the ppt code:

library(officer)
library(devEMF)
library(magrittr)
library(ggplot2)

t <- "../example.pptx"
filename <- gg

read_pptx() %>% 
  add_slide(layout = "Title and Content", master = "Office Theme") %>% 
  ph_with_img(src = filename, width = 6, height = 4, type = "body") %>% 
  print(target = t)

gg is any plot from ggplot2 (it doesn't matter actually). t is the output file address.

ph_with_img

PowerPoint documents and graphics

PD: All of this is unnecesary if there is some package/command I don't know and I still can't find, where I can edit the size of the ggplot2.

Dan Chaltiel
  • 7,811
  • 5
  • 47
  • 92
Chris
  • 2,019
  • 5
  • 22
  • 67

3 Answers3

5

I just made a new package export built on top of officer that easily allows one to to do this using the command graph2ppt() and which nicely exports in vector format as opposed to bitmap in the other answer posted above, e.g.

install.packages("export")
library(export)
library(ggplot2)
qplot(Sepal.Length, Petal.Length, data = iris, color = Species, 
      size = Petal.Width, alpha = I(0.7))     
graph2ppt(file="plots.pptx", width=6, height=5) 
Tom Wenseleers
  • 7,535
  • 7
  • 63
  • 103
  • Can this approach be combined with the standard officer language = e.g. `doc <- read_pptx(); doc <- ph_with(x = doc, value = my_ggplot, location = ph_location_fullsize() )` - or is it a full wrapper that only renders a single graphic as a pptx page? – geotheory Jun 15 '20 at 11:35
  • It's ony a wrapper, internally using officer, and so my only aim was to be able to easily export a single graph, though it also allows adding extra graphs to an existing file. – Tom Wenseleers Jun 16 '20 at 09:35
3

I have had success first saving the ggplot2 graph as a .png and then calling that file into ph_with_img. A bit roundabout, but it works. You can also save the graph as a ?tempfile and then ?unlink, but I somewhat like having a folder of my graphs.

ggplot() +
  (code for my ggplot)

ggsave("../thisplot.png", width = 6, height = 4)

read_pptx() %>% 
  add_slide(layout = "Title and Content", master = "Office Theme") %>% 
  ph_with_img(src = "../thisplot.png", width = 6, height = 4, type = "body") %>% 
  print(target = t)
  • 1
    Disadvantage of this solution is that the output is bitmap then as opposed to vector graphics... I posted alt answer below which retains vector format... – Tom Wenseleers Mar 26 '19 at 03:09
3

The following shows how to create and export a ggplot object as a vector graphic directly to powerpoint using officer 0.3.11. The key is to use ph_with with location = ph_location. This allows you to set the position and size of the object.


library(ggplot2)
library(dplyr, warn.conflicts = FALSE)
library(officer)
library(rvg)

t <- "example.pptx"

fig_gg <- ggplot(mtcars, aes(x=wt, y=mpg)) +
  geom_point()

fig_vg <- dml(ggobj = fig_gg)

read_pptx() %>% 
  add_slide(layout = "Title Only", master = "Office Theme") %>% 
  ph_with(fig_vg, location = ph_location(left = .5, top = 1.3, height = 5, width = 5)) %>% 
  print(t)

Created on 2020-06-12 by the reprex package (v0.3.0)

JWilliman
  • 3,558
  • 32
  • 36
  • Unless I'm mistaken this does not change the size (resolution) of the graph but rather the specification of its bounding box on the page. It gives no control over how big text and margins render. – geotheory Jun 15 '20 at 11:32
  • @geotheory In my example changing the specs for `width` and `height` do change the size of the figure in powerpoint. Vector images do not have a resolution per se (as compared to raster/bitmap images). The size of text and margins is controlled from within the ggplot call, eg. `fig_gg + theme(text = element_text(size=20))`. – JWilliman Jun 16 '20 at 03:23
  • It's very fiddly to get all this right inside ggplot. You need a method to re-scale the final plot. – geotheory Jun 16 '20 at 08:12
  • @geotheory I guess that is a matter of opinion. Setting the options from within the ggplot call ensures the figure is consistent regardless of the output (latex, html, or pptx). I prefer using vector graphics in pptx as it maintains the figure at the highest possible 'resolution', meets journal requirements, allows coworkers to view the image without specialist graphical software, and the final plot is editable from within powerpoint if you still want to tweak font sizes etc. – JWilliman Jun 16 '20 at 21:43
  • Certainly vector is preferable. The reason for my view is that inside ggplot you can either set all text elements to same size (rarely desirable) or individually write _theme_ arguments for each element. And then repeat for various types of margin/strip/etc. Or you can just specify adjust `w={x}, h={y}` in ggsave and you're sorted. Unfortunate that something closer to the latter approach isn't available here. – geotheory Jun 16 '20 at 21:56
  • Apologies I don't intend to sound critical. I just hate having to faff around with theme! – geotheory Jun 16 '20 at 22:03
  • 1
    Fair enough :) You can adjust the relative size of all text using the `theme(base_size = 20)` argument. See https://community.rstudio.com/t/ggplot-theme-with-larger-font-sizes/30084 for more info. – JWilliman Jun 16 '20 at 22:21
  • I didnt know that! Thanks – geotheory Jun 16 '20 at 22:33