11  Interactive Map Visualisation

# install.packages("devtools")
#devtools::install_github("thiyangt/ceylon")
library(ceylon)

11.1 Static Plot

ggplot(province) + 
  geom_sf(mapping = aes(fill = population), show.legend = TRUE) + scale_fill_viridis()

11.2 Using mapview

mapview(
  province,
  zcol = "population",
  legend = TRUE
)

11.3 Using leaflet

m <- leaflet() %>%
  addTiles() %>%  # OpenStreetMap tiles
  addMarkers(
    lng = 79.8612,
    lat = 6.9271,
    popup = "Colombo, Sri Lanka"
  )
m
#htmltools::save_html(m, file = "../HtmlWidget/leaflet-sri-lanka.html")
library(leaflet)

leaflet() %>%
  addTiles() %>%
  addMarkers(
    lng = c(-118.2437, -74.0060),
    lat = c(34.0522, 40.7128),
    popup = c("Los Angeles, CA", "New York City, NY")
  ) %>%
  addRectangles(
    lng1 = 79.6, lat1 = 5.9,   # bottom-left
    lng2 = 81.9, lat2 = 9.9,   # top-right
    fill = FALSE,
    color = "red",
    weight = 2,
    popup = "Sri Lanka Bounding Box"
  )

11.4 Exercise

Write the code to obtain the following plot