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

Dimitri Liakhovitski dimitri.liakhovitski at gmail.com
Fri Jan 27 18:00:26 CET 2012


I found an important part of the solution - was able to get rid of the legend!

library(raster)
can2<-getData('GADM', country="CAN", level=2)
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":
can2 at data[["groups"]]<-groups
can2 at data$groups<-as.factor(can2 at data$groups)
classcolors <- rainbow(6)
spplot(can2,zcol="groups",col.regions=classcolors,colorkey =
FALSE,par.settings =
    list(axis.line = list(col =  'transparent')))

One last challenge:
Is it possible to delete the black borders between small geographic
units and only leave borders between provinces?
(I need to leave only these borders:
can1<-getData('GADM', country="CAN", level=1
plot(can1)  )
Or maybe one could change the color of the black borders between small
geographic units?

Thanks a lot!
Dimitri

On Fri, Jan 27, 2012 at 11:34 AM, Dimitri Liakhovitski
<dimitri.liakhovitski at gmail.com> wrote:
> Vielen-vielen Dank, Rafael.
> A factor!
> That does exactly what I wanted! Thanks a million!
>
> Still can't figure out how to get rid of the legend on the right. I
> couldn't find the solution here:
> http://127.0.0.1:14681/library/sp/html/spplot.html
> Or, if it's impossible, maybe one could at least change the order in
> the legend (to have 1 at the top and 6 at the bottom)?
> Finally- anyone knows how to delete the borders between small
> geographic units and only leave borders between provinces?
>
> Thank you very much, everyone for your help!
> Dimitri
>
> The working code so far:
>
> library(raster)
> can2<-getData('GADM', country="CAN", level=2)
> 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":
> can2 at data[["groups"]]<-groups
> can2 at data$groups<-as.factor(can2 at data$groups)
> classcolors <- rainbow(6)
> spplot(can2,zcol="groups",col.regions=classcolors)
>
>
>
> On Fri, Jan 27, 2012 at 3:25 AM, Rafael Wüest <rafael.wueest at wsl.ch> wrote:
>> Hi Dimitri
>>
>> you can use the col.regions argument in case groups is a factor:
>>
>> can2 at data$groups<-as.factor(can2 at data$groups)
>> spplot(can2,zcol="groups",col.regions=c('red','blue','yellow','lightblue','green','orange'))
>>
>> Hope this does what you were looking for...
>>
>> Rafi
>>
>> On 26.01.2012, at 21:42, Dimitri Liakhovitski wrote:
>>
>>> 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
>>>
>>> _______________________________________________
>>> R-sig-Geo mailing list
>>> R-sig-Geo at r-project.org
>>> https://stat.ethz.ch/mailman/listinfo/r-sig-geo
>>
>>
>>
>> --
>> Rafael Wüest
>> Swiss Federal Research Institute WSL
>> Zürcherstrasse 111
>> 8903 Birmensdorf
>> Switzerland
>>
>> +41 44 7392126
>> rafael.wueest at wsl.ch
>> http://www.wsl.ch/info/mitarbeitende/wueest
>>
>>
>>
>
>
>
> --
> Dimitri Liakhovitski
> marketfusionanalytics.com



-- 
Dimitri Liakhovitski
marketfusionanalytics.com



More information about the R-sig-Geo mailing list