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