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

Dimitri Liakhovitski dimitri.liakhovitski at gmail.com
Thu Jan 26 21:42:22 CET 2012


Dear map-gurus,
I've made some progress based on one post I found, but now I am stuck.
I was able to group Canadian admin units into 6 groups so that each
group has the same color:

# Getting the data:
library(raster)
can2<-getData('GADM', country="CAN", level=2)
class(can2)

# Creating my (artificial) groupings of Canadian admin units:
nrofunits<-length(can2$NAME_2)
groups<-c(rep(1:6,(nrofunits %/%
6)),rep(1,5))[order(c(rep(1:6,(nrofunits %/% 6)),rep(1,5)))]

# adding values (grouping values) to "data" and calling this new
variables "groups"
can2 at data[["groups"]]<-groups

# Plotting:
spplot(can2,"groups")

Questions that I am still not sure how to address:
How can I use MY colors instead of the default colors? In fact, I
don't want heat map, I need 6 separate colors...
Can I remove the legend from the plot?
Can I eliminate borders between counties but have borders between
Canadian Provinces (like in the US example below)?
I.e., only borders from the plot for can1:
can1<-getData('GADM', country="CAN", level=1)
plot(can1)

Thanks a lot!
Dimitri



On Thu, Jan 26, 2012 at 1:14 PM, Dimitri Liakhovitski
<dimitri.liakhovitski at gmail.com> wrote:
> 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



-- 
Dimitri Liakhovitski
marketfusionanalytics.com



More information about the R-sig-Geo mailing list