[R-sig-Geo] Coloring Canada provinces in raster
Rafael Wüest
rafael.wueest at wsl.ch
Fri Jan 27 09:25:25 CET 2012
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
More information about the R-sig-Geo
mailing list