1

I would like to mask the ocean in a map of a section of Australia.

Here is my starting point:

library(maps)
library(mapdata)
image(x=110:155, y =-40:-10, z = outer(1:45, 1:30, "+"), 
  xlab = "lon", ylab = "lat")

Then, following the solution posted here (How can I color the ocean blue in a map of the US?) I set up the polypath:

outline <- map("worldHires", plot=FALSE) # returns a list of x/y coords
xrange <- range(outline$x, na.rm=TRUE) # get bounding box
yrange <- range(outline$y, na.rm=TRUE)
xbox <- xrange + c(-2, 2)
ybox <- yrange + c(-2, 2)
# create the grid path in the current device
polypath(c(outline$x, NA, c(xbox, rev(xbox))),
 c(outline$y, NA, rep(ybox, each=2)),
 col="light blue", rule="evenodd")][1]

However the resultant plot is masked either side of the country border. Can anyone help me only mask outside the country borders?

Community
  • 1
  • 1
  • is `map("worldHires", regions="australia", bg="light blue", fill=TRUE, xlim=c(110, 155), ylim=c(-40, -10))` sufficient ? – Xachriel Jul 16 '13 at 15:43

1 Answers1

1

Thanks to the R-help mailing list for this solution (https://stat.ethz.ch/pipermail/r-help/2013-July/356943.html):

library(maps)
library(mapdata)

image(x=110:155, y =-40:-10, z = outer(1:45, 1:30, "+"),
  xlab = "lon", ylab = "lat")

Explicitly state the name of the region ("Australia"):

outline <- map("worldHires", regions="Australia", exact=TRUE, plot=FALSE) # returns a list of x/y coords
xrange <- range(outline$x, na.rm=TRUE) # get bounding box
yrange <- range(outline$y, na.rm=TRUE)
xbox <- xrange + c(-2, 2)
ybox <- yrange + c(-2, 2)

Omit NAs from outline (which is what was interfering with the path previously):

subset <- !is.na(outline$x)

Create the polypath using the subset of the outline (ie. without NAs):

# create the grid path in the current device
polypath(c(outline$x[subset], NA, c(xbox, rev(xbox))),
  c(outline$y[subset], NA, rep(ybox, each=2)),
  col="light blue", rule="evenodd")