Heatmap using R

 Author: Niloy Ghosh


In a lot of scenarios, visualising data and information on world map can give some interesting insights and perspective.

In the current example, we are using London Olympic Athlete Data for plotting. The results of clustering has been visualised on world map to see how clusters are presented across countries.

London Olympic Athlete Data has been used for Clustering.

Tutorial on Clustering using R

The clusters were created based on Athlete Height and Weight. But the clusters were significantly different on profiled variables - age, and winning rate.

Profiling and Summary Statistics of the Clusters

Reading data into R

One of the first step in plotting and visualisation is reading data into R.

Manipulate Data using R

Objective of the blog is to plot count of athletes from each country and visualise on the world map. The heatmap so plot can give a great visualisation of the countries sent higher number of athletes to London Olympic.

Considering clusters have been created based on K Means. Heat-map shows spreads of clusters across countries.

sqldf is used to count athletes based on cluster and country.

Load Map Plotting Packages

Map plotting requires a few R Packages; hence they should be installed and load below using heatmap function.

Plotting Clusters on the Worldmap

Get data by cluster and plot on the worldmap as a heatmap.

Steps used:

  • Subset data for each cluster
  • Create player count groups using cut function
  • Create color palatte
  • Add country
  • Draw on worldmap using map function

Similarly other clusters can be plotted to the worldmap.