# Maps with R, and polygon boundaries

With R, it is extremely easy to draw maps. Let us start with something simple, like French regions. Baptiste mentioned on his blog that shapefiles can be downloaded from http://ign.fr/ website. Hence, if you extract the zip file, it is possible to get claims frequency per region (as done in the course ACT2040),

```> library(maptools)
> library(maps)
> region<-tapply(baseFREQ[,"nbre"],
+ as.factor(baseFREQ[,"region"]),sum)/
+ tapply(baseFREQ[,"exposition"],
+ as.factor(baseFREQ[,"region"]),sum)
> depFREQ=rep(NA,nrow(departements))
> names(depFREQ)=as.character(
+ departements\$CODE_REG)
> for(nom in names(region)){
+ depFREQ[names(depFREQ)==nom] =
+ region[nom]}
> plot(departements,col=gray((depFREQ-.05)*20))
> legend(166963,6561753,legend=seq(1,0,by=-.1)/20+.05,
+ fill=gray(seq(1,0,by=-.1)),cex=1.25, bty="n")``` Another application is on earthquakes. It is possible to use shapefiles of tectonic plates contour, and to relate earthquakes to plates. Shapefiles can be found onhttp://www.colorado.edu/ (here). First, we can extract the shapes of the tectonic plates

```> plates = readShapePoly("plates.shp",
+ proj4string=CRS("+proj=longlat"))
> PP=SpatialPolygons2PolySet(plates)```

Consider Montreal,

`> montreal=c(-73.600,45.500)` Given that specific location, it is possible to use the following code to get the associated plate,

```> PLATE.loc=function(pt){
+ K=NA
+ for(k in 1:17){
+ c=point.in.polygon(pt, pt,
+ PP[PP\$PID==k,c("X")],PP[PP\$PID==k,c("Y")],
+ mode.checked=FALSE)
+ if(c>0){K=k}
+ }
+ return(K)}
> abline(v=montreal,col="red")
> abline(h=montreal,col="red")
> PLATE.loc(montreal)
 1```

and then to plot the associated tectonic plate very easily

```> PLATE=function(k0){
+ library(maps)
+ map("world")
+ polygon(PP[PP\$PID==k0,c("X")],PP[PP\$PID==k0,c("Y")],
+ col="red")
+ for(k in (1:17)[-k0]){polygon(PP[PP\$PID==k,c("X")],
+ PP[PP\$PID==k,c("Y")],col="light blue")}
> PLATE(PLATE.loc(montreal))``` Those code were used in the paper written with Mathieu, and that will be presented on January 30th at the Geotop seminar.

## 3 thoughts on “Maps with R, and polygon boundaries”

1. Aga says:

“http://freakonometrics.free.fr/baseFREQ.csv”,
(based on http://freakonometrics.hypotheses.org/978)
but the data does not include used above columns like baseFREQ\$region … (instead:
 “numeropol” “debut_pol” “fin_pol” “freq_paiement”
 “langue” “type_prof” “alimentation” “type_territoire”
 “utilisation” “presence_alarme” “marque_voiture” “sexe”
 “cout1” “cout2” “cout3” “cout4”
 “nbsin” “exposition” “cout” “age”
 “duree_permis” “annee_vehicule”),
so the example does not look being replicable…

2. Arthur says:

Sorry if I went (too) quickly on that one. The database used can be, e.g., the following,

`> sinistre <- read.table("http://freakonometrics.free.fr/ sinistreACT2040.txt", + header=TRUE,sep=";") > sinistres=sinistre[sinistre\$garantie=="1RC",] > sinistres=sinistres[sinistres\$cout>0,] > contrat <- read.table("http://freakonometrics.free.fr/ contractACT2040.txt", + header=TRUE,sep=";") > T=table(sinistres\$nocontrat) > T1=as.numeric(names(T)) > T2=as.numeric(T) > nombre1 = data.frame(nocontrat=T1,nbre=T2) > I = contrat\$nocontrat%in%T1 > T1= contrat\$nocontrat[I==FALSE] > nombre2 = data.frame(nocontrat=T1,nbre=0) > nombre=rbind(nombre1,nombre2) > baseFREQ = merge(contrat,nombre)`

(actually, it is an extraction of the one here to get that graph, and that is used in a forthcoming book on R for actuarial science… but I’ll post more on that soon).

3. Josesito says:

in the first example, the baseFREQ object is not defined in that code. It would be very helpful for those (me included) who’re not very well versed on R mapping. Thannk you in advance!

This site uses Akismet to reduce spam. Learn how your comment data is processed.