Reputation: 41
I have created a plot in R, and exported it as a vector object in a powerpoint slide (pptx file), using the "rvg" and "officer" packages (see code below).
I would like to define the dimensions (height and width) of the slides in the pptx create by the code, but I cannot see an option to do this. It is possible to specify the size of the plot object exported, but not the size of the powerpoint slide size.
Is it possible to define the size of the powerpoint slide when exporting a plot from R into a pptx file?
Many thanks for advice/help
#EXAMPLE CODE
#IMPORT LIBRARIES
library(officer)
library(rvg)
#MAKE RANDOM DATA
x<-rnorm(50,1,10)
y<-rnorm(50,2,20)
#WRAP PLOT CODE IN A FUNCTION
plot.code<-function(){
plot(x,y)
}
#CREATE AND EXPORT PPTX FILE
export.plot<-read_pptx()
export.plot <- add_slide(export.plot, "Title and Content", "Office Theme")
export.plot<-ph_with_vg(export.plot,code=plot.code(),type="body",height=10,width=10)
#height and width in the above code define the size of the plot, not the slide
print(export.plot, target = "export.plot.pptx")
Upvotes: 4
Views: 485
Reputation: 1917
I have the same question, and haven't yet found a clear answer, but I did figure out a shoddy work-around.
That will look something like this, with a previously created manually_created.pptx
.
doc <- officer::read_pptx("manually_created.pptx") %>%
# remove the empty slide
officer::remove_slide(1)
doc %>%
# add a new slide to the powerpoint
officer::add_slide("Title and Content", "Office Theme") %>%
# add plot to the slide
officer::ph_with_vg(code = plot.code(), type = "body", height = 10, width = 10) %>%
# save the powerpoint
officer:::print.rpptx("manually_created.pptx")
Upvotes: 2