Introduction

Leaflet is one of the most popular Javascript libraries for creating interactive maps. The leaflet R package allows you to create your own leaflet maps without needing to know any Javascript.

In this assignment, we generated a world map containing

Details of interactive map

In this assignment, we generated a world map via the Leaflet package, that highlights all cities with a population > 500,000. Feel free to navigate through it and if you selected a marker, it’ll tell you the city name and its population as of January 2006.

Below is the R code that makes this possible:

library(leaflet)
library(maps)
library(dplyr, quietly = TRUE)
## 
## Attaching package: 'dplyr'
## The following objects are masked from 'package:stats':
## 
##     filter, lag
## The following objects are masked from 'package:base':
## 
##     intersect, setdiff, setequal, union
data("world.cities")

world.cities <- world.cities %>% filter(pop > 500000)
world.cities <- mutate(world.cities, popup_label = paste0(world.cities$name, ". Pop = ", world.cities$pop))
etiqueta <- paste("Cities with population > 500,000 as of January 2006 -", "Time generated =",
              format(Sys.Date(), "%d %b %Y"))

my_map <- leaflet() %>% 
             addTiles()

my_map <- addMarkers(my_map, lng = world.cities$long, lat = world.cities$lat,
                     popup = world.cities$popup_label,
                     clusterOptions = markerClusterOptions())    

my_map <- addLegend(my_map, "topright", 
                      labels = etiqueta,
                      colors = "blue")

Happy surfing…!

my_map

```