3

I have been asked to make a map of New Zealand using R with regional boundaries. I need something exactly like this example of a map for Switzerland: http://blog.revolutionanalytics.com/2009/10/geographic-maps-in-r.html

I tried copying the same commands but after I downloaded the data from gadm and tried plotting it with spplot, I ended up with ten tiny plots with New Zealand on the extreme right hand edge. I can't paste the output into this question because R will not let me save it as a file. But what I did was simply download the "level 1" data from gadm in R format and then spplot(gadm).

Please could someone help me to make a map of New Zealand showing regional boundaries, so that I can make the regions different colours? It looks like the main problems at the moment are

  1. I would like the North and South Islands to be in the middle.
  2. I don't want a super-detailed map as I would like to plot it quickly.
  3. I just want a single map, not ten of them.

Alternatively, any pointers to tutorials would be very welcome. I don't necessarily want to use the gadm data if there is a better alternative.

3

Well, I found a minimal way to answer the question but I would still be interested if people can tell me how to improve it, and I still don't understand exactly what spplot is doing. After doing a load(), I used the following commands:

nz1 <- gadm
nz1$NAME_2 <- as.factor(nz1$NAME_1)
nz1$fake.data <- runif(length(nz1$NAME_1)) 
spplot(nz1,
        "NAME_2", 
         xlim=c(163,180), 
         scales=list(draw=T), 
         ylim=c(-50,-32), 
         col.regions=rgb(nz1$fake.data, 1-nz1$fake.data, 0), 
         colorkey=F)

which produced this plot (which is the sort of thing I'm looking for): enter image description here

  • Your solution looks pretty good to me. You solved your goal to zoom in on the two main islands by setting xlim and ylim. For an example with ggplot you may want to take a look at this answer - you could easily adapt it to your needs. – cengel Dec 16 '13 at 2:22
  • what libraries/data is requiered to reproduce this? – HattrickNZ Jan 9 '15 at 0:48
  • If you go to the link in the question, you need to copy the steps for Switzerland. So you need the sp package and the gadm data, which you can either get from within R or download directly. – Flounderer Jan 9 '15 at 0:58
2

Here is an approach that is closely related to this recent topic. It uses ggplot2 instead of spplot. Note that I reprojected the data to UTM 60S prior to plotting.

## Avoid scientific notation
options(scipen = 12)

## Load required packages
lib <- c("raster", "rgdal", "ggplot2")
sapply(lib, function(x) require(x, character.only = TRUE))

## Download and reproject data from gadm.org to UTM 60S
nz1 <- getData("GADM", country = "NZ", level = 1)
nz1 <- spTransform(nz1, CRS("+init=epsg:2135"))

## Extract polygon corners and merge with shapefile data
nz1@data$id <- rownames(nz1@data)
nz1.ff <- fortify(nz1)
nz1.df <- merge(nz1@data, nz1.ff, by = "id", all.y = TRUE)

## Plot map
ggplot() + 
  geom_polygon(data = nz1.df, aes(x = long, y = lat, group = group, 
                                  fill = NAME_1), 
               color = "black", show_guide = FALSE) +
  labs(x = "x", y = "y") + 
  theme_bw()

Administrative map of New Zealand

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.