<p><div><div><div><div data-show-faces="false" data-height="21" data-width="100" data-layout="button_count" data-send="true" data-href="https://www.r-bloggers.com/creating-and-saving-multiple-plots-to-powerpoint/"></div></div><div></div></div><div></div></div>
<div>
(This article was first published on <strong><a href="https://www.johnmackintosh.com/2018-11-30-multiple-plots-to-powerpoint/"> HighlandR</a></strong>, and kindly contributed to <a rel="nofollow" href="https://www.r-bloggers.com/">R-bloggers)</a> </div>
<p>At the NHS R conference we delivered a session on animating patient flow. This started with a single plot showing all patient movements, and then I demonstrated the ability to create a faceted plot.</p>
<p>But, with many different areas, and a small plot space, the faceted plot was a bit meh really.</p>
<p>So what else could we do?</p>
<p>How about creating individual plots for each ward area? And saving them into Powerpoint for onward distribution?</p>
<p>All the files you need to recreate this are on the <a rel="nofollow" href="https://github.com/johnmackintosh/NHS_R" target="_blank">repo</a>.<br>
If all goes well, you should create a series of images, and also save these to a powerpoint ready for onward distribution to the sort of folk who think spreadsheets are where it’s at.</p>
<div>
<div>
<pre><code><span># some wards have very few movements, but how many?</span><span>
</span><span>source</span><span>(</span><span>&quot;1_setup.R&quot;</span><span>)</span><span>
</span><span>source</span><span>(</span><span>&quot;2_data_wrangling.R&quot;</span><span>)</span><span>
</span><span>library</span><span>(</span><span>officer</span><span>)</span><span>
</span><span>library</span><span>(</span><span>magrittr</span><span>)</span><span>
</span><span>setwd</span><span>(</span><span>here</span><span>::</span><span>here</span><span>())</span><span>
</span><span># how many obs per group?</span><span>
</span><span># base R:</span><span>
</span><span>table</span><span>(</span><span>data</span><span>$</span><span>Ward_Dept</span><span>)</span><span>
</span><span># succinct, but the output is not great..how it looks, structure etc</span><span>
</span><span># using dplyr we get a tibble, which is a more useful output</span><span>
</span><span>plot_data</span><span> </span><span>%>%</span><span> </span><span>group_by</span><span>(</span><span>Ward_Dept</span><span>)</span><span> </span><span>%>%</span><span> </span><span>count</span><span>()</span><span> </span><span>%>%</span><span> </span><span>#arrange in descending order</span><span>
</span><span>arrange</span><span>(</span><span>desc</span><span>(</span><span>n</span><span>))</span><span>
</span><span>## create a list of distinct ward names</span><span>
</span><span>places</span><span> </span><span>&lt;-</span><span> </span><span>plot_data</span><span> </span><span>%>%</span><span> </span><span>distinct</span><span>(</span><span>Ward_Dept</span><span>)</span><span> </span><span>%>%</span><span> </span><span>rename</span><span>(</span><span>Location</span><span> </span><span>=</span><span> </span><span>Ward_Dept</span><span>)</span><span>
</span><span>#create plot function </span><span>
</span><span>png_plot</span><span> </span><span>&lt;-</span><span> </span><span>function</span><span>(</span><span>Location</span><span>)</span><span> </span><span>{</span><span>
</span><span>plot_colours</span><span> </span><span>&lt;-</span><span> </span><span>c</span><span>(</span><span>&quot;orangered1&quot;</span><span>,</span><span>&quot;royalblue2&quot;</span><span>,</span><span>&quot;grey60&quot;</span><span>)</span><span>
</span><span>tempdf</span><span> </span><span>&lt;-</span><span> </span><span>plot_data</span><span> </span><span>%>%</span><span> </span><span>filter</span><span>(</span><span>Ward_Dept</span><span> </span><span>==</span><span> </span><span>Location</span><span>)</span><span> </span><span>ggplot</span><span>(</span><span>plot_data</span><span>,</span><span>aes</span><span>(</span><span>Movement15</span><span>,</span><span> </span><span>Movement_15_SEQNO</span><span>,</span><span>colour</span><span> </span><span>=</span><span> </span><span>Movement_Type</span><span>))</span><span> </span><span>+</span><span>
</span><span>geom_point</span><span>(</span><span>alpha</span><span> </span><span>=</span><span> </span><span>0</span><span>)</span><span> </span><span>+</span><span>
</span><span>geom_point</span><span>(</span><span>data</span><span> </span><span>=</span><span> </span><span>tempdf</span><span>,</span><span>aes</span><span>(</span><span>Movement15</span><span>,</span><span>Movement_15_SEQNO</span><span>,</span><span>colour</span><span> </span><span>=</span><span> </span><span>Movement_Type</span><span>),</span><span> </span><span>na.rm</span><span> </span><span>=</span><span> </span><span>FALSE</span><span>)</span><span> </span><span>+</span><span>
</span><span>scale_colour_manual</span><span>(</span><span>values</span><span> </span><span>=</span><span> </span><span>plot_colours</span><span>,</span><span>drop</span><span> </span><span>=</span><span> </span><span>FALSE</span><span>)</span><span> </span><span>+</span><span>
</span><span>scale_x_datetime</span><span>(</span><span>date_labels</span><span> </span><span>=</span><span> </span><span>&quot;%H:%M&quot;</span><span>,</span><span>date_breaks</span><span> </span><span>=</span><span> </span><span>&quot;3 hours&quot;</span><span>,</span><span>
</span><span>limits</span><span> </span><span>=</span><span> </span><span>lims</span><span>,</span><span>
</span><span>timezone</span><span> </span><span>=</span><span> </span><span>&quot;GMT&quot;</span><span>,</span><span>
</span><span>expand</span><span> </span><span>=</span><span> </span><span>c</span><span>(</span><span>0</span><span>,</span><span>0</span><span>))</span><span> </span><span>+</span><span>
</span><span>scale_y_continuous</span><span>(</span><span>breaks</span><span> </span><span>=</span><span> </span><span>seq</span><span>(</span><span>-15</span><span>,</span><span>15</span><span>,</span><span> </span><span>by</span><span> </span><span>=</span><span> </span><span>5</span><span>),</span><span> </span><span>limits</span><span> </span><span>=</span><span> </span><span>c</span><span>(</span><span>-15</span><span>,</span><span>15</span><span>))</span><span> </span><span>+</span><span>
</span><span>expand_limits</span><span>(</span><span>y</span><span> </span><span>=</span><span> </span><span>c</span><span>(</span><span>-15</span><span>,</span><span> </span><span>15</span><span>))</span><span> </span><span>+</span><span>
</span><span>ggtitle</span><span>(</span><span>label</span><span> </span><span>=</span><span> </span><span>&quot;Anytown General Hospital | Wednesday 3rd September 2014 00:00 to 23:59\n&quot;</span><span>,</span><span>
</span><span>subtitle</span><span> </span><span>=</span><span> </span><span>paste0</span><span>(</span><span>Location</span><span>,</span><span>&quot; ARRIVALS, DEPARTURES AND TRANSFERS&quot;</span><span>))</span><span> </span><span>+</span><span>
</span><span>labs</span><span>(</span><span>x</span><span> </span><span>=</span><span> </span><span>NULL</span><span>,</span><span> </span><span>y</span><span> </span><span>=</span><span> </span><span>NULL</span><span>,</span><span>caption</span><span> </span><span>=</span><span> </span><span>&quot;NHS-R conference&quot;</span><span>)</span><span> </span><span>+</span><span>
</span><span>theme_minimal</span><span>(</span><span>base_size</span><span> </span><span>=</span><span> </span><span>11</span><span>)</span><span> </span><span>+</span><span>
</span><span>theme</span><span>(</span><span>legend.position</span><span> </span><span>=</span><span> </span><span>&quot;bottom&quot;</span><span>)</span><span> </span><span>+</span><span>
</span><span>theme</span><span>(</span><span>panel.grid.minor</span><span> </span><span>=</span><span> </span><span>element_blank</span><span>())</span><span> </span><span>+</span><span>
</span><span>theme</span><span>(</span><span>strip.text.y</span><span> </span><span>=</span><span> </span><span>element_text</span><span>(</span><span>angle</span><span> </span><span>=</span><span> </span><span>180</span><span>))</span><span> </span><span>+</span><span>
</span><span>guides</span><span>(</span><span>color</span><span> </span><span>=</span><span> </span><span>guide_legend</span><span>(</span><span>&quot;Movement Type&quot;</span><span>))</span><span>
</span><span>ggsave</span><span>(</span><span>filename</span><span> </span><span>=</span><span> </span><span>paste0</span><span>(</span><span>Location</span><span>,</span><span>&quot;.png&quot;</span><span>),</span><span>
</span><span>width</span><span> </span><span>=</span><span> </span><span>10</span><span>,</span><span> </span><span>height</span><span> </span><span>=</span><span> </span><span>8</span><span>)</span><span> </span><span>}</span><span>
</span><span># create a folder to store .png files</span><span>
</span><span>dir.create</span><span>(</span><span>here</span><span>::</span><span>here</span><span>(</span><span>&quot;png&quot;</span><span>))</span><span>
</span><span>setwd</span><span>(</span><span>here</span><span>::</span><span>here</span><span>(</span><span>&quot;png&quot;</span><span>))</span><span>
</span><span>## any errors here - did you create the folder in the first place?</span><span>
</span><span># now use the walk function from purrr to create an image per location</span><span>
</span><span>walk</span><span>(</span><span>places</span><span>$</span><span>Location</span><span>,</span><span>png_plot</span><span>)</span><span>
</span><span>#check your &quot;.png&quot; folder</span><span>
</span><span>#now copy a blank powerpoint template from home directory to current &quot;png&quot; directory</span><span>
</span><span># file.copy(where from, what, where to)</span><span>
</span><span>file.copy</span><span>(</span><span>file.path</span><span>(</span><span>here</span><span>::</span><span>here</span><span>(),</span><span>&quot;blank.pptx&quot;</span><span>),</span><span> </span><span>getwd</span><span>())</span><span>
</span><span>############## automating powerpoint slides using officer ###############</span><span>
</span><span># Set a footer</span><span>
</span><span>set_ftr</span><span> </span><span>&lt;-</span><span> </span><span>&quot;NHS_R Conference Oct 2018&quot;</span><span>
</span><span>set_pres</span><span> </span><span>&lt;-</span><span> </span><span>read_pptx</span><span>(</span><span>&quot;blank.pptx&quot;</span><span>)</span><span> </span><span>%>%</span><span> </span><span># Load template Add a slide</span><span>
</span><span>add_slide</span><span>(</span><span>layout</span><span> </span><span>=</span><span> </span><span>&quot;Title Slide&quot;</span><span>,</span><span> </span><span>master</span><span> </span><span>=</span><span> </span><span>&quot;Office Theme&quot;</span><span>)</span><span> </span><span>%>%</span><span> </span><span># Add some text to the title (ctrTitle)</span><span>
</span><span>ph_with_text</span><span>(</span><span>type</span><span> </span><span>=</span><span> </span><span>&quot;ctrTitle&quot;</span><span>,</span><span> </span><span>str</span><span> </span><span>=</span><span> </span><span>&quot;Drill Down to Individual Location&quot;</span><span>)</span><span> </span><span>%>%</span><span> </span><span># Add some text to the subtitle (subTitle)</span><span>
</span><span>ph_with_text</span><span>(</span><span>type</span><span> </span><span>=</span><span> </span><span>&quot;subTitle&quot;</span><span>,</span><span> </span><span>str</span><span> </span><span>=</span><span> </span><span>&quot;Individual Location Plots&quot;</span><span>)</span><span> </span><span>%>%</span><span> </span><span>ph_with_text</span><span>(</span><span>type</span><span> </span><span>=</span><span> </span><span>&quot;ftr&quot;</span><span>,</span><span> </span><span>str</span><span> </span><span>=</span><span> </span><span>set_ftr</span><span>)</span><span>
</span><span>slidef</span><span> </span><span>&lt;-</span><span> </span><span>function</span><span>(</span><span>places</span><span>,</span><span> </span><span>pres</span><span> </span><span>=</span><span> </span><span>set_pres</span><span>)</span><span> </span><span>{</span><span>
</span><span>set_pres</span><span> </span><span>%>%</span><span> </span><span>add_slide</span><span>(</span><span>layout</span><span> </span><span>=</span><span> </span><span>&quot;Title and Content&quot;</span><span>,</span><span> </span><span>master</span><span> </span><span>=</span><span> </span><span>&quot;Office Theme&quot;</span><span>)</span><span> </span><span>%>%</span><span>
</span><span>ph_with_text</span><span>(</span><span>type</span><span> </span><span>=</span><span> </span><span>&quot;ftr&quot;</span><span>,</span><span> </span><span>str</span><span> </span><span>=</span><span> </span><span>set_ftr</span><span>)</span><span> </span><span>%>%</span><span> </span><span>ph_with_img</span><span>(</span><span>type</span><span> </span><span>=</span><span> </span><span>&quot;body&quot;</span><span>,</span><span> </span><span>index</span><span> </span><span>=</span><span> </span><span>1</span><span>,</span><span> </span><span>src</span><span> </span><span>=</span><span> </span><span>paste0</span><span>(</span><span>places</span><span>,</span><span> </span><span>&quot;.png&quot;</span><span>))</span><span> </span><span>-></span><span> </span><span>set_pres</span><span> </span><span># get images </span><span>
</span><span>}</span><span>
</span><span>walk</span><span>(</span><span>places</span><span>$</span><span>Location</span><span>,</span><span> </span><span>slidef</span><span>)</span><span>
</span><span>set_pres</span><span> </span><span>%>%</span><span> </span><span>print</span><span>(</span><span>target</span><span> </span><span>=</span><span> </span><span>&quot;All Wards.pptx&quot;</span><span>)</span><span> </span><span>%>%</span><span> </span><span>invisible</span><span>()</span><span>
</span><span>## hooray</span><span>
</span><span># move the powerpoints elsewhere, then delete them from current folder</span><span>
</span><span>filestomove</span><span> </span><span>&lt;-</span><span> </span><span>c</span><span>(</span><span>&quot;All Wards.pptx&quot;</span><span>,</span><span>&quot;blank.pptx&quot;</span><span>)</span><span>
</span><span>file.copy</span><span>(</span><span>file.path</span><span>(</span><span>getwd</span><span>(),</span><span>filestomove</span><span>),</span><span> </span><span>here</span><span>::</span><span>here</span><span>())</span><span>
</span><span>file.remove</span><span>(</span><span>filestomove</span><span>)</span><span>
</span><span>#make sure you are still in the png folder</span><span>
</span><span>getwd</span><span>()</span><span>
</span><span>filenames</span><span> </span><span>&lt;-</span><span> </span><span>dir</span><span>()</span><span>
</span><span>library</span><span>(</span><span>gifski</span><span>)</span><span>
</span><span>gifski</span><span>(</span><span>png_files</span><span> </span><span>=</span><span> </span><span>filenames</span><span>,</span><span> </span><span>gif_file</span><span> </span><span>=</span><span> </span><span>&quot;Location_animation.gif&quot;</span><span>,</span><span> </span><span>width</span><span> </span><span>=</span><span> </span><span>800</span><span>,</span><span> </span><span>height</span><span> </span><span>=</span><span> </span><span>600</span><span>,</span><span>
</span><span>delay</span><span> </span><span>=</span><span> </span><span>1</span><span>,</span><span> </span><span>loop</span><span> </span><span>=</span><span> </span><span>TRUE</span><span>,</span><span> </span><span>progress</span><span> </span><span>=</span><span> </span><span>TRUE</span><span>)</span><span>
</span><span>utils</span><span>::</span><span>browseURL</span><span>(</span><span>&quot;Location_animation.gif&quot;</span><span>)</span><span>
</span><span># stick this in a powerpoint too?</span><span>
</span><span>file.copy</span><span>(</span><span>file.path</span><span>(</span><span>here</span><span>::</span><span>here</span><span>(),</span><span>&quot;blank.pptx&quot;</span><span>),</span><span> </span><span>getwd</span><span>())</span><span>
</span><span># Set a footer</span><span>
</span><span>set_ftr</span><span> </span><span>&lt;-</span><span> </span><span>&quot;NHS_R Conference Oct 2018&quot;</span><span>
</span><span>set_pres</span><span> </span><span>&lt;-</span><span> </span><span>read_pptx</span><span>(</span><span>&quot;blank.pptx&quot;</span><span>)</span><span> </span><span>%>%</span><span> </span><span># Load template Add a slide</span><span>
</span><span>add_slide</span><span>(</span><span>layout</span><span> </span><span>=</span><span> </span><span>&quot;Title Slide&quot;</span><span>,</span><span> </span><span>master</span><span> </span><span>=</span><span> </span><span>&quot;Office Theme&quot;</span><span>)</span><span> </span><span>%>%</span><span> </span><span># Add some text to the title (ctrTitle)</span><span>
</span><span>ph_with_text</span><span>(</span><span>type</span><span> </span><span>=</span><span> </span><span>&quot;ctrTitle&quot;</span><span>,</span><span> </span><span>str</span><span> </span><span>=</span><span> </span><span>&quot;Individual Location Animation&quot;</span><span>)</span><span> </span><span>%>%</span><span> </span><span># Add some text to the subtitle (subTitle)</span><span>
</span><span>ph_with_text</span><span>(</span><span>type</span><span> </span><span>=</span><span> </span><span>&quot;subTitle&quot;</span><span>,</span><span> </span><span>str</span><span> </span><span>=</span><span> </span><span>&quot;Individual Location Plots gif&quot;</span><span>)</span><span> </span><span>%>%</span><span> </span><span>ph_with_text</span><span>(</span><span>type</span><span> </span><span>=</span><span> </span><span>&quot;ftr&quot;</span><span>,</span><span> </span><span>str</span><span> </span><span>=</span><span> </span><span>set_ftr</span><span>)</span><span>
</span><span>set_pres</span><span> </span><span>%>%</span><span> </span><span>add_slide</span><span>(</span><span>layout</span><span> </span><span>=</span><span> </span><span>&quot;Title and Content&quot;</span><span>,</span><span> </span><span>master</span><span> </span><span>=</span><span> </span><span>&quot;Office Theme&quot;</span><span>)</span><span> </span><span>%>%</span><span>
</span><span>ph_with_text</span><span>(</span><span>type</span><span> </span><span>=</span><span> </span><span>&quot;ftr&quot;</span><span>,</span><span> </span><span>str</span><span> </span><span>=</span><span> </span><span>set_ftr</span><span>)</span><span> </span><span>%>%</span><span> </span><span>ph_with_img</span><span>(</span><span>type</span><span> </span><span>=</span><span> </span><span>&quot;body&quot;</span><span>,</span><span> </span><span>index</span><span> </span><span>=</span><span> </span><span>1</span><span>,</span><span> </span><span>src</span><span> </span><span>=</span><span> </span><span>&quot;Location_animation.gif&quot;</span><span>)</span><span> </span><span>-></span><span> </span><span>set_pres</span><span> </span><span># get images </span><span>
</span><span>set_pres</span><span> </span><span>%>%</span><span> </span><span>print</span><span>(</span><span>target</span><span> </span><span>=</span><span> </span><span>&quot;Animation.pptx&quot;</span><span>)</span><span> </span><span>%>%</span><span> </span><span>invisible</span><span>()</span><span>
</span><span># move the powerpoints to the root of the folder, then delete them from current folder</span><span>
</span><span>filestomove</span><span> </span><span>&lt;-</span><span> </span><span>c</span><span>(</span><span>&quot;Animation.pptx&quot;</span><span>,</span><span>&quot;Location_animation.gif&quot;</span><span>,</span><span>&quot;blank.pptx&quot;</span><span>)</span><span>
</span><span>file.copy</span><span>(</span><span>file.path</span><span>(</span><span>getwd</span><span>(),</span><span>filestomove</span><span>),</span><span> </span><span>here</span><span>::</span><span>here</span><span>())</span><span>
</span><span>file.remove</span><span>(</span><span>filestomove</span><span>)</span><span>
</span><span>#go back home</span><span>
</span><span>setwd</span><span>(</span><span>here</span><span>::</span><span>here</span><span>())</span><span>
</span></code></pre>
</div>
</div>
<p>There you have it.<br>
Credit to Len Keifer, who’s code I borrowed liberally (found it, pinched it) in order to create the slides in powerpoint.<br>
These techniques have saved me a lot of time each month – with a small bit of setup time, regular reporting can be automated, giving you time to work on the harder stuff.</p>
	<p>
