Export image from R to word with alpha channel (transparency) - r

I am wanting to export an R produced figure to Word. The figure contains transparency (alpha channel). Below is some example code - when exported to Windows metafile it throws an error:
Warning message:
In plot.xy(xy, type, ...) :
semi-transparency is not supported on this device: reported only once per page
Exporting to SVG produces the desired result, but this image format is not supported by MS Office. Is there a way around this? What image type could I use while retaining the alpha channel? PNG is possible, but this doesn't produce very crisp graphics - it loses the clear vectorized image.
# Get some colours with transparency (alpha = 0.6)
col.dot <- rainbow(5, alpha = .6)
# Save to svg file - OK
svg("test_fig.svg")
plot(1:5,col = col.dot, pch=15)
dev.off()
# Save to wmf - warning "semi-transparency is not supported on this device..."
win.metafile("test_fig.wmf")
plot(1:5,col = col.dot, pch=15)
dev.off()
I should add, this is on a Windows system (Windows 8 64 bit, with Word 2013)

I just made a new package export to easily export R graphs to Office (Word, Powerpoint), see
https://cran.r-project.org/web/packages/export/index.html and
for demo https://github.com/tomwenseleers/export.
Typical syntax is very easy, e.g.:
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)
Output is vector format and so fully editable after you ungroup your graph in Powerpoint. 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.
This results in a fully editable, high quality Powerpoint graph in native Office vector-based DrawingML format, which you can also readily copy & paste as enhanced metafile if you like, and which unlike the EMFs exported from R also fully supports transparency.

From the help of win.metafile:
There is support for semi-transparent colours of lines, fills and text
on the screen devices. These work for saving (from the ‘File’ menu) to
PDF, PNG, BMP, JPEG and TIFF, but will be ignored if saving to
Metafile and PostScript.
So you cannot use transparency in a metafile. You can try saving as png and increasing the resolution of the output.

Related

How to increase resolution of ggplots when exporting using officer R

