Mappare DataCamp HQ

Mappe interattive con leaflet in R

Rich Majerus

Vice President of Strategy & Planning, Queens University of Charlotte

Mappare un punto

# aggiungi un layer di marker alla mappa
leaflet() %>% 
     addTiles() %>% 
     addMarkers(lng = -73.98575, 
                lat = 40.74856)

Esempio con un singolo marker

  • Fornire i dati dei marker

    • Colonne numeriche in un data frame
    • Vettori numerici
  • Predefiniti di addMarkers()

    • Centrata su un singolo punto
    • Zoom per includere tutti i punti
Mappe interattive con leaflet in R

Mappare più punti

dc_hq <- 
     tibble(
         hq  = c("DataCamp - NYC", "DataCamp - Belgium"),
         lon = c(-73.98575, 4.717863),
         lat = c(40.74856, 50.881363))
leaflet() %>% 
     addTiles() %>% 
     addMarkers(lng = dc_hq$lon, lat = dc_hq$lat)

Esempio con più marker

Mappe interattive con leaflet in R

Mappare più punti II

# Quando passi un data frame a leaflet con la pipe, R cerca 
# colonne chiamate lat/latitude e lon/lng/long/longitude  
dc_hq  %>% 
     leaflet() %>% 
     addTiles() %>% 
     addMarkers() 
Assuming 'lon' and 'lat' are longitude and latitude, respectively

Esempio con più marker

Mappe interattive con leaflet in R

Pop-up

leaflet() %>% 
     addTiles() %>% 
     addMarkers(lng = dc_hq$lon, lat = dc_hq$lat, 
               popup = dc_hq$hq)

Esempio di marker con pop-up

Mappe interattive con leaflet in R

Pop-up II

leaflet() %>% 
     addTiles() %>% 
     addPopups(lng = dc_hq$lon, lat = dc_hq$lat,
               popup = dc_hq$hq)

Esempio di pop-up

Mappe interattive con leaflet in R

Salvare mappe leaflet come oggetti

m <- leaflet() %>%
     addTiles()  %>% 
     setView(lng = dc_hq$lon[1], 
             lat = dc_hq$lat[1], 
             zoom = 12)

Salvare le mappe come oggetti

# usa %>% per passare oggetti leaflet a funzioni 
# per aggiungere o modificare layer
m %>% addMarkers(lng = dc_hq$lon, 
                 lat = dc_hq$lat, 
                 popup = dc_hq$hq)

Aggiungere layer

Mappe interattive con leaflet in R

Esercitiamoci!

Mappe interattive con leaflet in R

Preparing Video For Download...