3

So I need to copy and paste plots I make in R/Rstudio to PowerPoint (Unfortunately my government client requires everything to be in PowerPoint, otherwise this question could easily be solved by just putting my graphics in an Rmarkdown file). This hasn't been that much of an issue until recently where I discovered that transparent objects can't be copy and pasted to PowerPoint, at least in the method I've been using (Export > Copy as Metafile > Copy Plot > Paste). Instead, where the transparent objects were, there's now nothing. I have done some reading and found that this has to do with my "graphics engine" not supporting transparent objects and that a "cairo" engine can but I don't understand what that means or if it's an option I can change in Rstudio or PowerPoint.

Here's some example code:

iris %>%
mutate(small_petal = if_else(Petal.Width > 1.5, "big", "small")) %>%
ggplot(aes(x = Sepal.Length, y = Sepal.Width, color = Species, alpha = small_petal)) +
geom_point()

enter image description here

I've tried a pretty extravagant workaround to get the colors I want. I've made custom RGB values which are non-transparent to mimic the transparency I want. I then attach them to the data.frame using mutate with four if_elses (one color for each alpha-color combination). I then use scale_fill_identity to apply my custom colors but it then produces a legend with elements I don't want. I need to use the following to make a legend appear with at least some of the elements I want:

scale_fill_identity(guide = "legend",
                    labels = c("DUMMY LABEL",
                               "DUMMY LABEL",
                               "Small Petal",
                               "Big Petal"),
                    breaks = unique(iris$custom_colors))

I then delete the legend objects I don't want in PowerPoint. Any help is greatly appreciated.

Ben G
  • 4,148
  • 2
  • 22
  • 42

3 Answers3

5

Have you tried rendering directly to PowerPoint from RMarkdown? That feature was added pretty recently You'll have to install the dev version of both RMarkdown and RStudio to get it. Instructions in the prior link, but the tldr; is as follows:

  1. latest rmarkdown: devtools::install_github("rstudio/rmarkdown")
  2. Install latest RStudio Preview: https://www.rstudio.com/products/rstudio/download/preview/

When I tried it on my Mac, I think the output looks pretty good:

enter image description here

My RMarkdown test was as follows:

---
title: "testing ppt"
author: "JD Long"
date: "8/3/2018"
output: powerpoint_presentation
---

```{r setup, include=FALSE}
knitr::opts_chunk$set(echo = FALSE)
```

## Test chunk

```{r iris, echo = TRUE}
library(tidyverse)
iris %>%
  mutate(small_petal = if_else(Petal.Width > 1.5, "big", "small")) %>%
  ggplot(aes(x = Sepal.Length, y = Sepal.Width, color = Species, alpha = small_petal)) +
  geom_point()
```
JD Long
  • 59,675
  • 58
  • 202
  • 294
  • That is pretty cool and would be very helpful. I liked the copy as metafile because it imports everything as microsoft objects so easier to change things in PowerPoint. Unfortunately, I'm on my government client's system, which (surprise surprise) is using an older version of Rstudio and Rmarkdown and everything I use either has to be whitelisted or get approval. And I can't get approval for things that are still in dev. – Ben G Aug 03 '18 at 16:41
  • 1
    ahhh... sorry to provide an untenable solution. I was afraid that might be the case. – JD Long Aug 03 '18 at 18:43
3

Rendering directly as @JD Long mentions is probably the way to go, but as an alternative, Cairo might work:

p = iris %>%
  mutate(small_petal = if_else(Petal.Width > 1.5, "big", "small")) %>%
  ggplot(aes(x = Sepal.Length, y = Sepal.Width, color = Species, alpha = small_petal)) +
  geom_point() +
  theme_bw()   

ggsave(p, filename = "a.png", type = "cairo")

I also added theme_bw(), transparency looks better on white background

Dieter Menne
  • 10,076
  • 44
  • 67
  • Thanks so much, for more context see above. This still renders it as a png though, right? What's great about copying as a metafile and then pasting it is that it imports everything as a powerpoint object. – Ben G Aug 03 '18 at 16:42
  • I don't have a windows machine handy to test this, but can you save to wmf or svg format? `ggsave(p, filename = "a.wmf", device = "wmf")` or `ggsave(p, filename = "a.svg", device = "svg") ` – JD Long Aug 03 '18 at 19:23
  • 1
    `wmf` output works, but does not support transparency and warns about it. `svg` hangs on my Windows system - and it is not supported on my Powerpoint version; it might work on 360 though. `emf` via package `devEMF` produces output, but only shows the text, lines and points are empty. `eps` is supported on older versions of Office, but not longer because of security constraints. Out of luck, Microsoft is a vector-graphics free world. – Dieter Menne Aug 04 '18 at 09:16
  • Bummer. Seems like the functionality of copying and pasting as a metafile into powerpoint is just a "lucky" side benefit. It's be great for microsoft/Rstudio to integrate the two better (though I'd really love for my clients to just uninstall powerpoint!). – Ben G Aug 06 '18 at 12:08
  • 1
    I remember that emf was introduced with big noise telling us that it was the real thing after the buggy wmf. But 20 years later, it still is semi-orphaned. – Dieter Menne Aug 06 '18 at 13:00
1

If you would like to export R graphs to Powerpoint you can also use the wrapper package export built on top of officer that just came out on CRAN, see https://cran.r-project.org/web/packages/export/index.html and for demo https://github.com/tomwenseleers/export

Typical syntax is very easy, and output is in native Powerpoint vector format and fully supports semi-transparency:

install.packages("export")
library(export)
library(ggplot2)
qplot(Sepal.Length, Petal.Length, data = iris, color = Species, 
      size = Petal.Width, alpha = I(0.7))     
graph2ppt(file="ggplot2_plot.pptx", width=6, height=5) 

You can also use it to export to Word, Excel, Latex or HTML and you can also use it to export statistical output of various R stats objects.

Tom Wenseleers
  • 7,535
  • 7
  • 63
  • 103