Basic Map

Column


Leaflet is an open source JavaScript library used to build web mapping applications. It creates embedding maps with tiled base layers, interactive panning and zooming and feature layers.


The {leaflet} R package allows to integrate and control Leaflet maps in R and to create maps from the R console or from RStudio. {leaflet} also provides interactive panning and zooming and allows rendering spatial objects from the {sp} or {sf} packages or from dataframes with latitude and longitude coordinates. There is also a leaflet plugins repository that you can use to add more features/plugins to your maps.


In this first post we will present a few examples using {leaflet}. The dashboard will show several base maps using map tiles selected from different providers. In those maps we will indicate a list of cities with more than one million population. The world cities dataset with long and lat coordinates comes from (simplemaps.com).

Column

Cities with more than 1,000,000 habitants

OpenTopoMap

OpenTopoMap

TonerBackground

TonerBackground

WorldShadedRelief

WorldShadedRelief

CartoDB

CartoDB

SpinalTap

SpinalTap

Earth at Night (NASA)

NASA

---
title: "Data visualization: Leaflet Maps (Part I)"
output: 
  flexdashboard::flex_dashboard:
    storyboard: true
    orientation: columns
    source_code: embed
    vertical_layout: fill
    theme: lumen
---



```{r setup, include=FALSE}


# PACKAGES / LIBRARIES:

library(flexdashboard)
library(tidyverse)
library(leaflet)
library(readxl)
library(leaflet.providers)
library(raster)
library(sf)


```

Basic Map
===========================================================================

Column {data-width=200}
---------------------------------------------------------------------------

### 

\

**Leaflet** is an open source JavaScript library used to build web mapping applications. It creates embedding maps with tiled base layers, interactive panning and zooming and feature layers. 

\

**The {leaflet} R package** allows to integrate and control Leaflet maps in R and to create maps from the R console or from RStudio. {leaflet} also provides interactive panning and zooming and allows rendering spatial objects from the {sp} or {sf} packages or from dataframes with latitude and longitude coordinates. There is also a [leaflet plugins repository](https://leafletjs.com/plugins) that you can use to add more features/plugins to your maps. 

\

**In this first post** we will present a few examples using {leaflet}. The dashboard will show several base maps using map tiles selected from different providers. In those maps we will indicate a list of cities with more than one million population. The world cities dataset with long and lat coordinates comes from ([simplemaps.com](https://simplemaps.com/data/world-cities)). 


Column
---------------------------------------------------------------------------

### Cities with more than 1,000,000 habitants

```{r}


library(readxl)
worldcities <- read_excel("C:/Users/Usuario/Desktop/r_que_r/r_que_r/content/datasets/worldcities.xlsx")

worldcities %>%
  filter(population > 1000000) %>%
  leaflet() %>%
  addTiles() %>%
  addCircles(label = ~paste("", city))

```


OpenTopoMap
===========================================================================

### OpenTopoMap

```{r}

worldcities %>%
  filter(population > 1000000) %>%
  leaflet() %>%
  addProviderTiles(providers$OpenTopoMap) %>%
  addCircleMarkers( radius = ~population/1000000,
                    label = ~city,
                    color = "white",
                    weight = 2,
                    opacity = 0.6,
                    fill = TRUE,
                    fillColor = "purple",
                    fillOpacity = 0.9,
                    stroke = TRUE,
                    popup = ~paste( "City:", city ,
                                    "
", "Population:", population)) ``` TonerBackground =========================================================================== ### TonerBackground ```{r} worldcities %>% filter(population > 1000000) %>% leaflet() %>% addProviderTiles(providers$Stamen.TonerBackground) %>% addCircleMarkers( radius = ~population/500000, label = ~city, color = "white", weight = 2, opacity = 0.6, fill = TRUE, fillColor = "orange", fillOpacity = 0.6, stroke = TRUE, popup = ~paste( "City:", city , "
", "Population:", population)) ``` WorldShadedRelief =========================================================================== ### WorldShadedRelief ```{r} worldcities %>% filter(population > 1000000) %>% leaflet() %>% addProviderTiles(providers$Esri.WorldShadedRelief) %>% addCircleMarkers( radius = ~population/500000, label = ~city, color = "white", weight = 2, opacity = 0.6, fill = TRUE, fillColor = "red", fillOpacity = 1, stroke = TRUE, popup = ~paste( "City:", city , "
", "Population:", population)) ``` CartoDB ============================================================================== ### CartoDB ```{r} m <- leaflet() %>% setView(0,0,2.5) CartoDB <-providers %>% purrr::keep( ~grepl('^CartoDB',.)) CartoDB %>% purrr::walk(function(x) m <<- m %>% addProviderTiles(x,group=x)) cities <- worldcities %>% filter ( population > 1000000) m %>% addLayersControl( baseGroups = names(CartoDB), options = layersControlOptions(collapsed = FALSE) ) %>% addCircleMarkers( data = cities, radius = ~population/1000000, label = ~city, color = "magenta", weight = 2, opacity = 0.6, fill = TRUE, fillColor = "magenta", fillOpacity = 0.4, stroke = FALSE, popup = ~paste( "City:", city , "
", "Population:", population)) ``` SpinalTap ========================================================================= ### SpinalTap ```{r echo=FALSE} worldcities %>% filter(population > 1000000) %>% leaflet() %>% addProviderTiles(providers$Thunderforest.SpinalMap, options = providerTileOptions(apikey = "9ca8915290af46cdbac8933262f77d2d")) %>% addCircleMarkers( radius = ~population/500000, label = ~city, color = "white", weight = 2, opacity = 0.6, fill = TRUE, fillColor = "green", fillOpacity = 0.9, stroke = TRUE, popup = ~paste( "City:", city , "
", "Population:", population)) %>% setView(16.36, 48.20, 5) ``` Earth at Night (NASA) =========================================================================== ### NASA ```{r} worldcities %>% filter(city == "Vitoria-Gasteiz") %>% leaflet() %>% addProviderTiles(providers$NASAGIBS.ViirsEarthAtNight2012) %>% addMarkers(label = "This is where I live") %>% setView(-2.67, 42.85, 3.2) ```