Search code examples
rggplot2powerpointofficerrvg

Changing the scale of a plot or table when generating a power point


I wish to be able to choose the dimensions of the plots I insert into my powerpoint when I generate them. I realise I could simply save them as a separate file and then insert them. But I would prefer to be able to manipulate them whilst I'm inserting them into the slide deck upon creation. Whether this be by changing the plot dimensions before insertion or changing the bounding box dimensions of the slide.

I've done some testing already:

"use_loc_size = F" within ph_with seems to only work with images as far as I can tell from the testing I've done.

Changing the pixel quality does change the scale somewhat, but the labels and graphs seems to change in very different ways if I touch that (labels get bigger the higher the pixel number whilst the graph gets smaller, and vice versa for the when I decrease the pixel number)

Example code:

library(flextable)
library(rvg)
library(officer)
library(ggplot2)

path_out <- "."

# prep ggplot
p1 <- iris %>% 
  ggplot() +
  geom_point(aes(Sepal.Length,Petal.Length,color = Species), size = 3) +
  theme_minimal()

# prep editable graph (rvg)
p2 <- dml(ggobj = p1)

my_pres <- read_pptx() %>%
  #slide 1
  add_slide(layout = "Title and Content", master = "Office Theme") %>%
  ph_with(value = p1, location = ph_location_type("body", width = 2, height = 13)) %>%
  #slide 2
  add_slide() %>%
  ph_with(value = p2, location = ph_location_type("body"), width = 6, height = 6) %>%
  print(target = file.path(path_out,"example_v1.pptx"))

Solution

  • If you want to define size from R without using placeholder properties, you can use ph_location and specify the width, height, and top left positions:

    library(flextable)
    library(rvg)
    library(officer)
    library(ggplot2)
    library(magrittr)
    
    path_out <- "."
    
    # prep ggplot
    p1 <- iris %>% 
      ggplot() +
      geom_point(aes(Sepal.Length,Petal.Length,color = Species), size = 3) +
      theme_minimal()
    
    # prep editable graph (rvg)
    p2 <- dml(ggobj = p1)
    
    my_pres <- read_pptx() %>%
      #slide 1
      add_slide(layout = "Title and Content", master = "Office Theme") %>%
      ph_with(value = p1, location = ph_location("body", left = 1, top = 1, width = 5, height = 5)) %>%
      #slide 2
      add_slide() %>%
      ph_with(value = p2, location = ph_location("body", left = 1, top = 1, width = 5, height = 5)) %>%
      print(target = file.path(path_out,"example_v1.pptx"))
    

    enter image description here