<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/exporting-editable-plots-from-r-to-excel-making-ggplot2-purrr-with-officer/"></div></div><div></div></div><div></div></div>
<div>
(This article was first published on <strong><a href="http://www.brodrigues.co/blog/2018-10-05-ggplot2_purrr_officer/"> Econometrics and Free Software</a></strong>, and kindly contributed to <a rel="nofollow" href="https://www.r-bloggers.com/">R-bloggers)</a> </div>
<div>
<p><a rel="nofollow" href="https://www.youtube.com/watch?v=oc9XOxUcvLY" target="_blank"><br>
<img src="https://i0.wp.com/www.brodrigues.co/blog/2018-10-05-ggplot2_purrr_officer/img/officer_meme.jpg?w=456" data-recalc-dims="1"></a></p>
</div>
<p>I was recently confronted to the following problem: creating hundreds of plots that could still be<br>
edited by our client. What this meant was that I needed to export the graphs in Excel or Powerpoint<br>
or some other such tool that was familiar to the client, and not export the plots directly to pdf or<br>
png as I would normally do. I still wanted to use R to do it though, because I could do what I always<br>
do to when I need to perform repetitive tasks such as producing hundreds of plots; map over a list<br>
of, say, countries, and make one plot per country. This is something I discussed in a previous<br>
blog post, <a rel="nofollow" href="http://www.brodrigues.co/blog/2017-03-29-make-ggplot2-purrr/" target="_blank">Make ggplot2 purrr</a>.</p>
<p>So, after some online seaching, I found the <code>{officer}</code> package. This package allows you to put<br>
objects into Microsoft documents. For example, editable plots in a Powerpoint document. This is what<br>
I will show in this blog post.</p>
<p>Let’s start by loading the required packages:</p>
<pre><code>library(&quot;tidyverse&quot;)
library(&quot;officer&quot;)
library(&quot;rvg&quot;)</code></pre>
<p>Then, I will use the data from the time use survey, which I discussed in a previous blog post<br>
<a rel="nofollow" href="http://www.brodrigues.co/blog/2018-09-11-human_to_machine/" target="_blank">Going from a human readable Excel file to a machine-readable csv with {tidyxl}</a>.</p>
<p>You can download the data <a rel="nofollow" href="https://github.com/rbind/b-rodrigues.github.com/blob/master/content/blog/clean_data.csv" target="_blank">here</a>.</p>
<p>Let’s import and prepare it:</p>
<pre><code>time_use &lt;- rio::import(&quot;clean_data.csv&quot;)


time_use &lt;- time_use %>%
    filter(population %in% c(&quot;Male&quot;, &quot;Female&quot;)) %>%
    filter(activities %in% c(&quot;Personal care&quot;, &quot;Sleep&quot;, &quot;Eating&quot;, 
                             &quot;Employment&quot;, &quot;Household and family care&quot;)) %>%
    group_by(day) %>%
    nest()</code></pre>
<p>I only kept two categories, “Male” and “Female” and 5 activities. Then I grouped by day and nested<br>
the data. This is how it looks like:</p>
<pre><code>time_use</code></pre>
<pre><code>## # A tibble: 3 x 2
##   day                         data             
##   </code></pre>