8

I'm plotting some hemispheric fields with ggplot2 and when I try to project them into a stereographic projection it takes ages to render. This is a minimal example:

lat <- seq(-87.159, -2, by = 3.7)
lon <- seq(0, 360, by = 3.75)
month <- 1:12

gdata <- expand.grid(lat = lat, lon = lon, month = month)
gdata$z <- rnorm(nrow(gdata))

g <- ggplot(gdata, aes(lon, lat)) +
    geom_tile(aes(fill = z)) +
    facet_wrap(~month, ncol = 4) 

benchplot(g)

I get:

       step user.self sys.self elapsed
1 construct     0.000    0.000   0.000
2     build     0.156    0.004   0.162
3    render     0.976    0.016   0.990
4      draw     0.464    0.000   0.464
5     TOTAL     1.596    0.020   1.616

If I add a projection with coord_map()

benchplot(g + coord_map("stereographic", orientation = c(-90,0, -120)))

it takes ~130 times longer.

       step user.self sys.self elapsed
1 construct     0.008    0.000   0.004
2     build    39.520    0.184  40.612
3    render   147.744    0.060 147.968
4      draw    17.884    0.004  17.902
5     TOTAL   205.156    0.248 206.486

Is there a way to speed this up? I feel that a 100+ fold increase in render time is too much.

Sébastien Rochette
  • 6,536
  • 2
  • 22
  • 43
Elio Campitelli
  • 1,408
  • 1
  • 10
  • 20
  • 1
    This question was already asked 3 years ago, but as not received answer: http://stackoverflow.com/questions/17863765/r-ggplot2-combining-geom-tile-and-coord-mapmoll?rq=1 – Sébastien Rochette Apr 09 '17 at 08:47
  • I have experienced the same I think this relates to each tile becoming a polygon for which the projection needs to be calculated. – Bart Sep 28 '21 at 09:58

0 Answers0