Chris
Chris

Reputation: 2071

Plotting a ggplot2 graph in a power point slide with a given size - R

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.

Upvotes: 3

Views: 6499

Answers (3)

JWilliman
JWilliman

Reputation: 3883

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)

Upvotes: 3

Tom Wenseleers
Tom Wenseleers

Reputation: 7989

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) 

Upvotes: 5

user10917479
user10917479

Reputation:

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)

Upvotes: 3

Related Questions