Site icon R-bloggers

Create editable Microsoft Office charts from R

[This article was first published on Revolutions, 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.

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 "how can I tweak that graphic?". I could change the colors or s or dimensions in R, of course, but sometimes people just want to watch the world burn 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 editable. Both options work in conjunction with the "officer" package, which lets you create Word and PowerPoint documents from R

The first option — the rvg package — 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 s, resize, rotate, recolor, or add other annotations. For example, this code creates a 1-slide PowerPoint document with a standard R bar chart: 

library(rvg)
library(ggplot2)
library(officer)

doc <- read_pptx()
doc <- add_slide(doc, layout = "Title and Content", master = "Office Theme")
doc <- ph_with_vg(doc, code = barplot(sample(1:20,10),xlab="Day",ylab="Widgets"),
                  type = "body")
print(doc, target = "my_plot.pptx")

Out of the box, the slide looks like this:

If the recipient wants to color the bars and change the label to Papyrus, they can use the standard tools in Powerpoint. These changes took me about 20 seconds.

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.

Another option is the mschart 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:

  • Bar charts, including grouped bars and vertical and horizontal stacked bars
  • Line charts, including stacked line charts
  • Area charts
  • Scatter plots

You can see several examples in the mschart package vignette. 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 this bar chart in word with a couple of clicks, and even change the labels from French to English using the Edit Data tool.

Like the officer package, the mschart and rvg 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 install.packages function.

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

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.