[R] need for speed on grid.rect

Mikkel Grum mi2kelgrum at yahoo.com
Wed Oct 20 22:11:26 CEST 2010


Hi Paul

Thanks ever so much for this. This should help tremendously. My only reason for using semi-transparent colours was not knowing how to use colorRampPalette. I'm really just looking at white-to-somecolour gradients. My examples weren't meant to be written one on top of the other.

Unlike in the examples, my real-life grids are irregular, with uneven distances between rows, so grid.raster probably isn't an option.

Also, in real-life I'm writing the plots to png and pdf files. It was on the png files that I noticed the problem, because these files involve much more data than the pdfs. I'm not sure to what extent the Windows graphics drivers affects the png and pdf drivers?

Mikkel

--- On Wed, 10/20/10, Paul Murrell <p.murrell at auckland.ac.nz> wrote:

> From: Paul Murrell <p.murrell at auckland.ac.nz>
> Subject: Re: [R] need for speed on grid.rect
> To: "Mikkel Grum" <mi2kelgrum at yahoo.com>
> Cc: "r-help at r-project.org" <r-help at r-project.org>
> Date: Wednesday, October 20, 2010, 2:38 PM
> Hi
> 
> I think the problem is that the Windows graphics device is
> not great at 
> semitransparent colours.  For example, your code runs
> acceptably fast on 
> my Linux system.
> 
> If your goal is just a colour gradient, then you could
> avoid using 
> semitransparency by calculating the colours yourself, for
> example ...
> 
> library(colorspace)
> finalColour <- rgb(coords(mixcolor(.5,
>                
>                
>     RGB(1, 0, 0),
>                
>                
>     RGB(t(col2rgb("lightblue"))/255))))
> colourPalette <- colorRampPalette(c("lightblue",
> finalColour))
> 
> ... then just draw one set of rectangles ...
> 
> system.time(grid.rect(x = unit(rep(1:100, 200) ,
> "native"),
>      y = unit(rep(1:200, each = 100),
> "native"),
>      width = unit(1, "native"),
>      height = unit(1, "native"),
>      gp = gpar(col = NA,
>         fill =
> colourPalette(20000))
> ))
> 
> ... or even better, draw the gradient as a raster image
> ...
> 
> system.time(grid.raster(matrix(colourPalette(20000),
>                
>                
> ncol=100, nrow=200, byrow=TRUE)))
> 
> ... which is much faster again.
> 
> Paul
> 
> On 21/10/2010 6:10 a.m., Mikkel Grum wrote:
> > When I use grid.rect to print a multi-coloured grid,
> it is
> > incredibly
> slow compared to a single colour grid, or even a two colour
> grid.
> >
> > I've set out some simplified examples below. This is
> something I run
> literally thousands of times a day, so I would greatly
> appreciate any
> hints on how I might improve the speed???
> >
> > library(grid)
> > pushViewport(viewport(
> >      width = unit(100, "native"),
> >      height = unit(100, "native"),
> >      xscale = c(0, 100),
> >      yscale = c(0, 100),
> >      ))
> > pushViewport(viewport(
> >      x = unit(10, "native"),
> >      y = unit(10, "native"),
> >      width = unit(100, "native"),
> >      height = unit(200, "native"),
> >      xscale = c(0, 100),
> >      yscale = c(0, 200),
> >      just = c("left", "bottom"),
> >      angle = 10))
> > system.time(grid.rect(x = unit(rep(1:100, 200) ,
> "native"),
> >      y = unit(rep(1:200, each = 100),
> "native"),
> >      width = unit(1, "native"),
> >      height = unit(1, "native"),
> >      gp = gpar(col = NA,
> >         fill =
> "lightblue")
> > ))
> > system.time(grid.rect(x = unit(rep(1:100, 200) ,
> "native"),
> >      y = unit(rep(1:200, each = 100),
> "native"),
> >      width = unit(1, "native"),
> >      height = unit(1, "native"),
> >      gp = gpar(col = NA,
> >         fill = rgb(1, 0,
> 0, 1:20000/40000))
> > ))
> >
> > My times for the two plots are:
> >     user  system elapsed
> >     0.61    0.30 
>   0.92
> > vs
> >    user  system elapsed
> >     0.08   
> 0.36   24.39
> >
> > The time it takes to calculate the colours clearly
> doesn't explain
> > the
> difference:
> > system.time(rgb(1, 0, 0, 1:20000/20000))
> >     user  system elapsed
> >        0   
>    0       0
> >
> > Even if I do a two coloured grid, it finishes as
> quickly as the
> > single
> coloured one, so my first interpretation is that the
> function finds the
> colours fairly quickly, e.g.
> >
> > system.time(grid.rect(x = unit(rep(1:100, 200) ,
> "native"),
> >      y = unit(rep(1:200, each = 100),
> "native"),
> >      width = unit(1, "native"),
> >      height = unit(1, "native"),
> >      gp = gpar(col = NA,
> >         fill =
> c("lightblue", "red"))
> > ))
> >
> >     user  system elapsed
> >     0.51    0.18 
>   0.70
> >
> > Best regards,
> > Mikkel
> >> sessionInfo()
> > R version 2.12.0 (2010-10-15)
> > Platform: i386-pc-mingw32/i386 (32-bit)
> >
> > locale:
> > [1] LC_COLLATE=English_Ireland.1252 
> LC_CTYPE=English_Ireland.1252
> > [3] LC_MONETARY=English_Ireland.1252 LC_NUMERIC=C
> > [5] LC_TIME=English_Ireland.1252
> >
> > attached base packages:
> > [1] grid      stats 
>    graphics  grDevices utils 
>    datasets  methods
> > [8] base
> >
> > loaded via a namespace (and not attached):
> > [1] tools_2.12.0
> >
> > ______________________________________________
> > R-help at r-project.org
> mailing list
> > https://stat.ethz.ch/mailman/listinfo/r-help
> > PLEASE do read the posting guide http://www.R-project.org/posting-guide.html
> > and provide commented, minimal, self-contained,
> reproducible code.
> 
> -- 
> Dr Paul Murrell
> Department of Statistics
> The University of Auckland
> Private Bag 92019
> Auckland
> New Zealand
> 64 9 3737599 x85392
> paul at stat.auckland.ac.nz
> http://www.stat.auckland.ac.nz/~paul/
> 






More information about the R-help mailing list