[R-sig-Geo] Coloring Canada provinces in raster

Dimitri Liakhovitski dimitri.liakhovitski at gmail.com
Thu Jan 26 19:14:16 CET 2012


Dear R map gurus,
I am very new to creating maps in R and have never used "raster"
before, so very sorry if it's a simple question.

I am trying to create a map of Canada with small administrative units
(counties and the like) colored based on some criterion I set (e.g.,
all units that belong to Group 1 get one color, to Group 2 get another
color, etc). At the bottom of this e-mail is an example of what I am
trying to achieve - but it's for the US (based on coloring US
counties) and using "maps". I was not able to find small
administrative divisions (like counties) for Canada in "maps", only
cities.
So, I was referred to "raster". I was able to read in the Canada data
at the right administrative level:

library(raster)
can2<-getData('GADM', country="CAN", level=2)
class(can2)

# "NAME_2" in object "can2" contains exactly the level of
administrative detail I need!
can2$NAME_2

# and plot and ssplot plot the map with those counties and colors them:
plot(can2)     # No colors
spplot(can2, "NAME_2")  # colored, but not sure based on what rule +
creates a legend

Question:
Is it at all possible to color the units in can2$NAME_2 based on their
groupings that I create (like in the US example below), and the colors
I choose?
Is it possible to remove the legend from the plot?
Is it possible to have no borders between counties but have borders
between Canadian Provinces (like in the US example below)?

Thank you very much for your help!
Dimitri Liakhovitski


### Example for creating a US map with colored counties using "maps":

# 1. Creating an (ARTIFICIAL) criterion for coloring US counties in
only few colors:
library(maps)
allcounties<-data.frame(county=map('county', plot=FALSE)$names)
allcounties$group<-c(rep(1:6,513),rep(1,4))[order(c(rep(1:6,513),rep(1,4)))]
### My colors:
mycolors <- rainbow(6)
map('county',fill=TRUE,col=mycolors[allcounties$group],resolution=0,lty=0,bg=
"transparent")
map('state', lwd=1, add=TRUE)     # adding the state borders



More information about the R-sig-Geo mailing list