Package

pacman::p_load(tidyverse, dismo, magrittr, leaflet)

DATA

Load data

We will load data of Cecropia peltata from GBIF with gbif function of package dismo.

cecropia_gbif <- gbif("Cecropia", geo = T)
write_csv(cecropia_gbif, "cecropia_gbif.csv")

Filter data

We filter data with condition continent have string “AMERICA” and select 3 columns “Species, long and lat”. After that we remove NA in data with function na.omit

cecropia <- cecropia_gbif %>%  
  filter(str_detect(continent,"AMERICA")) %>% 
  dplyr::select(species,lon,lat)
cecropia <- na.omit(cecropia)

View data

knitr::kable(head(cecropia, 10))
species lon lat
Cecropia peltata -70.74133 7.013617
Cecropia peltata -73.35900 7.268000
Cecropia peltata -74.80731 7.912361
Cecropia peltata -74.78366 7.882264
Cecropia peltata -74.80700 7.870556
Cecropia peltata -74.79675 7.930417
Cecropia peltata -74.82233 7.591100
Cecropia peltata -74.78366 7.882264
Cecropia peltata -74.78366 7.882264
Cecropia peltata -74.80319 7.902611

MAP WITH LEAFLET

Ex 1

leaflet(data = cecropia) %>% addTiles() %>%
  addCircleMarkers(~lon, ~lat, popup=~species)

Ex 2

leaflet(data = cecropia) %>% addTiles() %>%
  addCircleMarkers(~lon, ~lat, popup=~species, clusterOptions = markerClusterOptions())

Ex 3 we add base map

View more base map at object providers

# add base map
leaflet(data = cecropia) %>% 
  addProviderTiles("Esri", group="Esri") %>%
  addProviderTiles("OpenTopoMap", group = "OpenTopoMap") %>%
  addProviderTiles("OpenStreetMap", group = "Road map") %>%
  addProviderTiles("Esri.WorldImagery", group = "Satellite") %>%
  
  addLayersControl(position = 'bottomright',
                   baseGroups = c("Esri","OpenTopoMap", "Road map", "Satellite"),
                   options = layersControlOptions(collapsed = FALSE)) %>%
  
  addCircleMarkers(~lon, ~lat,
                   color='red', radius=5, 
                   clusterOptions = markerClusterOptions(),
                   popup=~species)

Add photo to popup of point

pts <- data.frame(Latitude =12, Longitude = 108)

file <- 'https://upload.wikimedia.org/wikipedia/commons/thumb/c/c1/Rlogo.png/274px-Rlogo.png'

leaflet() %>%
  addTiles %>%
  addCircleMarkers(data = pts, lng =~Longitude, lat = ~Latitude,
                   popup = paste0("<img src = ", file, ">"))

Add 2 photos to popup of 1 point

pts <- data.frame(Latitude =12, Longitude = 108)
file1 <- 'https://upload.wikimedia.org/wikipedia/commons/thumb/c/c1/Rlogo.png/274px-Rlogo.png'
file2 <- 'https://udemy-images.udemy.com/course/480x270/804290_94b5_3.jpg'

leaflet() %>%
  addTiles %>%
  addCircleMarkers(data = pts, lng =~Longitude, lat = ~Latitude,
                   popup = paste0("<img src = ", file1, "> <p>",
                                  "<img src = ", file2, ">"))

Add 2 photos to popup of 2 points

file1 <- 'https://upload.wikimedia.org/wikipedia/commons/thumb/c/c1/Rlogo.png/274px-Rlogo.png'
file2 <- 'https://udemy-images.udemy.com/course/480x270/804290_94b5_3.jpg'
pts <- data.frame(Latitude =c(12,13), Longitude = c(108,109))

leaflet() %>%
  addTiles %>%
  addCircleMarkers(data = pts, lng =~Longitude, lat = ~Latitude,
                   popup = paste0("<img src = ", c(file1,file2), ">"))