Thursday, 6 November 2014

How do we migrate?

Recently I came across world migration data publicly available on United Nations site. Here you can find this data- http://esa.un.org/wpp/Excel-Data/migration.htm.  It shows the net number of migrants, that is, the number of immigrants minus the number of emigrants. It is expressed as thousands and the numbers are available across different countries. Looking into the data, the first thought came to my mind is to draw this on world map. And it can be done very easily in R. Later in this post I describe the r code. But before that, let us see the world heat-map.




As expected population drift is from less developed to more developed regions.

Here is the R code for this-

#Required Library- fastest way to draw this chart is through ‘rworldmap’ package

library(rworldmap)

#Load Data- it loads data in R environment

migrantsData <- read.csv("Migrants_data.csv")

#Join data to map- this step essentially attaches location (Long\ Lat) for each country

spdf <- joinCountryData2Map(migrantsData,joinCode="UN", nameJoinColumn="Country.code",nameCountryColumn= "Country",verbose = TRUE)

#Draw world map- required function to draw this map is ‘mapCountryData’. For more details on this, #please refer - ‘http://cran.r-project.org/web/packages/rworldmap/rworldmap.pdf’

par(mai=c(0,0,0.2,0),xaxs="i",yaxs="i")

mapParams <- mapCountryData( spdf, nameColumnToPlot="Migrants", addLegend=FALSE,numCats=7,colourPalette="terrain", mapTitle="Net Number of Migrants (Immigrants - Emigrants), 2005-2010 (Thousands)", oceanCol="lightblue")

do.call( addMapLegend, c(mapParams, legendWidth=0.5, legendMar = 4,legendLabels="all"))  

And that’s it! Similar code can be used for any other metric.


Thanks, for now!