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 fonts 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 fonts, 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 font 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.
Nice and effective. Thank you very much. However, R2PPT package is also helpful.
https://m.youtube.com/watch?v=F7FW_kRk1Sw
Posted by: Trip2R | October 24, 2017 at 17:26
This is great. My team has used Office for eons, leadership has been resistant to ggplot2 charts because they "look different". I'm going to try to create a chart that includes a forecast model.
Posted by: Chris | October 25, 2017 at 00:11
This looks incredible, good work all! Going to give it a go this month for some clients.
Posted by: Appupio | October 25, 2017 at 11:40
@Chris: https://cran.r-project.org/web/packages/ggthemes/ can make ggplot2 graphics follow other styles, too, but it doesn't add editability. While ggthemes doesn't cover MS Office well, I suspect other themes could be added, either by submitting an issue or perhaps by a pull request.
Posted by: Bill Harris | November 16, 2017 at 08:50