Skip to content
Prev 22507 / 29559 Next

Pole centric map with Raster?

Hi,
On Mar 27, 2015, at 5:55 PM, Edzer Pebesma <edzer.pebesma at uni-muenster.de> wrote:

            
Yes, in the ggplot2 example the volcano matrix is transformed into a data frame, 'mat', of [lon,lat,z]. I followed this nice post:

http://www.numbertheory.nl/2011/11/08/drawing-polar-centered-spatial-maps-using-ggplot2/

I think the following replicates what I did before (less the coastline). I don't know how to control the boundaries in ggplot2.  I'll try what you suggest with SpatialPolygonsDataFrame - I would prefer to use just one graphics paradigm, such as lattice/spplot, if possible.

Thanks!
Ben

### START
library(ggplot2)

# convert a matrix to data frame
as.tile <- function(x = volcano, 
   bb = c(ll.lat = 45, ll.lon = -90, ur.lat = 75, ur.lon = -40) ){
   if (!is.matrix(x)) stop("input must be a matrix")
   d <- dim(x)
   xx <- rep(seq(from = bb[['ll.lon']], to = bb[['ur.lon']], length = d[2]), each = d[1])
   yy <- rep(seq(from = bb[['ll.lat']], to = bb[['ur.lat']], length = d[1]), d[2])
   data.frame(lon = xx, lat = yy, z = as.vector(x))
}

# custom theme without axes and annotations
theme_polar <- function(){
   list(
      theme_bw(base_size=18),
      theme(
         axis.text = element_blank(),
         axis.ticks = element_blank(),
         legend.position = "none"),
      labs(x='',y=''))
}

ylim <- c(30,90)
xlim <- c(-180,180)
projection <- "azequidist" 
orientation <- c(90, -30, 0)
mat <- as.tile()

ggplot() + 
	geom_tile(data = mat, aes(x = lon, y = lat, fill = z)) +
	coord_map(projection = projection, orientation = orientation, ylim = ylim, xlim = xlim) + 
	scale_x_continuous(breaks=(-6:6) * 30, expand = c(0,0)) +
	scale_y_continuous(breaks = seq(from = ylim[1], to = ylim[2], by = 15), limits = ylim, expand = c(0,0)) +
	theme_polar()

#### END
Ben Tupper
Bigelow Laboratory for Ocean Sciences
60 Bigelow Drive, P.O. Box 380
East Boothbay, Maine 04544
http://www.bigelow.org