loading the required packages
library(leaflet)
library(htmltools)
library(RColorBrewer)
library(rgbif)
library(tidyverse)
## ── Attaching core tidyverse packages ──────────────────────── tidyverse 2.0.0 ──
## ✔ dplyr 1.2.0 ✔ readr 2.1.6
## ✔ forcats 1.0.1 ✔ stringr 1.6.0
## ✔ ggplot2 4.0.2 ✔ tibble 3.3.1
## ✔ lubridate 1.9.5 ✔ tidyr 1.3.2
## ✔ purrr 1.2.1
## ── Conflicts ────────────────────────────────────────── tidyverse_conflicts() ──
## ✖ dplyr::filter() masks stats::filter()
## ✖ dplyr::lag() masks stats::lag()
## ℹ Use the conflicted package (<http://conflicted.r-lib.org/>) to force all conflicts to become errors
library(sf)
## Linking to GEOS 3.13.0, GDAL 3.8.5, PROJ 9.5.1; sf_use_s2() is TRUE
Importing a subset of the data, containing just the fish family I am interested in, for the map.
#download key
res <- "0003331-260225131425191"
centrarchidae<- occ_download_get(res) %>% occ_download_import() %>%
filter(family == 'Centrarchidae')
## Download file size: 5.33 MB
## file exists & overwrite=FALSE, not overwriting...
Downloaded dataset has coordinates but isn’t seen as a spatial object by R, just as a dataset. This code converts it into a spatial object needed to plot it on a map
centrarchidae<- st_as_sf(centrarchidae, coords = c('decimalLongitude','decimalLatitude'), crs =4326)
creating a color palette for the different species that will be plotted on the map
pal <- c('#8F00FF','#FB00FF','#00D2FF','#00FF47')
This piece of code creates an interactive map and plots where each species of centrachidae found in South africa have occured.
leaflet() %>%
addTiles(group = 'specificEpithet') %>%
addCircleMarkers(data = centrarchidae,
group =c("macrochirus","dolomieu",'punctulatus','salmoides'),
radius = 0.5,
color = pal,
opacity =1) %>%
addLegend(position = "topleft",
color=pal,
labels = c("Bluegill","Smallmouth Bass",'Spotted Bass','Largemouth Bass (Florida var.)'))