Interactive Maps in R with Leaflet

Install and Load Packages

# install.packages("leaflet")
# install.packages("dplyr")
library(leaflet)
library(dplyr)
## 
## 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
library(readr)

Basic Interactive Map

map <- leaflet() %>%
  addTiles()   # default OpenStreetMap tiles
map

Add Marker (My Home)

r_MyHome <- leaflet() %>%
  addTiles() %>%
  addMarkers(lng = 90.409739, lat = 23.757419,
             popup = "Home")
r_MyHome

Load Map Data

Data source: Bangladesh Healthsites

bangladesh <- read_csv("C:/Users/user/OneDrive/Desktop/bangladesh.csv")
## Warning: One or more parsing issues, call `problems()` on your data frame for details,
## e.g.:
##   dat <- vroom(...)
##   problems(dat)
## Rows: 6883 Columns: 35
## ── Column specification ────────────────────────────────────────────────────────
## Delimiter: ","
## chr (18): osm_type, amenity, healthcare, name, operator, source, speciality,...
## dbl  (6): X, Y, osm_id, completeness, changeset_id, changeset_version
## lgl (11): operational_status, beds, staff_doctors, staff_nurses, health_amen...
## 
## ℹ Use `spec()` to retrieve the full column specification for this data.
## ℹ Specify the column types or set `show_col_types = FALSE` to quiet this message.
dhaka <- bangladesh %>%
  filter(addr_city == "Dhaka") %>%
  filter(!(X == "NA"))

Add Data to Map

m <- leaflet(dhaka) %>%
  addTiles() %>%
  addCircles(~X, ~Y, popup = dhaka$name)
m

Different Map Views

CartoDB DarkMatter

m <- leaflet(dhaka) %>%
  addTiles() %>%
  addCircles(~X, ~Y, popup = dhaka$name) %>%
  addProviderTiles(providers$CartoDB.DarkMatter)
m

OpenTopoMap

m <- leaflet(dhaka) %>%
  addTiles() %>%
  addProviderTiles(providers$OpenTopoMap) %>%
  addMarkers(lng = 90.409739, lat = 23.757419, popup = "MyHome")
m

👉 More map styles: Leaflet Providers Preview


Customize Circle Style

m <- leaflet(dhaka) %>%
  addTiles() %>%
  addCircles(~X, ~Y,
             popup = dhaka$name,
             weight = 13,
             radius = 4,
             stroke = TRUE,
             fillOpacity = 0.5,
             color = "red")
m

Color by Category

pal <- colorFactor(
  c("green", "red", "blue", "black", "navy"),
  domain = c("clinic", "hospital", "dentist", "doctors", "pharmacy")
)

leaflet(dhaka) %>%
  addTiles() %>%
  addCircleMarkers(
    ~X, ~Y,
    color = ~pal(amenity),
    stroke = TRUE,
    fillOpacity = 0.5,
    popup = ~name,
    label = ~as.character(amenity)
  )

Add Popup with Hospital List

content <- paste(sep = "<br/>",
  "<b><a href='https://en.wikipedia.org/wiki/List_of_hospitals_in_Bangladesh'>List of Health Facility</a></b>",
  dhaka$name,
  dhaka$amenity
)

m <- leaflet(dhaka) %>%
  addTiles() %>%
  addCircles(~X, ~Y, popup = content)
m

Add Labels with Style

content <- paste(sep = "<br/>", dhaka$name, dhaka$amenity)

m <- leaflet(dhaka) %>%
  addTiles() %>%
  addMarkers(~X, ~Y, popup = content,
    label = ~name,
    labelOptions = labelOptions(
      noHide = FALSE,
      direction = "bottom",
      style = list(
        "color" = "red",
        "font-family" = "serif",
        "font-style" = "italic",
        "box-shadow" = "3px 3px rgba(0,0,0,0.25)",
        "font-size" = "12px",
        "border-color" = "rgba(0,0,0,0.5)"
      )
    ))
m