I want to export charts to a PPT and am using the officer package to achieve the same. However, the default resolution of the charts is low and I would like to change that. I am currently using the following call
ph_with_gg(p1,type = "chart",res = 1200)
where p1 is a ggplot object. On running this, I get the following error:
Error in png(filename = file, width = width, height = height, units =
"in", :
formal argument "res" matched by multiple actual arguments
Would really appreciate the help around this
Rather than using png, for high-resolution plots in PPT you should be using vector graphics.
See under extensions:
Vector graphics with package rvg
The package rvg brings an API to
produce nice vector graphics that can be embedded in PowerPoint
documents or Excel workbooks with officer.
This package provides functions dml() and ph_with() corresponding method to export ggplots to .pptx as vector graphics.
Example:
library(ggplot2)
library(officer)
library(rvg)
library(magrittr)
data(iris)
read_pptx() %>%
add_slide(layout='Title and Content',master='Office Theme') %>%
ph_with('Iris Sepal Dimensions', location = ph_location_type(type="title")) %>%
ph_with(dml( ggobj=
ggplot(iris, aes(x=Sepal.Length,y=Sepal.Width,col=Species)) +
geom_point()), location = ph_location_type(type="body")) %>%
print('iris_presentation.pptx')
As an additional benefit, you will be able to edit the charts in PowerPoint. For example, if you decided to capitalize the names of the 3 species, you could just edit the chart instead of editing the data and regenerating the slides. (You can also make the plots non-editable, but editable is the default.)
Is it important that the plot is saved to a presentation in the code?
Otherwise using:
ggsave(filename = file, p1, width = width, height = height, dpi = dpi)
will give you a png of any resolution you need..
(provided that filename ends with .png and you set width, height and dpi to appropriate values)

ggplot2 png in fullscreen Powerpoint on secondary screen

I create a plot with ggplot and save it as an png in my linux box.
When I insert the plot into a presentation in Windows using MS Powerpoint (2013) it all looks fine until I go full screen with F5: in full screen the axes and the grey background grid disappears (font rendering is also noticeably worse).
In the presenter notes it still looks fine.
The png looks perfectly fine, only once projected in full screen on a secondary screen (monitor/wall) it goes bad. Full screen on primary screen works - this doesn't seem to be a resolution issue.
I tried saving plots in two different ways with the same results:
# 1
ggsave(filename="test.png", p)
# 2
png(file = "test.png", width = 1024, height = 768, units = 'px', dpi=300)
print(p)
dev.off()
Specifieng type (cairo-png) doesn't help either.
Workaround: exporting to pdf and displaying that in fullscreen works completely fine. It is only inconvenient if I want to have my notes along.
You can use the new export package to save your ggplot2 graph to native Powerpoint format- that should work OK, see
https://cran.r-project.org/web/packages/export/index.html and for demo
https://github.com/tomwenseleers/export
Typical syntax is very easy, e.g.:
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.
You can bring all of these together with
ggsave(filename="test.png",width=1024,height=768,units='px',dpi=300)
You can adjust the dpi to 600, but usually 300 will suffice.
I've also had success using inches instead of pixels for ppt instead of pixels.

Can transparency be used with PostScript/EPS?

I am trying to save an R plot as an EPS file but I have a problem with the following component of the plot - the gray transparent polygon (transparent black = gray effect):
polygon(x.polygon, y.polygon.6, col="#00000022", border=NA)
This line of code works fine when saving the plot as PDF but not as EPS. Looks like EPS does not support transparency? What other choice would I have?
Here is the code for the full plot:
postscript(file="Figure.eps", width=5.5, height=5.5, onefile=F, horizontal=F)
ts(t(data.frame(initial_timepoint, second_timepoint, third_timepoint, final_timepoint)))->obj
obj[,-c(3,7)]->obj1
plot(obj1, plot.type="single", lwd=0.6, xaxs="i",yaxs="i",xlab="",ylab="LV ejection fraction (%)",xaxt='n',yaxt='n',ylim=c(0,70),col="black")
axis(1, at=c(1,2,3,4), labels=c("1","2","3","4"),cex.axis=1)
axis(2, at=seq(0,70,10), labels=c("0%","10%","20%","30%","40%","50%","60%","70%"),cex.axis=1, las=1)
abline(v=c(2,3),lwd=0.6,lty=2)
stderr <- function(x) sqrt(var(x,na.rm=TRUE)/length(na.omit(x)))
avg<-c(mean(initial_timepoint,na.rm=T), mean(second_timepoint,na.rm=T), mean(third_timepoint,na.rm=T), mean(final_timepoint,na.rm=T))
err<-c(stderr(initial_timepoint), stderr(second_timepoint), stderr(third_timepoint), stderr(final_timepoint))
my.count <- c(1,2,3,4)
my.count.rev <- c(4,3,2,1)
y.polygon.6 <- c((avg+err*1.96)[my.count],(avg-err*1.96)[my.count.rev])
x.polygon <- c(my.count, my.count.rev)
polygon(x.polygon, y.polygon.6, col="#00000022", border=NA)
lines(avg,col="black",lwd=0.8,lty=3)
lines((avg+err*1.96),lwd=0.8,lty=3)
lines((avg-err*1.96),lwd=0.8,lty=3)
dev.off()
Although the EPS format does not natively support semi-transparency, it is still possible to use cairo_ps(), that one automatically rasterizes semi-transparent areas, and the resolution at which it does this can be controlled with the argument fallback_resolution :
cairo_ps(file = "test.eps", onefile = FALSE, fallback_resolution = 600)
qplot(Sepal.Length, Petal.Length, data = iris, color = Species, size = Petal.Width, alpha = I(0.7))
dev.off()
All the non-semi-transparent areas then nicely stay as vector graphics.
Or even shorter you can also use :
ggsave("filename.eps", device=cairo_ps, fallback_resolution = 600)
Or use the functions to export to eps using the new export package, which just came out on CRAN :
install.packages("export")
library(export)
graph2eps("filename.eps", fallback_resolution = 600)
That package also supports a number of other export formats, including Powerpoint (graph2ppt), see ?graph2vector, which also retains semi-transparency...
The PostScript graphics model itself does not support general transparency of page elements at all. (Hence it is also not possible for EPS.) PostScript colors are all fully opaque.
An object drawn on top of another object would overwrite and cover all lower objects with its own color leaving no room for transparent effects. (If you see something that looks like transparency overlays in a PostScript viewer or printout, then that was only emulated transparency, by flattening the two (or more) respective objects into one single rasterized area creating the illusion of transparency.)
The PDF graphics model is based on PostScript's, but it extends it in various aspects, adding several new features. One of these is real transparency for complete objects.
After Adobe added transparency to PDF, it also created an extension [1] to the existing PostScript language that was able to include code in PS programs which would add transparency to PDFs created from this PostScript via Distiller. However, when rendering on screen or printing on paper this same original PostScript including this same code, that additional transparency would not appear, and the top (transparent in PDF) object would still overwrite the bottom ones when directly used in PostScript.
What other choice would I have?
Various:
Use PDF only. Don't use EPS.
If you must use EPS, use a two-step process:
Create the PDF first.
Then convert from the (transparency-enabled) PDF to EPS, 'flattening' the transparent elements into rasterized areas which emulate the desired transparency effect.
[1] The name of this extension is called pdfmark. With the help of the pdfmark operator one can also add other features to PostScript code which only materialize when distilling this PostScript to PDF: annotations, interactive form fields and buttons, metadata, hyperlinks, and more. All these elements would not have any effect in the direct PostScript rendering on screen or on paper prints.
Instead of making gray out of transparent black, I recommend using the gray.colors() function in R to generate the shades of gray you need. Then you get the look you want in your .eps file without a problem.
This is working fine for me to save .eps files
import matplotlib.pyplot as plt
import matplotlib as mpl
mpl.use('PS')
legend = plt.legend(loc="upper left", edgecolor="black")
legend.get_frame().set_alpha(None)
legend.get_frame().set_facecolor((0, 0, 0, 0))
plt.show()
plt.savefig('fig1.eps', format='eps')

Weird lines appear in R graph after copying it "as metafile" to Word and exporting it as PDF

Journal in which I am submitting a research paper requires that article should be submitted in PDF-format. I am using R for statistics and graphs.
I have used basic formulas to print my graphs. barplot() for bar charts, boxplot() for boxplots and draw.triple.venn in VennDiagram package. I am not using special commands or formulas for device or graphical parameters.
I print my graphs in R and then copied them "as metafile" to produce high quality graphs.
When i attach these graphs in R they appear just fine. However, when I print my work as PDF in Word these lines appear to graphs.
Example for the first graph:
venn.plot <- draw.triple.venn(45, 34, 32,
14, 5, 11, 3, c(sprintf("Elevated\nWB Cr and Co"), sprintf("Mixed or\nsolid PT"), sprintf("Moderate to severe\ng.minimus atrophy")),
lwd = 4,
lty = 'solid',
cex = 3.5,
fontface = "bold",
fontfamily = "sans",
cat.cex = 1.8,
cat.fontface = "bold",
cat.default.pos = "outer",
cat.pos = c(-20, 20, 180),
cat.dist = c(0.115, 0.115, 0.095),
cat.fontfamily = "sans",
rotation = 1
);
Example for the second:
boxplot(df$Crmri~df$gmed2,log="y",yaxt="n",ylim=c(0.3,200));
axis(2,at=c(0,1,2,10,20,100),labels=c("0 ppb","1 ppb","2 ppb","10 ppb","20 ppb","100 ppb"),las=2);
I have several barplots in Word file and after saving as PDF these graphs are just fine.
At first I tried to save those graphs as metafile in R and adding them to Word after but the lines still appear. If I open my EMF-files in any other Viewer those lines are not there.
Any ideas? Thanks!
This has been discussed on r-help before. See:
https://stat.ethz.ch/pipermail/r-help/2011-September/289705.html
Seems to be due to the way Word converts documents to PDFs when you use "Save As" and then choose PDF.
If you have Acrobat (not just the reader), printing the document via the Adobe PDF "printer" should get rid of the lines. Otherwise, you may have to consider switching to another graphics format (e.g., png).
There's a new package export that just came out on CRAN that allows you to export graphs to Powerpoint or Word in native Office format. From there you can then save as PDF in Office without problems, without any weird lines appearing, see
https://cran.r-project.org/web/packages/export/index.html and
https://github.com/tomwenseleers/export
E.g.
install.packages("export")
library(export)
boxplot(count ~ spray, data = InsectSprays, las = 2)
graph2doc(file="plot.docx", width=7, height=5)
graph2ppt(file="plot.pptx", width=7, height=5)
Even after saving to PDF within Powerpoint this will give you a perfect quality vector format PDF without any weird lines :
Other advantage is that the Powerpoint version you get is fully editable vector format, enabling you to make any small required changes in the layout (it also fully supports transparency / alpha channels).
I can confirm that this issue still exists in Word for Office 365 and Word for Office 2016. Copying from R-Studio as a Metafile results in lines in the plots when you convert to PDF (using Word's Save as PDF function). Copying from R-Studio as a Bitmap or exporting as an image does not result in lines, but unfortunately the image quality in Word and PDF is not as clear as the Metafile. The response that described printing the document via the Adobe PDF Printer does avoid the lines, but I found that this approach loses the dynamic links in the text of the Word document (such as Table of Contents links). I have Adobe Acrobat Pro XI. So that solution does not work for me. In the end, the best solution I can find seems to be copying as a Bitmap. Unfortunately the image quality is not very good.

R eps export and import into Word 2010

I'm having trouble with exporting eps files from R and importing into Word 2010.
I'm using ggplot2 plots, eg
library(ggplot2)
p <- qplot(disp,hp,data=mtcars) + stat_smooth()
p
Even after calling setEPS() neither of the following produce files which can be successfully imported.
ggsave("plot.eps")
postscript("plot.eps")
print(p)
dev.off()
The strange thing is that if I produce the plot using File -> Save As -> Postscript from the menu in the GUI, it can be imported correctly. However, when the Word document is subsequently exported as a pdf, the fonts in the graphic are a little jagged.
So my questions are:
What combination of (ggsave/postscript) settings allows me to produce eps files that can be imported into Word 2010?
How can I ensure the fonts remain clear when the Word document is exported as a pdf?
Update
After more investigation I have had more luck with cairo_ps to produce the plots. However, no text shows up when imported into Word.
Furthermore, after checking the various eps outputs (cairo_ps, save from the GUI, ggsave) in a latex document, it seems like the eps import filter in Word quite poor as the printed/pdf output doesn't match the quality of the latex'd document. The ggsave version (which uses postscript) did have some issues with colours that the other two methods didn't have though.
The conclusion is that this is a Word issue and therefore fortune(109) does not apply. I'd be happy to be proven otherwise, but I'll award the answer and the bounty to whoever can provide the commands that can replicate the output from the GUI in command form.
This worked for me... following advice in the postscript help page:
postscript("RPlot.eps", height = 4, width = 4, horizontal = FALSE, onefile = FALSE,
paper = "special")
library(ggplot2)
p <- qplot(disp,hp,data=mtcars) + stat_smooth()
p
#geom_smooth: method="auto" and size of largest group is <1000, so using loess. Use 'method = x' to #change the smoothing method.
#Warning message:
#In grid.Call.graphics(L_polygon, x$x, x$y, index) :
# semi-transparency is not supported on this device: reported only once per page
dev.off()
#quartz
# 2
The funny stuff at the end puts you on notice that this is only a Mac-tested solution, so far anyway.
Edit: I just tested it with R version 2.15.1 (2012-06-22) -- "Roasted Marshmallows": Platform: i386-pc-mingw32/i386 (32-bit) and MS Word 2007 in Win XP and it worked. Commands were Insert/Picture.../select eps format/select file.
Edit2: There is another method for saving besides directly using the postscript device. The savePlot method with an "eps" mode is available in Windows (but not in the Mac). I agree that the fonts are not as smooth as they appear on a Mac but I can discern no difference in quality between saving with savePlot and using save as from an interactive window.
savePlot(filename = "Rplot2", type = "eps", device = dev.cur(), restoreConsole = TRUE)
savePlot calls (.External(CsavePlot, device, filename, type, restoreConsole))
I solved the problem with exporting .eps files from R and importing into Word 2010 on Windows 7 using the colormodel="rgb" option (defaults to "srgb") of the postscript command.
postscript("RPlot.eps", height = 4, width = 4, horizontal = FALSE,
paper = "special", colormodel = "rgb")
library(ggplot2)
p <- qplot(disp,hp,data=mtcars) + stat_smooth(se=FALSE, method="loess")
p
dev.off()
You are probably better of using wmf as a format which you can create on Windows.
Word indeed doesn't support EPS very well.
A better solution is to export your graphs to Word or Powerpoint directly in native Office format. I just made a new package, export, that does exactly that, see
https://cran.r-project.org/web/packages/export/index.html and
for demo
https://github.com/tomwenseleers/export
Typical syntax is very easy, e.g.:
install.packages("export")
library(export)
library(ggplot2)
qplot(Sepal.Length, Petal.Length, data = iris, color = Species,
size = Petal.Width, alpha = I(0.7))
graph2doc(file="ggplot2_plot.docx", width=6, height=5)
graph2ppt(file="ggplot2_plot.pptx", width=6, height=5)
Output is vector format and so fully editable after you ungroup your graph in Word or Powerpoint. You can also use it to export statistical output of various R stats objects.
You can use R studio to knit html files with all of your plots and then open HTML files with Word.
knitr tutorial

Resources