1

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"))
  • ph_location_type use placeholder defined dimensions. Did you have a look at the documention? `?officer::ph_location_type`, in the section "See Also", there are other `ph_location*` functions available. If none fits your need, you can use `ph_location`, see `?officer::ph_location` – David Gohel Mar 19 '20 at 15:22
  • In the documentation for `ph_with`, you will also see `use_loc_size` is only for object `external_img`, scrool to methods def and you will see arguments supported for each method. – David Gohel Mar 19 '20 at 15:25
  • @DavidGohel Thanks for the comment! I had noticed that `ph_location_type` had width and height modifiers. Sadly these do not seem to work for me for `ggplot` or `rvg` plots. I'm not why. The "See Also" section for `ggplot` sadly only directs me to the `rvg` function for more editing functionality, But I can only change the pixel number in `rvg`, not graph dimensions as far as I can tell. I did notice `ph_location` can change the bounding box size but some reason this does not work with graphs. I have changed the code in my example so you can see for yourself. – Ian Quest Mar 20 '20 at 07:23
  • ph_location_type don't have width and height modifiers: https://davidgohel.github.io/officer/reference/ph_location_type.html. Type the command I wrote upper: `?officer::ph_location_type`. Also, the command to open the manual of `ph_with` is `?officer::ph_with`. The code you modified is still not OK, it does not work because you did not use the correct arguments I will spend time later to answer you. – David Gohel Mar 20 '20 at 09:08

1 Answers1

1

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

David Gohel
  • 9,180
  • 2
  • 16
  • 34
  • Does this control the size that plot text and margins render as? Or os there any way to control that seperately? – geotheory Jun 15 '20 at 11:45