1. Introduction

The Leaflet library with javascript components is a tool that allows the creation of interactive maps. In RStudio javascript is not needed since within its packages it contains the tools for creating the maps. For this work we use coordinate data from the city of Neiva - Huila - Colombia

2. Instalation

install.packages("leaflet")

3. First map to develop

To start using the function leaflet (), which allows you to create a map widget that stores any variable to modify the map later. With the use of the operator (%>%), the same as the library in dplyr that is previously installed. The addTiles () function adds all the mapping data from Open Street Map.

library(leaflet)
map <- leaflet() %>% 
  addTiles()
map

### Warning: package ‘leaflet’ was built under R version 3.3.2

4.Adding Markers

Markers can be added to the created map using the addMarkers () function, where length and latitude is specified. Pop-up texts can be included for when the marker is clicked with the argument.

library(leaflet)
map <- map %>%
  addMarkers(lat=2.9255469, lng=-75.2890046, 
             popup="Catedral de la Inmaculada Concepción de Neiva - Huila -Colombia")
map

5. Adding Many Markers

100 different points of places in the city of Neiva - Huila were implemented to represent the exercise.

set.seed(2020-04-28)
df <- data.frame(lat = runif(50, min = 2.9255, max = 2.93),
                 lng = runif(50, min = -75.2890046, max = -75.2756784))
df %>% 
  leaflet() %>%
  addTiles() %>%
  addMarkers()

6. Drawing Circles

Marking of 5 points near the city of Neiva, which make up municipalities near the capital Huilense.

md_cities <- data.frame(name = c("Neiva", "Palermo","Aipe","Villavieja","Rivera","Tello"),
                        pop = c(20000,5000,5000,5000,5000,5000),
                        lat = c(2.9265247,2.8885394,3.2221164,3.2183677,2.7782127,3.0673612),
                        lng = c(-75.2890714,-75.4355222,-75.2498213,-75.2219373,-75.2588954,-75.1424828))
md_cities %>%
  leaflet() %>%
  addTiles() %>%
  addCircles(weight = 1, radius = sqrt(md_cities$pop) * 30)
## Assuming "lng" and "lat" are longitude and latitude, respectively

7. Drawing Rectangles

Marking of a rectangular central area of the city of Neiva.

leaflet() %>%
  addTiles() %>%
  addRectangles(lat1 = 2.9255469, lng1 = -75.2890046, 
                lat2 = 2.9299469, lng2 = -75.2899947)

8. Adding Legends

100 different points of places in the city of Neiva - Huila were implemented to represent the exercise a colour

df <- data.frame(lat = runif(100, min = 2.9255, max = 2.93),
                 lng = runif(100, min = -75.2890046, max = -75.2756784),
                 col = sample(c("red", "blue", "green"), 20, replace = TRUE),
                 stringsAsFactors = FALSE)

df %>%
  leaflet() %>%
  addTiles() %>%
  addCircleMarkers(color = df$col) %>%
  addLegend(labels = LETTERS[1:3], colors = c("blue", "red", "green"))
## Assuming "lng" and "lat" are longitude and latitude, respectively