[R] Coloring Canada provinces (package maps?)

Barry Rowlingson b.rowlingson at lancaster.ac.uk
Thu Jan 26 18:00:08 CET 2012


On Thu, Jan 26, 2012 at 4:03 PM, Dimitri Liakhovitski
<dimitri.liakhovitski at gmail.com> wrote:
> Barry, thanks a lot!
> I was able to read in Candian data set from gadm:
>
> library(raster)
> # Finding ISO3 code for Canada
> getData('ISO3')  # Canada's code is "CAN"
> # Reading in data at different levels
> can0<-getData('GADM', country="CAN", level=0)
> can1<-getData('GADM', country="CAN", level=1)
> can2<-getData('GADM', country="CAN", level=2)
> class(can0)
> str(can0)
> class(can1)
> str(can1)
>
> Apologies for a novice question (I've never worked with maps and
> raster before): what is the way in raster to see what are the
> geographic units within each data set (can0, can1, can2)?
> And what function allows to plot them and color them?

 These are now SpatialPolygonDataFrame objects - like data frames, but
each row has an associated polygonal geometry. These actually come
from the sp package which the raster package has loaded for you.

 class(can0) will tell you this.

 names(can1) will tell you the names of the attributes of the polygons
which you can use like columns of a data frame.

 plot(can1) will plot it

 spplot(can1, "columnname") will do a coloured plot.

 For more info, check the help for sp or read the R-Spatial Task View
on CRAN which has everything you need to know about maps and such in
R.

 Ask any more questions like this on the R-sig-geo mailing list where
the mapping R people hang out...

Barry



More information about the R-help mailing list