ggplot2 to PowerPoint in RStudio

[Total: 5    Average: 3.8/5]

R with ggplot2 is capable of producing visually appealing charts and is definitely more versatile than Excel for what concerns graphical representation of data. When it comes to presenting the results of an analysis though, PowerPoint is still the most widely used application, at least in the business environment.

This article shows a workflow to bring your ggplot2 charts to PowerPoint automatically, so you can build your analysis presentation directly from an R script within RStudio.

The tool (actually the package) behind this magic is called R2PPT. R2PPT encapsulates into R code calls to DCOM enabled components that, in Windows and Windows only, allow to manipulate applications like PowerPoint from other applications. In order to work properly, R2PPT relies either on the rcom or on the RDCOMClient package.

As it turns out, rcom is no more available from CRAN, so that is no longer an option. RDCOMClient can be downloaded and installed from the author’s site with the following command:

Once RDCOMClient has been made available to R, it is time to install R2PPT.

Once this is done, we are ready to roll with the creation of PowerPoint presentation with R directly from RStudio. We would like to send to PowerPoint charts in vector format (namely Windows Metafile Format) so that they can be also be edited from within PowerPoint, allowing changes in fonts, colors, sizes, etc.

Here is how to do it.

1) Load your data, do your analysis and produce a chart with ggplot2. The chart will appear in RStudio’s Plots window. For a quick and dirty test chart try this:

The call to print() is not necessary when working from the command line (interactive mode) but it is necessary when running from an R script. It is anyway not necessary to display the chart in RStudio for this method to work, it is enough to save the ggplot2 chart object into a variable (p in this case).

2)  Save the chart in Windows Metafile Format to a temporary file, which we will use later and then delete it. For this purpose we will use ggsave also part of the ggplot2 package.

The name of the temporary file to which the chart is saved is stored in the my_temp_file variable because we will need it later.

3) Now we need to create a new PowerPoint presentation using R2PPT. This only needs to be done once at the beginning. Once the link to the PowerPoint presentation is established, we can add to it as many slides and charts we want.

The previous code initializes the connection to PowerPoint. If PowerPoint is not running already, it will be started. PPT.Init has an optional flag to start PowerPoint in invisible mode.

Note that we had to specify method=”RDCOMClient” otherwise R2PPT tries to use rcom by default.

4) Add a blank slide to the presentation and add the chart to the slide as vector graphics. This can be done with another method of R2PPT.

5) Delete the temporary file.

At this point you can repeat steps 2, 4 and 5 for any new generate chart. You won’t need to repeat step 3.

With this method, the charts in PowerPoint will be editable objects. Just Ungroup them (multiple times if required) until you can access the part you want to change.

Once you are done with all the slides and charts you need, simply save your PowerPoint presentation manually (if visible) or use PPT.SaveAs from R2PPT to do it. You can even start your presentation using PPT.Present. Finally you can close the presentation (and PowerPoint) with PPT.Close.

For more information on all available possibilities, I invite you to read the help file for the R2PPT package.

Happy PowerPoint presentations with R! Till next time.

You may also like

2 Comments

  1. Great explanation!. Do you know if it is possible directly from r, to order more than 1 chart in one power point slide?

    Thanks

Leave a Reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.