[R] ggsave to .png bug in ggplot2 (?)

ONKELINX, Thierry Thierry.ONKELINX at inbo.be
Thu Aug 20 10:14:17 CEST 2009

Dear Chris, 

That problem is due to the fact that ggsave() and png() use different
default for the resolution. ggsave() uses 300 dpi and png() 72 dpi. If
you set the resolution in ggsave() to 72 then you get exactly the same
output as on the screen. But the resolution is way to low for
publication. I have the feeling that everything scales rather nice when
using a different resolution, except for the fonts.

My workaround is to always save my plots to pdf. I use them as pdf (no
problem with that in LaTeX), unless the figure contains lots of points
(e.g. scatterplots). In that case I convert the pdf to png outside R.
The best tools I found for that are Ghostfriend
(http://www.noliturbare.com/index.php) and www.zamzar.com.



ir. Thierry Onkelinx
Instituut voor natuur- en bosonderzoek / Research Institute for Nature
and Forest
Cel biometrie, methodologie en kwaliteitszorg / Section biometrics,
methodology and quality assurance
Gaverstraat 4
9500 Geraardsbergen
tel. + 32 54/436 185
Thierry.Onkelinx at inbo.be

To call in the statistician after the experiment is done may be no more
than asking him to perform a post-mortem examination: he may be able to
say what the experiment died of.
~ Sir Ronald Aylmer Fisher

The plural of anecdote is not data.
~ Roger Brinner

The combination of some data and an aching desire for an answer does not
ensure that a reasonable answer can be extracted from a given body of
~ John Tukey

-----Oorspronkelijk bericht-----
Van: r-help-bounces at r-project.org [mailto:r-help-bounces at r-project.org]
Namens Dieter Menne
Verzonden: donderdag 20 augustus 2009 9:13
Aan: r-help at r-project.org
Onderwerp: Re: [R] ggsave to .png bug in ggplot2 (?)

Chris Friedl wrote:
> Text is really small and legend boxes are huge in this plot when saved

> to .png with ggsave. Plot is correct (i.e. looks the same as the 
> screen) when saved with dev.print. Saving to .pdf with ggsave give the
correct output.

You are not alone. Things like this always happen the day before the
deadline, when you want to make minor changes. It is not limited to
ggplot, but looking at the variant

x <- data.frame(value=rnorm(5000, mean=0), case="A") y <-
data.frame(value=rnorm(5000, mean=3), case="B") xy <- rbind(x, y) p <-
ggplot(xy, aes(x=value, colour=case, group=case)) + geom_density() p
ggsave(p, width=4,height=4,filename = "xy_ggsave.png")

makes me believe that this function should be banned. You might also
check the output of the ggsave documentation page to learn what scaling
means in R. (Slightly modified below). 

ratings <- qplot(rating, data=movies, geom="histogram") qplot(length,
data=movies, geom="histogram")
ggsave(ratings, file="ratingsA.pdf")
ggsave(ratings, file="ratingsB.pdf", width=4, height=4) # make twice as
big as on screen ggsave(ratings, file="ratingsC.pdf", scale=2)

I never use these functions in final production, be it in standard
graphics or lattice, but always open a device and close it after all
plots have been produced.


View this message in context:
Sent from the R help mailing list archive at Nabble.com.

R-help at r-project.org mailing list
PLEASE do read the posting guide
and provide commented, minimal, self-contained, reproducible code.

Dit bericht en eventuele bijlagen geven enkel de visie van de schrijver weer 
en binden het INBO onder geen enkel beding, zolang dit bericht niet bevestigd is
door een geldig ondertekend document. The views expressed in  this message 
and any annex are purely those of the writer and may not be regarded as stating 
an official position of INBO, as long as the message is not confirmed by a duly 
signed document.

More information about the R-help mailing list