2

I'm using the officer package to write ggplots to Powerpoint, and even though my plots look good in my Rstudio viewer, my plots are getting "squished" when I add them to any custom slide with smaller content boxes.

I did my best to reproduce my issue with the following code:

library(tidyverse)
library(officer)

p <- data.frame(x = rnorm(100)) %>%
  ggplot() +
  geom_density(aes(x)) +
  labs(title = "This is a title") +
  annotate("text", x = 0, y = 0.2, label = "This is some text")

read_pptx("~/Downloads/template.pptx") %>%
  add_slide(layout = "Title and Content") %>%
  ph_with(p, location = ph_location_type("body", id = 1)) %>%
  add_slide(layout = "text_example") %>%
  ph_with(p, location = ph_location_type("body", id = 1)) %>%
  print("~/Desktop/test_example.pptx")

Where template.pptx is a copy of the officer template with the "Title and Content" slide duplicated (and named "test_example") and content box shrunken down a little bit (available here for reproducibility).

The first slide looks good, like this:enter image description here, but the second slide has the dimensions of the plot shrunken while the content in the plot stays the same size, giving it a "squished" feel: enter image description here

Has anyone run into this before? Are there any tricks/hacks out there that can tell my plot to decrease the size of all it's elements when the total image space is decreased? I'd like to maintain the relative size of the elements of the plot as I move to different slide templates.

If you're still here - THANK YOU FOR READING SO FAR! I'd appreciate any and all tips :)

redarah
  • 152
  • 7
  • This is due to the layout you using `add_slide(layout = "text_example")` which may have certain content container set with that size. – Sinh Nguyen Feb 23 '21 at 01:17
  • Yes I understand that, but is there a way to preserve the relative size of plot elements as I move them to different sized containers in the slide? – redarah Feb 23 '21 at 01:24

1 Answers1

0

Here is the code that would avoid that problem with officer version 0.3.14 or later (I haven't update my package for a while and officer is an on-going package)

For better quality plot with smaller size using vector graphic instead. Using package rvg

library(rvg)
library(gridExtra)

document %<>%
   add_slide(layout = "text_example") %>%
   # add the plot in at position relative to 1 inches top left 
   # with size is 6 inches square
   # grid.arrange is just a code for generate plot not actually arrange anything
   ph_with(value =  dml(code = grid.arrange(plot), bg = "transparent"), 
           location = ph_location(left=1, top=1, width=6,
                                  height=6, bg="transparent")) %>%
   # Add a text box on the right side of the slide with certain
   # size & colors
   ph_with(value = block_list(fpar(ftext(" "))),
           location = ph_location(left=6.11, top=1, width=3.73,
                                  height=3.3, bg="#C6D9F1"))

I would recommend this when working with Powerpoint otherwise, you need to adjust template in Powerpoint and remember the order of each content container in that slide

Sinh Nguyen
  • 4,277
  • 3
  • 18
  • 26
  • Unfortunately that doesn't solve the problem - the plots are still squished when the boxes get smaller. – redarah Feb 24 '21 at 13:22
  • I misunderstand your point - If you want better quality graph with smaller size then using vector graphic instead. - adjust the answer using `rvg` with `officer` And if you want to adjust the font size etc... it will require manual adjust in different template. – Sinh Nguyen Feb 24 '21 at 22:03