[R] PDF too large, PNG bad quality

Greg Snow Greg.Snow at imail.org
Mon Oct 26 18:12:55 CET 2009

Hexbin used to use base graphics, but has switched to grid.  So you can either learn how to augment the grid graph, use lattice and learn how to augent there.  Or you can get a fairly good base graphics approximation using the my.symbols function in the TeachingDemos package, e.g.:

x <- rnorm(10000)
y <- rnorm(10000,x,2)
(bin <- hexbin(x, y))

my.symbols( hcell2xy(bin), symb=ms.filled.polygon, n=6, add=FALSE, 
	asp=bin at shape, xlim=bin at xbnds, ylim=bin at ybnds, 
	bg=grey( (6:0)/7 )[ cut(bin at count, 7) ], fg='#00000000',
	inches=par('pin')[1]/bin at dimen[1]*1.25 )

Gregory (Greg) L. Snow Ph.D.
Statistical Data Center
Intermountain Healthcare
greg.snow at imail.org

> -----Original Message-----
> From: r-help-bounces at r-project.org [mailto:r-help-bounces at r-
> project.org] On Behalf Of Lasse Kliemann
> Sent: Thursday, October 22, 2009 6:35 PM
> To: r-help at r-project.org
> Subject: Re: [R] PDF too large, PNG bad quality
> * Message by -Greg Snow- from Thu 2009-10-22:
> > If you want to go the pdf route, then you need to find some way
> > to reduce redundant information while still getting the main
> > points of the plot.  With so many point, I would suggest
> > looking at the hexbin package (bioconductor I think) as one
> > approach, it will not be an identical scatterplot, but will
> > convey the information (possibly better) with much smaller
> > graphics file sizes.  There are other tools like sunflower
> > plots or others, but hexbin has worked well for me.
> I took a look at the 'hexbin' package, and it is really
> interesting. You were right that it also helps to better display
> the data. Finally, this forced me to learn using the 'grid'
> package :-) I think I will use a pretty high number of bins, so
> the plot looks similar to the scatter plots I am used to -- with
> the addition of colors giving different densities.
> > If you want to go the png route, the problem usually comes from
> > scaling the plot after producing it.  So, the solution is to
> > create the plot at the exact size and at the exact resolution
> > that you want to use it at in your document so that no scaling
> > needs to be done.  Use the png function, but don't accept the
> > defaults, choose the size and resolution.  If you later decide
> > on a different size of graph, recreate the file, don't let
> > LaTeX rescale the first one.
> This was my strategy so far. For instance, for a figure that is
> to span the whole text block from left to right:
> two_third_a4 <- 8.3 * 2/3
> png("new.png",
>     width=two_third_a4,
>     height=two_third_a4,
>     units="in",
>     res=300)
> plot(...)
> Earlier I wrote that the PNG looks good when displayed
> separately, but looks inferior when embedded in the LaTeX PDF
> document. However, I now believe that the dependence is more on
> the viewer application. It looks good displayed separately with
> 'qiv', but not with 'feh'. The PDF document looks inferior when
> displayed with 'evince' or 'epdfview', but it looks okay when
> displayed with 'xpdf'. I presume now that this phenomenon it not
> directly R-related.
> I thank you and everyone who responded so quickly.
> Lasse

More information about the R-help mailing list