Sam Dickson
Sam Dickson

Reputation: 5239

How can I make white text in vector graphics in R?

I'm trying to make vector graphics that can be inserted into Word. Whenever I try to use white text, though, it shows up as black when I insert it into a Word document. If I output to PDF, it works fine, but unfortunately I can't import PDFs into Word directly. How can I get white text to stay white when I output to EMF and import that EMF image to a Word Document?

library(devEMF)
library(grid)

emf("test.emf",height=3,width=3)
  grid.newpage()
  grid.circle(x=0.5,y=0.5,r=0.1,gp=gpar(col=NA,fill="blue"))
  grid.text(1,x=0.5,y=0.5,gp=gpar(col="white"))
dev.off()

Upvotes: 2

Views: 144

Answers (2)

Philip Johnson
Philip Johnson

Reputation: 186

(note I am the developer of the devEMF package; I only occasionally check stackoverflow, so just seeing this now)

devEMF contained a bug that neglected to set the text color for white text until non-white text had been used first. I have submitted to CRAN devEMF version 3.6-2 which fixes this bug; it will take a few days to percolate through CRAN. Meanwhile, since your use case is Microsoft Word, which handles EMF+ well, you can work around the problem immediately by requesting EMF+ fonts when you open the device:

emf("test.emf",height=3,width=3, emfPlusFont=TRUE)

Upvotes: 3

eipi10
eipi10

Reputation: 93821

You could generate this in a Word document directly by creating it in rmarkdown:

---
output: word_document
---

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

```{r}
grid.circle(x=0.5, y=0.5, r=0.1, gp=gpar(col=NA,fill="blue"))
grid.text(1, x=0.5, y=0.5, gp=gpar(col="white"))
```

The output looked as expected when I tried this on my system.

enter image description here

However, when I generated an EMF file using your code directly and dragged test.emf into a Word file, I got the same result you did:

enter image description here

I did find, however, that if I changed the color to "#FEFEFE", which is virtually white, then the color rendered correctly in the EMF file output (as did any other color specification I tried, except for "white" or "#FFFFFF"). So if you need to save as an EMF, this will hopefully get the job done.

A few months ago, I answered an SO question with a similar issue. In that case, the goal was to write an xlsx file using the xlsx package and have the worksheet title in black text. However, the text was actually rendered as white text in the output file. In that case, changing the color setting to "#010101", which is virtually black, worked, as did any other color specification except pure black ("#000000" or "black"). I don't know if the two issues are related, but I thought I'd mention it in case they are.

Upvotes: 1

Related Questions