Plotando o DataCamp HQ

Mapas Interativos com leaflet em R

Rich Majerus

Vice President of Strategy & Planning, Queens University of Charlotte

Plotando um ponto

# add marker layer to map
leaflet() %>% 
     addTiles() %>% 
     addMarkers(lng = -73.98575, 
                lat = 40.74856)

Exemplo com um único marcador

  • Fornecendo dados de marcadores

    • Colunas numéricas em data frame
    • Vetores numéricos
  • Padrões de addMarkers()

    • Centraliza em um único ponto
    • Ajusta o zoom para todos os pontos
Mapas Interativos com leaflet em R

Plotando vários pontos

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)

Exemplo com múltiplos marcadores

Mapas Interativos com leaflet em R

Plotando vários pontos II

# When piping a data frame into the leaflet function, R will search 
# for columns named lat/latitude and lon/lng/long/longitude  
dc_hq  %>% 
     leaflet() %>% 
     addTiles() %>% 
     addMarkers() 
Assuming 'lon' and 'lat' are longitude and latitude, respectively

Exemplo com múltiplos marcadores

Mapas Interativos com leaflet em R

Pop-ups

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

Marcadores com pop-ups

Mapas Interativos com leaflet em R

Pop-ups II

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

Exemplo de pop-ups

Mapas Interativos com leaflet em R

Armazenando mapas leaflet como objetos

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

Armazenando mapas como objetos

# %>% leaflet objects to functions 
# to add or edit layers
m %>% addMarkers(lng = dc_hq$lon, 
                 lat = dc_hq$lat, 
                 popup = dc_hq$hq)

Adicionando a camadas

Mapas Interativos com leaflet em R

Vamos praticar!

Mapas Interativos com leaflet em R

Preparing Video For Download...