Choropleth Maps in R
Choropleth maps provides a very simple and easy way to understand visualizations of a measurement across different geographical areas, be it states or countries.
If you were to compare growth rate of Indian states and present it to a bunch of people who have 15-20 seconds to look at it and infer insights from the data, what would be the right way? The best way? Would presenting the data in the traditional tabular format make sense? Or bar graphs would look better?
Bar graphs, indeed, will look better and present the data in visually appealing manner and provide a good comparison; but, will it make an impact in 15 seconds? I personally won’t be able to bring the desired outcome, moreover data for 36 states and union territories in 36 bars will make it cumbersome to scroll up and down. We have a much better alternative to table and bar charts, choropleth maps.
Choropleth maps are thematic maps in which different areas are colored or shaded in accordance with the value of a statistical variable being represented in the map. Taking an example, let’s say we were to compare population density in different states of the United States of America in a colorful manner, choropleth maps would be our best bet for representation. To sum it up, choropleth maps provides a very simple and easy way to understand visualizations of a measurement across different geographical areas, be it states or countries.
Let’s take some examples of choropleth maps and where they come handy in presenting data.
- Choropleth maps are widely used to represent macroeconomic variables such as GDP growth rate, population density, per-capita income, etc. on a world map and provide a proportional comparison among countries. This can also be done for states within a country.
- These maps can also be used to present nominal data such as gain/loss/no change in number of seats by an election party in a country.
One of the limitations of using choropleth maps is that they don’t provide details of total or absolute values. They are among the best for proportional comparison but when it comes to presenting absolute values, choropleth maps are not the right fit.
Now, let us try to see the practical implementation of choropleth maps in R. In the following code, we will try to achieve the following objectives as part of the overall implementation of the maps.
- Download and import the maps shape in R
- Creating our own dataset and representing it in the map of India
- Merging dataset and preparing it for visual representation
- Improving visualization
- Display external data on choropleth maps
- Presenting multiple maps at once
Download and import the maps share in R
There are multiple sites from where you can download shape files for free. I used this site (http://www.diva-gis.org/gdata) for downloading administrative map of India for further processing. Once you download the file, unzip the file and set your R working directory to the unzipped folder.
We will install all the necessary libraries at once and discuss one by one as we proceed along.
Set the working directory to the unzipped folder and use the following code to import the shape into R.
ID_1 provides a unique id for each of 36 states and union territories; while the NAME_1 provides the name of each of the states and union territories. We will be mainly using these two fields, other fields provide name of the country, code of the country and other information which separates data of one country from the other.
Alternatively, there is another function from different package which we can use to import shape into R.
In the above code “readOGR(“.”,”IND_adm1”), “.” means that the shapefile which we want to read is in our working directory; else, we would have to mention the entire path. Also, we need to mention the shapefile name without extension otherwise it will throw an error.
Creating our own dataset and representing it in the map of India
To begin with, we will create our own data for each of the 36 IDs and call it score D, a parameter which represents dancing talent of each of the states. (Please note that this score is randomly generated and does not reflect the true dancing talent :P).
Merging dataset and preparing it for visual representation
We will use the function fortify() of ggplot2 package to get the shape file into a data frame and then merge the data frame file and dataset together.
Now, let’s create a basic visualization and see how our maps looks like.
Top Stories Past 30 Days