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.
# 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))
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"))
my_map