<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/create-editable-microsoft-office-charts-from-r/"></div></div><div></div></div><div></div></div>
<div>
(This article was first published on <strong><a href="http://blog.revolutionanalytics.com/2017/10/office-charts.html"> Revolutions</a></strong>, and kindly contributed to <a rel="nofollow" href="https://www.r-bloggers.com/">R-bloggers)</a> </div>
<div>
<p>R has a rich and infinitely flexible graphics system, and you can easily embed R graphics into Microsoft Office documents like PowerPoint or Word. The one thing I dread hearing after delivering such a document, though, is &quot;how can I tweak that graphic?&quot;. I could change the colors or fonts or dimensions in R, of course, but sometimes people just want to <span>watch the world burn</span> tweak graphics to their hearts' content. If you're in that situation, you have a couple of options for using R to create Office documents with graphics, and make those graphics <em>editable</em>. Both options work in conjunction with the &quot;officer&quot; package, which lets you <a rel="nofollow" href="http://blog.revolutionanalytics.com/2017/10/officer-powerpoint.html" target="_blank">create Word and PowerPoint documents from R</a>. </p>
<p>The first option — the <a rel="nofollow" href="http://mran.microsoft.com/package/rvg/" target="_blank">rvg package</a> — lets you use traditional R graphics commands, but allows the PowerPoint or Word user to modify the components of those graphics. Labels become text elements; lines, points and bars become shapes; and so on. The recipient can then use the standard Office tools to change fonts, resize, rotate, recolor, or add other annotations. For example, this code creates a 1-slide PowerPoint document with a standard R bar chart: </p>
<div>
<pre>library(rvg)
library(ggplot2)
library(officer)

doc <span>&lt;-</span> read_pptx()
doc <span>&lt;-</span> add_slide(doc, layout <span>=</span> <span>&quot;Title and Content&quot;</span>, master <span>=</span> <span>&quot;Office Theme&quot;</span>)
doc <span>&lt;-</span> ph_with_vg(doc, code <span>=</span> barplot(sample(<span>1</span><span>:</span><span>20</span>,<span>10</span>),xlab<span>=</span><span>&quot;Day&quot;</span>,ylab<span>=</span><span>&quot;Widgets&quot;</span>),
                  type <span>=</span> <span>&quot;body&quot;</span>)
print(doc, target <span>=</span> <span>&quot;my_plot.pptx&quot;</span>)
</pre>
</div>
<p>Out of the box, the slide looks like this:</p>
<p><a rel="nofollow" href="http://revolution-computing.typepad.com/.a/6a010534b1db25970b01b7c92d4d19970b-pi" target="_blank"><img border="0" alt="Rvg-1" title="Rvg-1" src="http://revolution-computing.typepad.com/.a/6a010534b1db25970b01b7c92d4d19970b-800wi"></a></p>
<p>If the recipient wants to color the bars and change the label font to <a rel="nofollow" href="https://www.youtube.com/watch?v=jVhlJNJopOQ" target="_blank">Papyrus</a>, they can use the standard tools in Powerpoint. These changes took me about 20 seconds.</p>
<p><a rel="nofollow" href="http://revolution-computing.typepad.com/.a/6a010534b1db25970b01b8d2b7bb31970c-pi" target="_blank"><img border="0" alt="Rvg-2" title="Rvg-2" src="http://revolution-computing.typepad.com/.a/6a010534b1db25970b01b8d2b7bb31970c-800wi"></a></p>
<p>The downside is that the chart here is just a collection of objects. It's easy to make mistakes (say, to move a single bar to a new position), and charts with many components (like scatterplots with thousands of points) can be unwieldy. It's not possible to change the structure of the chart either, say to switch from a bar chart to a line chart.</p>
<p>Another option is the <a rel="nofollow" href="http://mran.microsoft.com/package/mschart/" target="_blank">mschart</a> package, which allows you to use data in R to create Microsoft Office charts in Word or PowerPoint documents. In this case, you won't be using the standard R graphics commands; instead, you'll be using new R functions to generate the standard Office chart types:</p>
<ul>
<li>Bar charts, including grouped bars and vertical and horizontal stacked bars</li>
<li>Line charts, including stacked line charts</li>
<li>Area charts</li>
<li>Scatter plots</li>
</ul>
<p>You can see several examples in the <a rel="nofollow" href="http://mran.microsoft.com/web/packages/mschart/vignettes/introduction.html" target="_blank">mschart package vignette</a>. While you lose the flexibility of R's graphics system, using the mschart package does mean the recipient can use the standard Office chart tools to reformat the chart while keeping the overall presentation structure. For example, I was able to change the style of <a rel="nofollow" href="https://view.officeapps.live.com/op/view.aspx?src=https://ardata-fr.github.io/mschart/articles/assets/pptx/gallery_bar_01.pptx" target="_blank">this bar chart</a> in word with a couple of clicks, and even change the labels from French to English using the Edit Data tool.</p>
<p><a rel="nofollow" href="http://revolution-computing.typepad.com/.a/6a010534b1db25970b01b8d2b7bc28970c-pi" target="_blank"><img border="0" alt="Mschart1" title="Mschart1" src="http://revolution-computing.typepad.com/.a/6a010534b1db25970b01b8d2b7bc28970c-800wi"></a></p>
<p>Like the <a rel="nofollow" href="http://mran.microsoft.com/package/officer/" target="_blank">officer</a> package, the <a rel="nofollow" href="http://mran.microsoft.com/package/mschart/" target="_blank">mschart</a> and <a rel="nofollow" href="http://mran.microsoft.com/package/rvg/" target="_blank">rvg</a> packages are maintained by David Gohel; Bob Rudis, and Francois Brunetti also contributed. Both packages are available now on CRAN, and you can install them into your R session with the <code>install.packages</code> function.</p>
</div>
	<p>
<div>
<div>To <strong>leave a comment</strong> for the author, please follow the link and comment on their blog: <strong><a href="http://blog.revolutionanalytics.com/2017/10/office-charts.html"> Revolutions</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/CJbxlTRWCPQ" height="1"></a></div>