<div>
<div>To <strong>leave a comment</strong> for the author, please follow the link and comment on their blog: <strong><a href="https://www.johnmackintosh.com/2018-11-30-multiple-plots-to-powerpoint/"> HighlandR</a></strong>.</div>
<hr>
<a rel="nofollow" href="https://www.r-bloggers.com/">R-bloggers.com</a> offers <strong><a rel="nofollow" href="https://feedburner.google.com/fb/a/mailverify?uri=RBloggers">daily e-mail updates</a></strong> about <a rel="nofollow" href="https://www.r-project.org/" title="The R Project for Statistical Computing">R</a> news and <a rel="nofollow" href="https://www.r-bloggers.com/search/tutorial" title="R tutorials">tutorials</a> on topics such as: <a rel="nofollow" href="https://www.r-bloggers.com/search/data%20science" title="Data science">Data science</a>, <a rel="nofollow" href="https://www.r-bloggers.com/search/Big%20Data" title="Big Data">Big Data, <a rel="nofollow" href="https://www.r-users.com/" title="R jobs">R jobs</a>, visualization (<a rel="nofollow" href="https://www.r-bloggers.com/search/ggplot2" title="ggplot and ggplot2 tutorials">ggplot2</a>, <a rel="nofollow" href="https://www.r-bloggers.com/search/boxplot" title="Boxplots using lattice and ggplot2 tutorials">Boxplots</a>, <a rel="nofollow" href="https://www.r-bloggers.com/search/map" title="Maps and gis">maps</a>, <a rel="nofollow" href="https://www.r-bloggers.com/search/animation" title="Animation in R">animation</a>), programming (<a rel="nofollow" href="https://www.r-bloggers.com/search/RStudio" title="RStudio IDE for R">RStudio</a>, <a rel="nofollow" href="https://www.r-bloggers.com/search/sweave" title="Sweave and literate programming">Sweave</a>, <a rel="nofollow" href="https://www.r-bloggers.com/search/LaTeX" title="LaTeX in R">LaTeX</a>, <a rel="nofollow" href="https://www.r-bloggers.com/search/SQL" title="SQL and databases">SQL</a>, <a rel="nofollow" href="https://www.r-bloggers.com/search/eclipse" title="Eclipse IDE for R">Eclipse</a>, <a rel="nofollow" href="https://www.r-bloggers.com/search/git" title="git and github, Version Control System">git</a>, <a rel="nofollow" href="https://www.r-bloggers.com/search/hadoop" title="Large data in R using Hadoop">hadoop</a>, <a rel="nofollow" href="https://www.r-bloggers.com/search/Web+Scraping" title="Web Scraping of google, facebook, yahoo, twitter and more using R">Web Scraping</a>) statistics (<a rel="nofollow" href="https://www.r-bloggers.com/search/regression" title="Regressions and ANOVA analysis tutorials">regression</a>, <a rel="nofollow" href="https://www.r-bloggers.com/search/PCA" title="principal component analysis tutorial">PCA</a>, <a rel="nofollow" href="https://www.r-bloggers.com/search/time+series" title="Time series">time series</a>, <a rel="nofollow" href="https://www.r-bloggers.com/search/trading" title="finance trading">trading</a>) and more...
<img width="1" alt="" src="http://feeds.feedburner.com/~r/RBloggers/~4/zFHvInNfSpo" height="1"></a></div>