Fill an Entire PowerPoint Slide with an R Plot?

2019-07-26 17:33发布

问题:

How can I fill an entire PowerPoint slide with an R plot? I'd like to use vector graphics (otherwise screenshot and crop would be a solution). I'd also like to avoid manual touches.

The following code (grabbed from this post) does a perfectly fine job of filling the "Content" of a "Title and Content" slide.

library( ReporteRs )
require( ggplot2 )
mydoc = pptx( )
mydoc = addSlide( mydoc, slide.layout = "Title and Content" )
mydoc = addTitle( mydoc, "Plot examples" )
myplot = qplot(Sepal.Length, Petal.Length
               , data = iris, color = Species
               , size = Petal.Width, alpha = I(0.7)
)
mydoc = addPlot( mydoc, function( ) print( myplot ), vector.graphic=TRUE)
writeDoc( mydoc, file = "test plot.pptx" )

I thought I might be able to fill an entire slide using the following code:

library( ReporteRs )
require( ggplot2 )
mydoc = pptx( )
mydoc = addSlide( mydoc, slide.layout = "Blank" )
#mydoc = addTitle( mydoc, "Plot examples" )
myplot = qplot(Sepal.Length, Petal.Length
               , data = iris, color = Species
               , size = Petal.Width, alpha = I(0.7)
)
mydoc = addPlot( mydoc, function( ) print( myplot ), vector.graphic=TRUE)
writeDoc( mydoc, file = "test plot.pptx" )

But, i get the error message:

Error in next_shape_pos(doc) : 
  shape of type 'plot' has no more room left to be displayed in the layout

I think there may be a solution where I create new PowerPoint slide layout with content filling the entire slide and then save as a PowerPoint template? But I'm not very familiar with this stuff and am starting to feel like I'm missing something obvious.

回答1:

You have effectively answered your own question. You need to create a template in PowerPoint with a new layout that has a single content placeholder filling the slide.

In PowerPoint 2016, you would do the following:

  • File -> New -> Blank presentation
  • Delete the first (and only) slide
  • View -> Slide Master
  • Slide Master -> Insert Layout
  • Delete all existing placeholders on this layout
  • Slide Master -> Insert Placeholder -> Content
  • Resize the new placeholder to fill the slide
  • Right click on the layout in the left-hand layout list
  • Rename layout
  • "Full page content"
  • File -> Save as -> Browse
  • Choose a location and name for the presentation template (e.g. "Documents/R/BlankPresentation.pptx")

Now run the following R code:

library(ReporteRs)
library(ggplot2)
mydoc <- pptx(template = "path to presentation template saved above")
mydoc <- addSlide(mydoc, slide.layout = "Full page content")
myplot <- qplot(
  Sepal.Length,
  Petal.Length,
  data = iris,
  color = Species,
  size = Petal.Width,
  alpha = 0.7
)
mydoc <- addPlot(mydoc, function() print(myplot), vector.graphic = TRUE)
writeDoc(mydoc, file = "test plot.pptx")