Editable Plots from R to PowerPoint

[This article was first published on Laurens Geffert, and kindly contributed to R-bloggers]. (You can report issue about the content on this page here)
Want to share your content on R-bloggers? click here if you have a blog, or here if you don't.

In this post I am giving a quick overview of how to create editable plots
in PowerPoint from R. These plots are comprised of simple vector-based shapes
and thus allow you to change labels, colours, or text position in seconds.
Your project managers will love it!


R allows us to create great visualisations, but in most data science settings
these need to be presented to key stakeholders and decision makers in
presentations or “slideuments”. Having to make small changes to previously
compiled slots can be time consuming and frustrating. A solution to this common
problem is to keep your plots and graphs editable as a group of vector shapes
in PowerPoint. This way project managers or data scientists themselves can make
small changes without having to re-execute a single line of code.


We will use a tidyverse approach for creating the plot. Furthermore, the
officer package enables us to smoothly interact with PowerPoint, and the rvg
package is required to save our plots as editable vector graphs.

<span class="n">library</span><span class="p">(</span><span class="n">tidyverse</span><span class="p">)</span><span class="w">
</span><span class="n">library</span><span class="p">(</span><span class="n">officer</span><span class="p">)</span><span class="w">
</span><span class="n">library</span><span class="p">(</span><span class="n">rvg</span><span class="p">)</span><span class="w">

For demonstration purposes, let’s create a plot using the diamonds dataset.
NB: I am saving the ggplot object to a variable name, but also displaying the
plot when executing the lines by appending the ; ggp at the end.

<span class="c1"># Using diamonds dataset which is shipped with R</span><span class="w">
</span><span class="n">ggp</span><span class="w"> </span><span class="o"><-</span><span class="w"> </span><span class="n">diamonds</span><span class="w"> </span><span class="o">%>%</span><span class="w">
  </span><span class="c1"># Let's simplify things by only considering natural number carats</span><span class="w">
  </span><span class="n">mutate</span><span class="p">(</span><span class="n">carat</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="nf">floor</span><span class="p">(</span><span class="n">carat</span><span class="p">))</span><span class="w"> </span><span class="o">%>%</span><span class="w">
  </span><span class="n">group_by</span><span class="p">(</span><span class="n">carat</span><span class="p">,</span><span class="w"> </span><span class="n">cut</span><span class="p">,</span><span class="w"> </span><span class="n">clarity</span><span class="p">,</span><span class="w"> </span><span class="n">color</span><span class="p">)</span><span class="w"> </span><span class="o">%>%</span><span class="w">
  </span><span class="n">summarise</span><span class="p">(</span><span class="n">price</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="n">mean</span><span class="p">(</span><span class="n">price</span><span class="p">))</span><span class="w"> </span><span class="o">%>%</span><span class="w">
  </span><span class="c1"># Create a plot of price by carat, colour, cut, and clarity</span><span class="w">
  </span><span class="n">ggplot</span><span class="p">(</span><span class="n">aes</span><span class="p">(</span><span class="n">x</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="n">carat</span><span class="p">,</span><span class="w"> </span><span class="n">y</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="n">price</span><span class="p">,</span><span class="w"> </span><span class="n">fill</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="n">color</span><span class="p">))</span><span class="w"> </span><span class="o">+</span><span class="w">
  </span><span class="n">geom_bar</span><span class="p">(</span><span class="n">stat</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="s1">'identity'</span><span class="p">)</span><span class="w"> </span><span class="o">+</span><span class="w">
  </span><span class="n">facet_grid</span><span class="p">(</span><span class="n">cut</span><span class="w"> </span><span class="o">~</span><span class="w"> </span><span class="n">clarity</span><span class="p">)</span><span class="w"> </span><span class="o">+</span><span class="w">
  </span><span class="c1"># Simplify the plot layout a little</span><span class="w">
  </span><span class="n">theme_bw</span><span class="p">()</span><span class="w"> </span><span class="o">+</span><span class="w">
  </span><span class="n">guides</span><span class="p">(</span><span class="n">fill</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="kc">FALSE</span><span class="p">)</span><span class="w"> </span><span class="o">+</span><span class="w">
  </span><span class="n">theme</span><span class="p">(</span><span class="n">panel.grid.major.x</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="n">element_blank</span><span class="p">(),</span><span class="w">
        </span><span class="n">panel.grid.minor.x</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="n">element_blank</span><span class="p">());</span><span class="w"> </span><span class="n">ggp</span><span class="w">

Plot example

Now we can use officer to create a new PowerPoint document and
rvg::ph_with_vg to drop our ggplot object in there.

<span class="c1"># Create a new powerpoint document</span><span class="w">
</span><span class="n">doc</span><span class="w"> </span><span class="o"><-</span><span class="w"> </span><span class="n">read_pptx</span><span class="p">()</span><span class="w">
</span><span class="n">doc</span><span class="w"> </span><span class="o"><-</span><span class="w"> </span><span class="n">add_slide</span><span class="p">(</span><span class="n">doc</span><span class="p">,</span><span class="w"> </span><span class="s1">'Title and Content'</span><span class="p">,</span><span class="w"> </span><span class="s1">'Office Theme'</span><span class="p">)</span><span class="w">

</span><span class="c1"># Add the plot</span><span class="w">
</span><span class="n">doc</span><span class="w"> </span><span class="o"><-</span><span class="w"> </span><span class="n">ph_with_vg</span><span class="p">(</span><span class="n">doc</span><span class="p">,</span><span class="w"> </span><span class="n">ggobj</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="n">ggp</span><span class="p">,</span><span class="w"> </span><span class="n">type</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="s1">'body'</span><span class="p">)</span><span class="w">  

</span><span class="c1"># Write the document to a file</span><span class="w">
</span><span class="n">print</span><span class="p">(</span><span class="n">doc</span><span class="p">,</span><span class="w"> </span><span class="n">target</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="s1">'plot.pptx'</span><span class="p">)</span><span class="w">

Now open the document in PowerPoint. Right-click and ungroup the plot. Voila!
You should be able to select individual elements, for example the data bars in
the plot, change their colour, move them around, change the text in labels,
and much more. Have a look at the plot below. A cookie for you if you can
find all ten edits that I made in the example.

Plot example

As always, hope this is helpful. And FYI, I am still looking for a way to
achieve the same result using Python. If you know one, collect some bounty

To leave a comment for the author, please follow the link and comment on their blog: Laurens Geffert.

R-bloggers.com offers daily e-mail updates about R news and tutorials about learning R and many other topics. Click here if you're looking to post or find an R/data-science job.
Want to share your content on R-bloggers? click here if you have a blog, or here if you don't.

Never miss an update!
Subscribe to R-bloggers to receive
e-mails with the latest R posts.
(You will not see this message again.)

Click here to close (This popup will not appear again)