Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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.

like image 397
Chris Avatar asked Jan 28 '26 15:01

Chris


1 Answers

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) 
like image 100
Tom Wenseleers Avatar answered Jan 30 '26 06:01

Tom Wenseleers