Drawing polar centered spatial maps using ggplot2

[This article was first published on NumberTheory » R stuff, and kindly contributed to R-bloggers]. (You can report issue about the content on this page here)
Want to share your content on R-bloggers? click here if you have a blog, or here if you don't.

Drawing maps of the polar regions can be done using square spatial maps. A small example says more than a thousand words:

xlim = c(-180,180)
ylim = c(60,90)
 
# Some fake grid data
dat_grid = expand.grid(x = xlim[1]:xlim[2], y = ylim[1]:ylim[2])
dat_grid$value = runif(nrow(dat_grid))
 
# Polygons data
dum = map(xlim = c(-180,180), ylim = c(60,90), plot = FALSE)
ant_ggplot = data.frame(dum[c("x","y")])
ant_ggplot$value = mean(dat_grid$value)
 
# Make the square spatial map
ggplot(aes(x = x, y = y, fill = value), data = dat_grid) + geom_tile() + 
  geom_path(data = ant_ggplot)

The final call to ggplot results in the following picture:

A much more natural way of plotting this polar centered data would be to use polar coordinates. This is supported by ggplot2 through the coord_polar() command. In code (with great help from Charlotte Wickham) this results in:

res <- 1 # 1 degree resolution
x_cell_lim <- c(180, -180) + c(1, -1) * res/2
y_cell_lim <- c(90, 60) + c(1, -1) * res/2
 
ggplot(aes(x = x, y = y, fill = value), data = dat_grid) + geom_tile() + 
  geom_path(data = ant_ggplot) + 
  scale_fill_gradient(low = "white", high = muted("red")) + 
  coord_polar(start = -pi/2, expand = FALSE) +
  scale_y_continuous("") + scale_x_continuous("") + 
  xlim(x_cell_lim) + ylim(y_cell_lim) +
  opts(axis.ticks = theme_blank(), axis.text.y = theme_blank(), 
       axis.title.x = theme_blank(), axis.title.y = theme_blank(),
       panel.border = theme_blank())

Note that I use theme_blank() many times to get rid of certain elements of the plot (rectangle around the plot, y-axis etc). In addition, the calls to x_lim and y_lim are necessary. Charlotte Wickham explains (direct quote from ggplot2 mailing list): The little additions to the limits make sure the entire cell in plotted without any gaps. It might make more sense to have: x_cell_lim <- c(180, -180) + c(1, +1) * res/2, assuming cells centered at 180 and -180 longitude are the same. The above call to ggplot resuls in the following plot:

Note that the grid cells are nicely scaled towards the north pole. In my opinion this is a much more effective plot for showing polar oriented data.

To leave a comment for the author, please follow the link and comment on their blog: NumberTheory » R stuff.

R-bloggers.com offers daily e-mail updates about R news and tutorials about learning R and many other topics. Click here if you're looking to post or find an R/data-science job.
Want to share your content on R-bloggers? click here if you have a blog, or here if you don't.

Never miss an update!
Subscribe to R-bloggers to receive
e-mails with the latest R posts.
(You will not see this message again.)

Click here to close (This popup will not appear again)