This document was rendered at August 10, 2019 at 09:52:12.
Below is a list of prominent cities in India. Each row includes a city’s latitude, longitude, state and population of each city. The data set was chosen for the assignment.
library(jsonlite)
library(leaflet)
library(DT) # Bringing in the DataTables package to display the data in a nice format
# Removing all environment variables.
rm(list = ls())
# reading the JSON file from India Cities Database
INmapdata <- fromJSON("https://simplemaps.com/static/data/country-cities/in/in.json", flatten = TRUE)
# converting Lattitude and Longitude from character to numeric
INmapdata$lat <- as.numeric(as.character(INmapdata$lat))
INmapdata$lng <- as.numeric(as.character(INmapdata$lng))
INmapdata$population <- as.numeric(as.character(INmapdata$population))
# Sort by vector name [z]
INmapdata <-INmapdata[with(INmapdata, order(-population)),]
# Using the datatable function from the DT package to see the first 6 rows of data
datatable(head(INmapdata,50))
Plotting the India map and showing up popup icons for top 50 cities arranged by population in descending order.
# get the first 50 most populated citities of India
df.50 <- INmapdata[1:50,]
# Defining function to get the colour of icons based on the population of city
getColor <- function(INmapdata) {
sapply(INmapdata$population, function(population) {
if(population <= 2000000) {
"green"
} else if(population <= 4000000) {
"orange"
} else {
"red"
} })
}
icons <- awesomeIcons(
icon = 'ios-close',
iconColor = 'black',
library = 'ion',
markerColor = getColor(df.50)
)
# Plotting the map
leaflet(df.50) %>% addTiles() %>%
setView(77.402892,23.254688, zoom = 5)%>%
addAwesomeMarkers(~lng, ~lat, icon=icons, label=~as.character(population))
References : all data is taken from https://simplemaps.com/data/in-cities