Overview

The objective of this assignment is to show the population and population density of districts in the city of Delhi on a map using leaflet package in R.

Loading the data of Delhi districts

# Load leaflet library
library(leaflet)
## Warning: package 'leaflet' was built under R version 3.3.2
# Read csv file to load the data
DelhiDistt<-read.csv("DelhiDistricts.csv",header=TRUE)

# Convert population variables to numeric
DelhiDistt$Pop.2011<-as.numeric(gsub(",","",as.character(DelhiDistt$Pop.2011)))
DelhiDistt$Pop.2001<-as.numeric(gsub(",","",as.character(DelhiDistt$Pop.2001)))
DelhiDistt$Pop.1991<-as.numeric(gsub(",","",as.character(DelhiDistt$Pop.1991)))

# Create a variable for the map popups
DelhiDistt$Popup<-paste(DelhiDistt$District,"  |  Population ('000s):",round(DelhiDistt$Pop.2011/1000),"  |  Pop Density:",round(DelhiDistt
$Pop.Density.2011))

Add circles, markers and legend to the map

  1. Add tiles
  2. Add blue circles depicting the relative size of population in various districts of Delhi
  3. Add red circles depicting the relative size of population density in the districts
  4. Add markers at the location of district headquarters with clustering enabled
  5. Add a legend for the coloured circles
my_map<-DelhiDistt%>%
  leaflet()%>%
  addTiles()%>%
  addCircles(lat=DelhiDistt$Lat,lng=DelhiDistt$Lng,weight=1,radius=sqrt(DelhiDistt$Pop.2011),col="blue")%>%
  addCircles(lat=DelhiDistt$Lat,lng=DelhiDistt$Lng,weight=1,radius=sqrt(DelhiDistt$Pop.Density.2011)*5,col="red")%>%
  addMarkers(lat=DelhiDistt$Lat,lng=DelhiDistt$Lng,popup=DelhiDistt$Popup,clusterOptions=markerClusterOptions())%>%
  addLegend(labels=c("Population","Population Density"),colors=c("blue","red"))

Render Map

my_map