Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

geom_rect + coord_map = very slow

Tags:

r

ggplot2

I am plotting a map of Norway with an area of interest highlighted with a red rectangle using ggplot2. If I omit the geom_rect or coord_map, the map plots very quickly (< 1 seconds). If I use both - which I need to - it is extremely slow to print and render (about five minutes).

I presume this is something to do with the munching - projecting the rectangle onto the new coordinate system. Is there a way to control this?

library(ggplot2)
library(maps)
library(mapdata)


xlim <- c(5, 10)
ylim <- c(60, 62)

norwaymap <- map_data("worldHires", "Norway")
a <- ggplot(norwaymap, aes(x = long, y = lat, group = group)) +
  geom_polygon(colour = NA, fill = "grey60") +
  geom_rect(xmin = xlim[1], xmax = xlim[2], ymin = ylim[1], ymax = ylim[2], 
    colour = "red", fill = NA) +
  coord_map(xlim = c(3, 33), ylim = c(57, 72))
print(a) # super slow

Using the low resolution map makes the map plotting much faster (about 10 seconds).

like image 639
Richard Telford Avatar asked Jun 05 '16 10:06

Richard Telford


2 Answers

No need to resort to mercator approximations:

library(ggplot2)
library(maps)
library(mapdata)

norwaymap <- map_data("worldHires", "Norway")

xlim <- c(5, 10)
ylim <- c(60, 62)

ggplot() +
  geom_map(data=norwaymap, map=norwaymap,
           aes(long, lat, map_id=region),
           color=NA, fill="grey60") +
  geom_rect(data=data.frame(),
            aes(xmin=xlim[1], xmax=xlim[2], ymin=ylim[1], ymax=ylim[2]),
            color="red", fill=NA) +
  coord_map(xlim=c(3, 33), ylim=c(57, 72)) +
  ggthemes::theme_map()

enter image description here

Another option would be to use an Albers equal-area conic projection (a typical one for that region):

ggplot() +
  geom_map(data=norwaymap, map=norwaymap,
           aes(long, lat, map_id=region),
           color=NA, fill="grey60") +
  geom_rect(data=data.frame(),
            aes(xmin=xlim[1], xmax=xlim[2], ymin=ylim[1], ymax=ylim[2]),
            color="red", fill=NA) +
  ggalt::coord_proj("+proj=aea +lat_1=60 +lat_2=70 +lon_0=18.37", 
                    xlim=c(3, 33), ylim=c(57, 72)) +
  ggthemes::theme_map()

enter image description here

That has a "disadvantage" of the rectangle being projected (it is with Mercator, too, there's just no distortion).

Either way, the magic for the rectangle is ensuring you're plotting only one, like Luke said.

like image 56
hrbrmstr Avatar answered Nov 10 '22 21:11

hrbrmstr


Use coord_quickmap and especially annotate instead of geom_rect to speed things up:

ggplot(norwaymap, aes(x = long, y = lat, group = group)) +
  geom_polygon(colour = NA, fill = "grey60") +
  annotate(geom="rect", xmin = xlim[1], xmax = xlim[2], ymin = ylim[1], 
           ymax = ylim[2], colour = "red", fill = NA) + 
  coord_quickmap(xlim = c(3, 33), ylim = c(57, 72))

geom_rect overplots several rectangles on the same spot, annotate just plots one rectangle. You can read about the difference between coord_map and coord_quickmap in the help files: ?coord_quickmap.

like image 5
lukeA Avatar answered Nov 10 '22 20:11

lukeA