ggplot2 png in fullscreen Powerpoint on secondary screen - r

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.

Related

How to enhance resolution of ggairs plot when ggsave not working

I'm trying to save a rather large ggpairs file. For some reason I can't get ggsave to work on my device (the file is created but it's blank no matter the plot, file type etc.) so I have been manually right clicking on save image as on the output.
However the resolution is so poor you can't read the correlation information as I've had to reduce the font size.
Appreciate any suggestions on how to get out a readable output.
TIPI_data<- vms_data[, c("cond","group_aff","personal_exp","outcomes","mechanics", "TIPI_O", "TIPI_C", "TIPI_E","TIPI_A","TIPI_N")]
ggpairs(TIPI_data, ggplot2::aes(colour=cond), upper = list(continuous = wrap("cor", size = 1.5)))
I don't know if this works for you, it worked for me. I have used this code to save as png with high resolution:
corrPlot <- ggpairs(df, diag=list(continuous="density"), axisLabels='show')
png(filename, height=1000, width=1000)
print(corrPlot)
dev.off()
I used this reference: https://github.com/tidyverse/ggplot2/issues/650#issuecomment-65086626

What is a simple way to thumbnail some plots in R Markdown/knitr?

I am using R Markdown with knitr in R Studio to create and update a simple project website to keep my colleagues up to speed with a data analysis model I am building. There are some plots on the page, which (for smaller plots) so far has worked nicely, they can see the code and the results in the same place.
However, some plots have grown very large (and must remain large to allow quick side-by-side comparison of models), and don't fit on the page very well. I've used separately uploaded pdfs (with a link on the page) for some of them. It would be nicer if there was a simple way of generating thumbnails of some of these plots, so that the user can view a small plot image, click on it and then inspect the much larger image in detail. However, if it takes a lot of manual scripting for each plot instance, I'd rather not waste time on it and just upload the couple of pdfs. A similar question here talks about package, knitrbootsrap, but I don't want to thumbnail all my plots, just a select few. The package seems to use Magnific popup, but integrating it myself in a Markdown page seems like a hassle(?). I didn't find anything in the R Markdown reference guide. Of course a one way would be to generate two plots, one tiny, which is shown, and link it to another, larger plot image/pdf that is uploaded separately - but a simpler, more automatic way would be desireable.
Hence the question - is there a simpler way to generate clickable plot thumbnails in R Markdown?
So, this is what I came up with. Add a plot hook, so that you generate a full-resolution pdf image before generating a small image in the chunk:
allow_thumbnails <- function(x, options) {
if (!is.null(options$thumb)) {
filename <- sprintf("%s.full.pdf", strsplit(basename(x), "\\.")[[1]][1])
absolute_path <- file.path(dirname(x), filename)
# generate the full resolution pdf
pdf(absolute_path, width = options$thumb$width, height = options$thumb$height)
eval(parse(text = options$code))
dev.off()
# add an html link to the low resolution png
options$fig.link = absolute_path
}
knitr:::hook_plot_md_base(x, options)
}
And then in the Rmd file, I define the size of the full-resolution image using the thumb argument:
```{r init}
knit_hooks$set(plot = allow_thumbnails)
```
```{r my_large_plot, fig.width = 15, fig.height = 15, thumb = list(width = 45, height = 45)}
my_large_plot()
```
This generates an html file with a clickable png that takes you to the pdf.

Get the plot I am looking at as a 300px TIFF for publication

I'm trying to solve a common problem for me. Once I have a plot (usually made using ggplot) then I need the output in a high res (300 or sometimes 600 dpi) TIFF image (in OSX).
I figure I could make a simple function for this, but it seems not to work the way I expected.
dev.copy2tiff <- function(filename,res=300,compression="lzw",...){
sizes <- dev.size(units="px")
cop <-dev.copy(tiff,filename=filename,width=sizes[[1]],height=sizes[[2]],units="px",res=res,compression=compression,pointsize = 3,...)
dev.flush()
dev.off()
return(cop)
}
Output:
Screenshot of what the plot should look like:
Why are the output in the TIFF not like what is on the screen, and what can I do about it?
EDIT:
Just realized: the size of the device should of course be specified as width and height, not pixels. This revised code:
dev.copy2tiff <- function(filename,res=300,compression="lzw",...){
sizes <- dev.size(units="cm")
cop <- dev.copy(tiff,filename=filename,width=(sizes[[1]]),height=(sizes[[2]]),units="cm",res=res,compression=compression,...)
dev.flush()
dev.off()
return(cop)
}
Produces a nicely looking TIFF image that looks like the one presented on the screen (in RStudio), but in 72 DPI (according to OS X Preview).
How come it's not 300 dpi?
The solution to this seems to be to go through the EPS file format.
dev.copy2eps(file="filename.eps")
is now my friend. Once opened in Preview (OSX) it can be exported to TIFF (or submitted as is).

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')

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

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.

Resources