Home > R > Display pie charts with varying alpha transparencies upon a Google static map

Display pie charts with varying alpha transparencies upon a Google static map

It is sometimes interesting to display pie charts on a map showing geographic details such as Google static maps. This post will illustrate :

  • how to download and manipulate Google static maps using the R package {RgoogleMaps} by M. Loecher
  • how to overlay floating pie charts upon a map using the R package {plotrix} by J. Lemon
  • how to change the alpha value associated to colors {seqinr} by D. Charif, and J.R. Lobry

As an example we will focus on the town of Niamey where my colleagues Madougou Garba and Gauthier Dobigny (Centre Régional AGRHYMET, Département Formation Recherche, Niamey) are currently studying urban rodents communities. Lets say we want to display pie charts corresponding to sampling locations where the rodent community has been sampled using traps.

First we download the static map from Google server using the function GetMap from the package RgoogleMaps.


path<-"/home/rossi/data/blog/data/"
map1.n<-paste(path,"map1",".png",sep="")
zoom<-12 ; lonc<-2.114668 ; latc<-13.520508 ; center<-c(latc,lonc) ; maptype<-"satellite"
niamey <- GetMap(center=center, zoom=zoom, maptype=maptype,destfile=map1.n)

Note that lonc and latc are the coordinates of a points roughtly located at the center of the town. It indicates where the center of the static map should be located.

Once the data are available through the R object niamey we can display it on sreeen using PlotOnStaticMap

PlotOnStaticMap(niamey,destfile=map1.n)

Here is is what you get :

Google static map of Niamey

Now we come to the pie charts. Lets say we have a sample of the urban rodent community collected in a location which coordinates are latitude=13.51204 and longitude=2.09884. Imagine that 20 individuals of R. rattus and 5 individuals of M. natalensis were recorded at that location.

We can plot a point that shows the spatial location of the locality

PlotOnStaticMap(niamey,lat=13.51204, lon=2.09884, pch=3,cex = 3,col="red",verbose=0,destfile=map1.n)

Niamey

If we want a pie chart showing the proportion of each rodent species we use the function floating.pie(xpos,ypos,x,...) where xpos and ypos are the coordintates of the centre of the pie and x is a vector with rodent species counts.
The trick is that xpos and ypos should correspond to the map tile coordinates system and cannot be given in raw degrees.
The package RgoogleMaps provides the function LatLon2XY.centered allowing the conversion:

newcoord<-LatLon2XY.centered(niamey,lat=13.51204,lon=2.09884,zoom=zoom)

We project the pie chart using these new coordinates:

PlotOnStaticMap(niamey,destfile=map1.n)
floating.pie(xpos=newcoord$newX,ypos=newcoord$newY,x=c(20,5),
radius=20,col=c("red","blue"))

radius controls the size of the pie chart, and it can be adjusted to plot a set of pie chart with size proportional to the sample size for example.

When a large number of pie charts are plotted on the same map they may be superimposed. In that case having more or less transparent colors (i.e. changing the alpha) may be interesting.
This is achieved using the function col2alpha from the package seqinr.

col<-c("red","blue")
for (i in 1:(2)) {col[i]<-col2alpha(color=col[i], alpha = 0.50)}
PlotOnStaticMap(niamey,destfile=map1.n)
floating.pie(xpos=newcoord$newX,ypos=newcoord$newY,x=c(20,5),
radius=20,col=col)

Here is the final result, note that the river Niger can now be seen through the pie chart:

Pretty cool he?

Advertisements
Categories: R Tags: ,
  1. No comments yet.
  1. No trackbacks yet.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: