17/3/2020

knitr::opts_chunk$set(echo = TRUE)

Today is:

today <- Sys.Date()
format(today, format="%B %d %Y")
## [1] "marzo 17 2020"

Loading the libraries

suppressPackageStartupMessages(library(dplyr))
library(leaflet)

Loading the dataset (Taken from https://simplemaps.com/data/pe-cities)

df.original <- read.csv(file = 'pe.csv')
df <- df.original %>% select( city, admin, lat, lng, population)

Exploring the dataset

str(df)
'data.frame':   89 obs. of  5 variables:
 $ city      : Factor w/ 88 levels "Abancay","Andoas",..: 49 9 3 86 16 42 35 61 25 18 ...
 $ admin     : Factor w/ 25 levels "Amazonas","Ancash",..: 15 7 4 13 14 16 12 20 8 2 ...
 $ lat       : num  -12.05 -12.06 -16.4 -8.11 -6.73 ...
 $ lng       : num  -77 -77.1 -71.5 -79 -79.8 ...
 $ population: int  8012000 876877 815000 765171 596792 458729 412733 396932 361182 349846 ...
#summary(df)
head(df)
      city       admin        lat       lng population
1     Lima        Lima -12.050000 -77.05000    8012000
2   Callao      Callao -12.056585 -77.11814     876877
3 Arequipa    Arequipa -16.398889 -71.53500     815000
4 Trujillo La Libertad  -8.111944 -79.02556     765171
5  Chilape  Lambayeque  -6.729548 -79.83530     596792
6  Iquitos      Loreto  -3.749125 -73.25383     458729

These are the map configurations

my_map <- addTiles(leaflet(df))

my_map <- df %>%
  leaflet() %>%
  addProviderTiles(providers$OpenStreetMap) %>% 
  addTiles() %>% 
  addCircles( weight=1,radius = sqrt(df$population)*2,
              lng=df$lng,lat=df$lat,fillOpacity = 0.8) %>% 
  addCircleMarkers( clusterOptions = markerClusterOptions(),
                    lng=df$lng,lat=df$lat, color = 'red',
                    popup = paste('City:',df$city, '<br>',
                                  'Province:',df$admin, '<br>',
                                  'Population:',df$population, '<br>',
                                  'lat:', df$lat, '<br>',
                                  'lng', df$lng)
                    
                      )

Now, it’s time to see the map

my_map