R Markdown

#rename file
old_name <- "0021923-240216155721649.csv"
new_name <- "passerina.csv"

file.rename(old_name, new_name)
## Warning in file.rename(old_name, new_name): cannot rename file
## '0021923-240216155721649.csv' to 'passerina.csv', reason 'No such file or
## directory'
## [1] FALSE
#read csv
passerina_data <- read.csv("passerina.csv", row.names = NULL)
#it seems the column headers are concatenated
concatenated_columns <- grep("\\s|[^[:alnum:]_]", colnames(passerina_data), value = TRUE)
if(length(concatenated_columns) > 0) {
  print("Concatenated column names found:")
  print(concatenated_columns)
} else {
  print("No concatenated column names found.")
}
## [1] "Concatenated column names found:"
## [1] "row.names"                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                  
## [2] "gbifID.datasetKey.occurrenceID.kingdom.phylum.class.order.family.genus.species.infraspecificEpithet.taxonRank.scientificName.verbatimScientificName.verbatimScientificNameAuthorship.countryCode.locality.stateProvince.occurrenceStatus.individualCount.publishingOrgKey.decimalLatitude.decimalLongitude.coordinateUncertaintyInMeters.coordinatePrecision.elevation.elevationAccuracy.depth.depthAccuracy.eventDate.day.month.year.taxonKey.speciesKey.basisOfRecord.institutionCode.collectionCode.catalogNumber.recordNumber.identifiedBy.dateIdentified.license.rightsHolder.recordedBy.typeStatus.establishmentMeans.lastInterpreted.mediaType.issue"
#it says there are concatenated column names so now im gonna separate them

library(readr)

passerina_data <- read_csv("passerina.csv")
## Warning: One or more parsing issues, call `problems()` on your data frame for details,
## e.g.:
##   dat <- vroom(...)
##   problems(dat)
## Rows: 389281 Columns: 1
## ── Column specification ────────────────────────────────────────────────────────
## Delimiter: ","
## chr (1): gbifID  datasetKey  occurrenceID    kingdom phylum  class   order   family  ge...
## 
## ℹ Use `spec()` to retrieve the full column specification for this data.
## ℹ Specify the column types or set `show_col_types = FALSE` to quiet this message.
column_names <- "gbifID\tdatasetKey\toccurrenceID\tkingdom\tphylum\tclass\torder\tfamily\tgenus\tspecies\tinfraspecificEpithet\ttaxonRank\tscientificName\tverbatimScientificName\tverbatimScientificNameAuthorship\tcountryCode\tlocality\tstateProvince\toccurrenceStatus\tindividualCount\tpublishingOrgKey\tdecimalLatitude\tdecimalLongitude\tcoordinateUncertaintyInMeters\tcoordinatePrecision\televation\televationAccuracy\tdepth\tdepthAccuracy\teventDate\tday\tmonth\tyear\ttaxonKey\tspeciesKey\tbasisOfRecord\tinstitutionCode\tcollectionCode\tcatalogNumber\trecordNumber\tidentifiedBy\tdateIdentified\tlicense\trightsHolder\trecordedBy\ttypeStatus\testablishmentMeans\tlastInterpreted\tmediaType\tissue"

split_column_names <- strsplit(column_names, "\t")[[1]]

print(split_column_names)
##  [1] "gbifID"                           "datasetKey"                      
##  [3] "occurrenceID"                     "kingdom"                         
##  [5] "phylum"                           "class"                           
##  [7] "order"                            "family"                          
##  [9] "genus"                            "species"                         
## [11] "infraspecificEpithet"             "taxonRank"                       
## [13] "scientificName"                   "verbatimScientificName"          
## [15] "verbatimScientificNameAuthorship" "countryCode"                     
## [17] "locality"                         "stateProvince"                   
## [19] "occurrenceStatus"                 "individualCount"                 
## [21] "publishingOrgKey"                 "decimalLatitude"                 
## [23] "decimalLongitude"                 "coordinateUncertaintyInMeters"   
## [25] "coordinatePrecision"              "elevation"                       
## [27] "elevationAccuracy"                "depth"                           
## [29] "depthAccuracy"                    "eventDate"                       
## [31] "day"                              "month"                           
## [33] "year"                             "taxonKey"                        
## [35] "speciesKey"                       "basisOfRecord"                   
## [37] "institutionCode"                  "collectionCode"                  
## [39] "catalogNumber"                    "recordNumber"                    
## [41] "identifiedBy"                     "dateIdentified"                  
## [43] "license"                          "rightsHolder"                    
## [45] "recordedBy"                       "typeStatus"                      
## [47] "establishmentMeans"               "lastInterpreted"                 
## [49] "mediaType"                        "issue"
#checking to see if it removed them
print(names(passerina_data))
## [1] "gbifID\tdatasetKey\toccurrenceID\tkingdom\tphylum\tclass\torder\tfamily\tgenus\tspecies\tinfraspecificEpithet\ttaxonRank\tscientificName\tverbatimScientificName\tverbatimScientificNameAuthorship\tcountryCode\tlocality\tstateProvince\toccurrenceStatus\tindividualCount\tpublishingOrgKey\tdecimalLatitude\tdecimalLongitude\tcoordinateUncertaintyInMeters\tcoordinatePrecision\televation\televationAccuracy\tdepth\tdepthAccuracy\teventDate\tday\tmonth\tyear\ttaxonKey\tspeciesKey\tbasisOfRecord\tinstitutionCode\tcollectionCode\tcatalogNumber\trecordNumber\tidentifiedBy\tdateIdentified\tlicense\trightsHolder\trecordedBy\ttypeStatus\testablishmentMeans\tlastInterpreted\tmediaType\tissue"
#it did separate the columns :D but for some reason added a "t" in front of them. i'm gonna remove them.
library(readr)

passerina_data <- read_tsv("passerina.csv")
## Warning: One or more parsing issues, call `problems()` on your data frame for details,
## e.g.:
##   dat <- vroom(...)
##   problems(dat)
## Rows: 293328 Columns: 50
## ── Column specification ────────────────────────────────────────────────────────
## Delimiter: "\t"
## chr  (32): datasetKey, occurrenceID, kingdom, phylum, class, order, family, ...
## dbl  (13): gbifID, individualCount, decimalLatitude, decimalLongitude, coord...
## lgl   (3): depth, depthAccuracy, typeStatus
## dttm  (2): dateIdentified, lastInterpreted
## 
## ℹ Use `spec()` to retrieve the full column specification for this data.
## ℹ Specify the column types or set `show_col_types = FALSE` to quiet this message.
colnames(passerina_data) <- gsub("\t", "", colnames(passerina_data))

print(names(passerina_data))
##  [1] "gbifID"                           "datasetKey"                      
##  [3] "occurrenceID"                     "kingdom"                         
##  [5] "phylum"                           "class"                           
##  [7] "order"                            "family"                          
##  [9] "genus"                            "species"                         
## [11] "infraspecificEpithet"             "taxonRank"                       
## [13] "scientificName"                   "verbatimScientificName"          
## [15] "verbatimScientificNameAuthorship" "countryCode"                     
## [17] "locality"                         "stateProvince"                   
## [19] "occurrenceStatus"                 "individualCount"                 
## [21] "publishingOrgKey"                 "decimalLatitude"                 
## [23] "decimalLongitude"                 "coordinateUncertaintyInMeters"   
## [25] "coordinatePrecision"              "elevation"                       
## [27] "elevationAccuracy"                "depth"                           
## [29] "depthAccuracy"                    "eventDate"                       
## [31] "day"                              "month"                           
## [33] "year"                             "taxonKey"                        
## [35] "speciesKey"                       "basisOfRecord"                   
## [37] "institutionCode"                  "collectionCode"                  
## [39] "catalogNumber"                    "recordNumber"                    
## [41] "identifiedBy"                     "dateIdentified"                  
## [43] "license"                          "rightsHolder"                    
## [45] "recordedBy"                       "typeStatus"                      
## [47] "establishmentMeans"               "lastInterpreted"                 
## [49] "mediaType"                        "issue"
#now i wanna remove all the columns we dont need
install.packages("dplyr")
## 
## The downloaded binary packages are in
##  /var/folders/90/sbk7b_0118vgxh1bg9b23g440000gn/T//RtmpxuJ1X5/downloaded_packages
library(dplyr)
## 
## Attaching package: 'dplyr'
## The following objects are masked from 'package:stats':
## 
##     filter, lag
## The following objects are masked from 'package:base':
## 
##     intersect, setdiff, setequal, union
passerina_data_subset <- passerina_data %>%
  select(gbifID,countryCode,locality,stateProvince,occurrenceStatus,individualCount,decimalLatitude,decimalLongitude,eventDate,day,month,year,dateIdentified)

#checking
print(names(passerina_data_subset))
##  [1] "gbifID"           "countryCode"      "locality"         "stateProvince"   
##  [5] "occurrenceStatus" "individualCount"  "decimalLatitude"  "decimalLongitude"
##  [9] "eventDate"        "day"              "month"            "year"            
## [13] "dateIdentified"
#for some reason there are points outside the US. I want to remove them.
library(dplyr)

passerina_data_subset_filtered <- passerina_data_subset %>%
  filter(countryCode == "US")
#load leaflet
library(leaflet)
library(tidyverse)
## ── Attaching core tidyverse packages ──────────────────────── tidyverse 2.0.0 ──
## ✔ forcats   1.0.0     ✔ stringr   1.5.1
## ✔ ggplot2   3.4.4     ✔ tibble    3.2.1
## ✔ lubridate 1.9.3     ✔ tidyr     1.3.1
## ✔ purrr     1.0.2     
## ── 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
install.packages("usmap")
## 
## The downloaded binary packages are in
##  /var/folders/90/sbk7b_0118vgxh1bg9b23g440000gn/T//RtmpxuJ1X5/downloaded_packages
library(usmap)

# Get spatial dataset of the United States
usa <- us_map(regions = "states")
#getting spatial data set for the US
library(dplyr)
library(usmap)
library(sf)
## Linking to GEOS 3.11.0, GDAL 3.5.3, PROJ 9.1.0; sf_use_s2() is TRUE
usa <- us_map(regions = "states")

usa_sf <- st_as_sf(usa)

usa_sf$area_of_state <- st_area(usa_sf)

head(usa_sf)
## Simple feature collection with 6 features and 4 fields
## Geometry type: MULTIPOLYGON
## Dimension:     XY
## Bounding box:  xmin: -2590847 ymin: -2608151 xmax: 1433831 ymax: -39560.82
## Projected CRS: NAD27 / US National Atlas Equal Area
## # A tibble: 6 × 5
##   fips  abbr  full                                            geom area_of_state
##   <chr> <chr> <chr>                             <MULTIPOLYGON [m]>         [m^2]
## 1 02    AK    Alaska     (((-2396840 -2547726, -2393291 -2546396,… 390315576600.
## 2 01    AL    Alabama    (((1093779 -1378539, 1093270 -1374227, 1… 133957186194.
## 3 05    AR    Arkansas   (((483066 -927786.9, 506063 -926262.2, 5… 137791084570.
## 4 04    AZ    Arizona    (((-1388677 -1254586, -1389182 -1251858,… 295231686421.
## 5 06    CA    California (((-1719948 -1090032, -1709613 -1090025,… 410516591657.
## 6 08    CO    Colorado   (((-789537.1 -678772.6, -789536.6 -67876… 269577779250.
#checking unique state values
unique_states_df <- unique(passerina_data_subset_filtered$stateProvince)

unique_states_sf <- unique(usa_sf$id)
## Warning: Unknown or uninitialised column: `id`.
print(unique_states_df)
##  [1] "United States - Florida (FL)"    "Texas"                          
##  [3] "Missouri"                        "South Carolina"                 
##  [5] "Louisiana"                       "Georgia"                        
##  [7] "Arizona"                         "Florida"                        
##  [9] "New York"                        "Tennessee"                      
## [11] "Maryland"                        "California"                     
## [13] "Oklahoma"                        "Illinois"                       
## [15] "Kansas"                          "Vermont"                        
## [17] "Iowa"                            NA                               
## [19] "Arkansas"                        "Ohio"                           
## [21] "Mississippi"                     "North Carolina"                 
## [23] "Massachusetts"                   "Alabama"                        
## [25] "Delaware"                        "New Hampshire"                  
## [27] "Michigan"                        "New Jersey"                     
## [29] "Virginia"                        "New Mexico"                     
## [31] "Oregon"                          "Colorado"                       
## [33] "Pennsylvania"                    "Minnesota"                      
## [35] "Connecticut"                     "Washington"                     
## [37] "Wyoming"                         "Rhode Island"                   
## [39] "Utah"                            "Maine"                          
## [41] "Nevada"                          "Wisconsin"                      
## [43] "North Dakota"                    "Kentucky"                       
## [45] "Nebraska"                        "District of Columbia"           
## [47] "West Virginia"                   "Unknown"                        
## [49] "Georgia (State)"                 "Florida (State)"                
## [51] "Texas (State)"                   "Tx"                             
## [53] "United States - Texas (TX)"      "Ok"                             
## [55] "Frorida"                         "South Dakota"                   
## [57] "Montana"                         "United States - Georgia (GA)"   
## [59] "Fl"                              "South carolina"                 
## [61] "Ga"                              "Louisiana, Captive"             
## [63] "Idaho"                           "Indiana"                        
## [65] "Floride"                         "Louisiane"                      
## [67] "North carolina"                  "United States - New Mexico (NM)"
## [69] "Nm"
print(unique_states_sf)
## NULL
#standardizing state names
state_mapping <- c(
  "Fl" = "Florida",
  "Tx" = "Texas",
  "Nm" = "New Mexico",
  "United States - Texas (TX)" = "Texas",
  "Georgia (State)" = "Georgia",
  "Florida (State)" = "Florida",
  "Texas (State)" = "Texas",
  "Ok" = "Oklahoma",
  "Frorida" = "Florida",
  "United States - Georgia (GA)" = "Georgia",
  "South carolina" = "South Carolina",
  "Ga" = "Georgia",
  "Louisiana, captive" = "Louisiana",
  "Floride" = "Florida",
  "Louisiane" = "Louisiana",
  "North carolina" = "North Carolina",
  "United States - New Mexico (NM)" = "New Mexico")

passerina_data_subset_filtered$stateProvince <- ifelse(passerina_data_subset_filtered$stateProvince %in% names(state_mapping), 
                                                       state_mapping[passerina_data_subset_filtered$stateProvince], 
                                                       passerina_data_subset_filtered$stateProvince)
#checking unique values again
unique_states_df <- unique(passerina_data_subset_filtered$stateProvince)

# Check unique values in the 'id' column of usa_sf
unique_states_sf <- unique(usa_sf$id)
## Warning: Unknown or uninitialised column: `id`.
# Print unique values
print(unique_states_df)
##  [1] "United States - Florida (FL)" "Texas"                       
##  [3] "Missouri"                     "South Carolina"              
##  [5] "Louisiana"                    "Georgia"                     
##  [7] "Arizona"                      "Florida"                     
##  [9] "New York"                     "Tennessee"                   
## [11] "Maryland"                     "California"                  
## [13] "Oklahoma"                     "Illinois"                    
## [15] "Kansas"                       "Vermont"                     
## [17] "Iowa"                         NA                            
## [19] "Arkansas"                     "Ohio"                        
## [21] "Mississippi"                  "North Carolina"              
## [23] "Massachusetts"                "Alabama"                     
## [25] "Delaware"                     "New Hampshire"               
## [27] "Michigan"                     "New Jersey"                  
## [29] "Virginia"                     "New Mexico"                  
## [31] "Oregon"                       "Colorado"                    
## [33] "Pennsylvania"                 "Minnesota"                   
## [35] "Connecticut"                  "Washington"                  
## [37] "Wyoming"                      "Rhode Island"                
## [39] "Utah"                         "Maine"                       
## [41] "Nevada"                       "Wisconsin"                   
## [43] "North Dakota"                 "Kentucky"                    
## [45] "Nebraska"                     "District of Columbia"        
## [47] "West Virginia"                "Unknown"                     
## [49] "South Dakota"                 "Montana"                     
## [51] "Louisiana, Captive"           "Idaho"                       
## [53] "Indiana"
print(unique_states_sf)
## NULL
#again
passerina_data_subset_filtered <- passerina_data_subset_filtered %>%
  filter(!is.na(stateProvince) & stateProvince != "Unknown")
#checking NA and unknown were removed
unique_states_df <- unique(passerina_data_subset_filtered$stateProvince)

unique_states_sf <- unique(usa_sf$id)
## Warning: Unknown or uninitialised column: `id`.
print(unique_states_df)
##  [1] "United States - Florida (FL)" "Texas"                       
##  [3] "Missouri"                     "South Carolina"              
##  [5] "Louisiana"                    "Georgia"                     
##  [7] "Arizona"                      "Florida"                     
##  [9] "New York"                     "Tennessee"                   
## [11] "Maryland"                     "California"                  
## [13] "Oklahoma"                     "Illinois"                    
## [15] "Kansas"                       "Vermont"                     
## [17] "Iowa"                         "Arkansas"                    
## [19] "Ohio"                         "Mississippi"                 
## [21] "North Carolina"               "Massachusetts"               
## [23] "Alabama"                      "Delaware"                    
## [25] "New Hampshire"                "Michigan"                    
## [27] "New Jersey"                   "Virginia"                    
## [29] "New Mexico"                   "Oregon"                      
## [31] "Colorado"                     "Pennsylvania"                
## [33] "Minnesota"                    "Connecticut"                 
## [35] "Washington"                   "Wyoming"                     
## [37] "Rhode Island"                 "Utah"                        
## [39] "Maine"                        "Nevada"                      
## [41] "Wisconsin"                    "North Dakota"                
## [43] "Kentucky"                     "Nebraska"                    
## [45] "District of Columbia"         "West Virginia"               
## [47] "South Dakota"                 "Montana"                     
## [49] "Louisiana, Captive"           "Idaho"                       
## [51] "Indiana"
print(unique_states_sf)
## NULL
#leaflet
map <- leaflet() %>%
  addTiles() %>%
  addCircleMarkers(data = passerina_data_subset_filtered, 
                   lng = ~decimalLongitude, 
                   lat = ~decimalLatitude,
                   popup = ~paste("State:", stateProvince))
## Warning in validateCoords(lng, lat, funcName): Data contains 1553 rows with
## either missing or invalid lat/lon values and will be ignored
#elevation layer
install.packages("leaflet")
## 
## The downloaded binary packages are in
##  /var/folders/90/sbk7b_0118vgxh1bg9b23g440000gn/T//RtmpxuJ1X5/downloaded_packages
install.packages("raster")
## 
## The downloaded binary packages are in
##  /var/folders/90/sbk7b_0118vgxh1bg9b23g440000gn/T//RtmpxuJ1X5/downloaded_packages
install.packages("elevatr")
## 
## The downloaded binary packages are in
##  /var/folders/90/sbk7b_0118vgxh1bg9b23g440000gn/T//RtmpxuJ1X5/downloaded_packages
library(leaflet)
library(raster)
## Loading required package: sp
## 
## Attaching package: 'raster'
## The following object is masked from 'package:dplyr':
## 
##     select
library(elevatr)
## elevatr v0.99.0 NOTE: Version 0.99.0 of 'elevatr' uses 'sf' and 'terra'.  Use 
## of the 'sp', 'raster', and underlying 'rgdal' packages by 'elevatr' is being 
## deprecated; however, get_elev_raster continues to return a RasterLayer.  This 
## will be dropped in future versions, so please plan accordingly.
#bounding box
bbox <- c(left = min(passerina_data_subset_filtered$decimalLongitude),
          bottom = min(passerina_data_subset_filtered$decimalLatitude),
          right = max(passerina_data_subset_filtered$decimalLongitude),
          top = max(passerina_data_subset_filtered$decimalLatitude))
#download elevation data for United States
elevation_data <- raster::getData("alt", country = "USA", mask = TRUE)
## Warning in raster::getData("alt", country = "USA", mask = TRUE): getData will be removed in a future version of raster
## . Please use the geodata package instead
## returning a list of RasterLayer objects
install.packages("leaflet")
## 
## The downloaded binary packages are in
##  /var/folders/90/sbk7b_0118vgxh1bg9b23g440000gn/T//RtmpxuJ1X5/downloaded_packages
install.packages("raster")
## 
## The downloaded binary packages are in
##  /var/folders/90/sbk7b_0118vgxh1bg9b23g440000gn/T//RtmpxuJ1X5/downloaded_packages
library(leaflet)
library(raster)

elevation_data1 <- raster::getData("alt", country = "USA", mask = TRUE, level = 1)
## Warning in raster::getData("alt", country = "USA", mask = TRUE, level = 1): getData will be removed in a future version of raster
## . Please use the geodata package instead
## returning a list of RasterLayer objects
elevation_data2 <- raster::getData("alt", country = "USA", mask = TRUE, level = 2)
## Warning in raster::getData("alt", country = "USA", mask = TRUE, level = 2): getData will be removed in a future version of raster
## . Please use the geodata package instead
## returning a list of RasterLayer objects
elevation_data3 <- raster::getData("alt", country = "USA", mask = TRUE, level = 3)
## Warning in raster::getData("alt", country = "USA", mask = TRUE, level = 3): getData will be removed in a future version of raster
## . Please use the geodata package instead
## returning a list of RasterLayer objects
elevation_data4 <- raster::getData("alt", country = "USA", mask = TRUE, level = 4)
## Warning in raster::getData("alt", country = "USA", mask = TRUE, level = 4): getData will be removed in a future version of raster
## . Please use the geodata package instead
## returning a list of RasterLayer objects
#merge the raster layers from raster 1
merged_raster1 <- raster::merge(elevation_data1[[1]], elevation_data2[[1]], 
                                elevation_data3[[1]], elevation_data4[[1]])
#print the merged raster layer
print(merged_raster1)
## class      : RasterLayer 
## dimensions : 3012, 6948, 20927376  (nrow, ncol, ncell)
## resolution : 0.008333333, 0.008333333  (x, y)
## extent     : -124.8, -66.9, 24.4, 49.5  (xmin, xmax, ymin, ymax)
## crs        : +proj=longlat +ellps=WGS84 +no_defs 
## source     : memory
## names      : layer 
## values     : -171, 4275  (min, max)
#download or generate the downsampled raster object
downsampled_raster <- raster::getData(name = "alt", country = "USA", mask = TRUE)
## Warning in raster::getData(name = "alt", country = "USA", mask = TRUE): getData will be removed in a future version of raster
## . Please use the geodata package instead
## returning a list of RasterLayer objects
library(leaflet)
library(raster)

#extract the first RasterLayer object
raster_object <- downsampled_raster[[1]]

#color palette
elevation_palette <- colorNumeric(
  palette = terrain.colors(100),
  domain = c(minValue(raster_object), maxValue(raster_object)),
  na.color = "transparent"
)
#raster image too big, aggregate it
library(raster)

#define the factor by which to downsize the raster
factor <- 10

#aggregate the raster
downsized_raster <- aggregate(raster_object, fact = factor, fun = mean)

#legend
leaflet() %>%
  addTiles() %>%
  addRasterImage(downsized_raster, colors = elevation_palette) %>%
  addLegend(
    pal = elevation_palette,
    values = c(minValue(raster_object), maxValue(raster_object)),
    title = "Elevation (m)",
    opacity = 0.7
  )
print(object.size(downsized_raster))
## 1692448 bytes
library(raster)

#download temperature data for the contiguous United States
temperature_raster <- raster::getData(name = "worldclim", var = "bio", res = 2.5, 
                                      lon = -125, lat = 50)
## Warning in raster::getData(name = "worldclim", var = "bio", res = 2.5, lon = -125, : getData will be removed in a future version of raster
## . Please use the geodata package instead
#extract the temperature data for the contiguous United States
temperature_raster_us <- crop(temperature_raster, extent(-125, -65, 25, 50))
#gotta make this one smaller
# Define the factor by which to downsize the raster
factor <- 2

# Aggregate the raster
aggregate_temp <- aggregate(temperature_raster_us, fact = factor, fun = mean)

# Print the size of the aggregated raster
print(object.size(aggregate_temp))
## 32847296 bytes
# Add temp layer to leaflet
newtemplayer <- aggregate_temp[[1]]

# Print the size of the raster image
print(object.size(newtemplayer))
## 1741312 bytes
#visualize the downsized raster
leaflet() %>%
  addTiles() %>%
  addRasterImage(newtemplayer, colors = "viridis")
#adding a legend to the temperature map
if (!requireNamespace("viridis", quietly = TRUE)) {
  install.packages("viridis")
}
#load viridis
library(viridis)
## Loading required package: viridisLite
#define color palette
color_palette <- viridis(100)

#temp scale legend
temperature_legend <- leaflet::colorNumeric(
  palette = color_palette,
  domain = values(newtemplayer),
  na.color = "transparent"
)
leaflet() %>%
  addTiles() %>%
  addRasterImage(newtemplayer, colors = "viridis") %>%
  addLegend(
    pal = colorNumeric("viridis", values(newtemplayer)),
    values = values(newtemplayer),
    title = "Temperature (°C)",
    opacity = 0.7
  )
#now im gonna cluster the bird sightings so it wont make R crash if it tries to plot every single point.

#filter out rows with missing or invalid lat/lon values
passerina_data_subset_filtered <- passerina_data_subset_filtered[complete.cases(passerina_data_subset_filtered$decimalLatitude, passerina_data_subset_filtered$decimalLongitude),]

#create leaflet
map <- leaflet() %>%
  addTiles()

#function to create popup content with date information
createPopupContent <- function(lat, lon, data) {
  paste("Date:", data$month, "/", data$day, "/", data$year)
}
#add clustered markers with popup including date information
map <- addMarkers(
  map = map,
  data = passerina_data_subset_filtered,
  lat = ~decimalLatitude,
  lng = ~decimalLongitude,
  clusterOptions = markerClusterOptions(),
  popup = ~createPopupContent(decimalLatitude, decimalLongitude, data = passerina_data_subset_filtered))

#print map
map
#combining the elevation leaflet and the bird sighting leaflet with a toggle function
library(leaflet)

#create a Leaflet map with elevation layer
elevation_palette <- colorNumeric(
  palette = terrain.colors(100),
  domain = values(downsized_raster),
  na.color = "transparent"
)

#filter out rows with missing or invalid lat/lon values for data points
passerina_data_subset_filtered <- passerina_data_subset_filtered[complete.cases(passerina_data_subset_filtered$decimalLatitude, passerina_data_subset_filtered$decimalLongitude),]

#create a leaflet with elevation layer
leaflet() %>%
  addTiles() %>%
  addRasterImage(downsized_raster, colors = elevation_palette) %>%
  addLegend(
    pal = elevation_palette,
    values = c(minValue(raster_object), maxValue(raster_object)),
    title = "Elevation (m)",
    opacity = 0.7
  )
## Warning in pal(c(r[1], cuts, r[2])): Some values were outside the color scale
## and will be treated as NA
#create a leaflet map with clustered data points and dates
data_points_map <- leaflet() %>%
  addTiles() %>%
  addMarkers(
    data = passerina_data_subset_filtered,
    lat = ~decimalLatitude,
    lng = ~decimalLongitude,
    clusterOptions = markerClusterOptions(),
    popup = ~createPopupContent(decimalLatitude, decimalLongitude, data = passerina_data_subset_filtered)
  )

#combine both maps and add layer control
final_map <- leaflet() %>%
  addTiles() %>%
  addLayersControl(
    baseGroups = c("Elevation"),
    overlayGroups = c("Data Points"),
    options = layersControlOptions(collapsed = FALSE)
  ) %>%
  addLegend(
    pal = elevation_palette,
    values = values(downsized_raster),
    title = "Elevation (m)",
    opacity = 0.7
  ) %>%
  addRasterImage(downsized_raster, colors = elevation_palette) %>%
  addLayersControl(
    overlayGroups = "Data Points",
    options = layersControlOptions(collapsed = FALSE)
  )

#print map
final_map
#now i want to combine the occurrence data with the temperature layer as opposed to the elevation
library(raster)
library(leaflet)

#download temperature data for the contiguous United States
temperature_raster <- raster::getData(name = "worldclim", var = "bio", res = 2.5, 
                                      lon = -125, lat = 50)
## Warning in raster::getData(name = "worldclim", var = "bio", res = 2.5, lon = -125, : getData will be removed in a future version of raster
## . Please use the geodata package instead
#extract the temperature data for the contiguous United States
temperature_raster_us <- crop(temperature_raster, extent(-125, -65, 25, 50))

#select a single layer from the RasterBrick object
temperature_layer <- temperature_raster_us[[1]]

#create a Leaflet map
map <- leaflet() %>%
  addTiles() %>%
  addRasterImage(temperature_layer, colors = "viridis")

#filter out rows with missing or invalid lat/lon values for data points
passerina_data_subset_filtered <- passerina_data_subset_filtered[complete.cases(passerina_data_subset_filtered$decimalLatitude, passerina_data_subset_filtered$decimalLongitude),]

#function to create popup content with date information
createPopupContent <- function(lat, lon, data) {
  paste("Date:", data$month, "/", data$day, "/", data$year)
}

#add clustered markers with popup including date information
map <- addMarkers(
  map = map,
  data = passerina_data_subset_filtered,
  lat = ~decimalLatitude,
  lng = ~decimalLongitude,
  clusterOptions = markerClusterOptions(),
  popup = ~createPopupContent(decimalLatitude, decimalLongitude, data = passerina_data_subset_filtered))

#print map
map
#i tried to add back in the temperature legend but now the data points just disappeared. i dont know why.
library(raster)
library(leaflet)

#download temperature data for the contiguous United States
temperature_raster <- raster::getData(name = "worldclim", var = "bio", res = 2.5, 
                                      lon = -125, lat = 50)
## Warning in raster::getData(name = "worldclim", var = "bio", res = 2.5, lon = -125, : getData will be removed in a future version of raster
## . Please use the geodata package instead
#extract the temperature data for the contiguous United States
temperature_raster_us <- crop(temperature_raster, extent(-125, -65, 25, 50))

#select a single layer from the RasterBrick object
temperature_layer <- temperature_raster_us[[1]]

#temp scale legend
temperature_legend <- leaflet::colorNumeric(
  palette = color_palette,
  domain = values(temperature_layer),
  na.color = "transparent"
)

leaflet() %>%
  addTiles() %>%
  addRasterImage(downsized_raster) %>%
  addRasterImage(temperature_layer, colors = colorNumeric(color_palette, values(temperature_layer))) %>%
  addLegend(
    pal = temperature_legend,
    values = values(temperature_layer),
    title = "Temperature (°C)",
    opacity = 0.7
  )
#create a Leaflet map
map <- leaflet() %>%
  addTiles() %>%
  addRasterImage(temperature_layer, colors = "viridis")

#filter out rows with missing or invalid lat/lon values for data points
passerina_data_subset_filtered <- passerina_data_subset_filtered[complete.cases(passerina_data_subset_filtered$decimalLatitude, passerina_data_subset_filtered$decimalLongitude),]

#function to create popup content with date information
createPopupContent <- function(lat, lon, data) {
  paste("Date:", data$month, "/", data$day, "/", data$year)
}

#add clustered markers with popup including date information
map <- addMarkers(
  map = map,
  data = passerina_data_subset_filtered,
  lat = ~decimalLatitude,
  lng = ~decimalLongitude,
  clusterOptions = markerClusterOptions(),
  popup = ~createPopupContent(decimalLatitude, decimalLongitude, data = passerina_data_subset_filtered))
#now i want to label the three most populous cities near the largest set of observations for my species (passerina ciris)

#using the maps package
install.packages("maps")
## 
## The downloaded binary packages are in
##  /var/folders/90/sbk7b_0118vgxh1bg9b23g440000gn/T//RtmpxuJ1X5/downloaded_packages
library(maps)
## 
## Attaching package: 'maps'
## The following object is masked from 'package:viridis':
## 
##     unemp
## The following object is masked from 'package:purrr':
## 
##     map
#find the coordinates with the most occurrences
most_common_coords <- passerina_data_subset_filtered %>%
  group_by(decimalLatitude, decimalLongitude) %>%
  summarise(n = n(), .groups = "drop_last") %>%
  arrange(desc(n)) %>%
  slice(1)

# Extract latitude and longitude values
lat <- most_common_coords$decimalLatitude
lon <- most_common_coords$decimalLongitude
#find nearest city using maps database
nearest_city <- maps::map.where(database = "world", x = lon, y = lat)

#get the city name
city_name <- ifelse(is.na(nearest_city), NA, sub("^[^:]*:", "", nearest_city))
#it has a bunch of NA and non-city values in the city_name vector so im removing them

# Remove values that are NA, "USA", or "Mexico"
city_name_filtered <- city_name[!is.na(city_name) & !grepl("USA|Mexico", city_name)]

# Print the filtered city names
print(city_name_filtered)
##   [1] "Guatemala"                       "Florida:Key West"               
##   [3] "Florida:Key West"                "Florida:Key West"               
##   [5] "Florida:Key West"                "Florida:Key West"               
##   [7] "Florida:Key West"                "Florida:Key West"               
##   [9] "Florida:Key West"                "Florida:Key West"               
##  [11] "Florida:Key West"                "Florida:Key West"               
##  [13] "Florida:Sugarloaf Key"           "Florida:Sugarloaf Key"          
##  [15] "Florida:Big Pine Key"            "Florida:Big Pine Key"           
##  [17] "Florida:Big Pine Key"            "Florida:Big Pine Key"           
##  [19] "Florida:Marathon Key"            "Florida:Marathon Key"           
##  [21] "Florida:Marathon Key"            "Florida:Big Pine Key"           
##  [23] "Florida:Marathon Key"            "Florida:Long Key"               
##  [25] "Florida:Long Key"                "13"                             
##  [27] "13"                              "Florida:Key Largo"              
##  [29] "Florida:Key Largo"               "Florida:Key Largo"              
##  [31] "Florida:Key Largo"               "Florida:Key Largo"              
##  [33] "Florida:Key Largo"               "Florida:Key Largo"              
##  [35] "Florida:Key Largo"               "Florida:Key Largo"              
##  [37] "Florida:Key Largo"               "Florida:Key Largo"              
##  [39] "Florida:Key Largo"               "Florida:Key Largo"              
##  [41] "Florida:Key Largo"               "Texas:Padre Island"             
##  [43] "Texas:Padre Island"              "Texas:Padre Island"             
##  [45] "Texas:Padre Island"              "Florida:Sanibel Island"         
##  [47] "Florida:Sanibel Island"          "Florida:Sanibel Island"         
##  [49] "Florida:Sanibel Island"          "Florida:Sanibel Island"         
##  [51] "Florida:Sanibel Island"          "Florida:Sanibel Island"         
##  [53] "Florida:Sanibel Island"          "Florida:Sanibel Island"         
##  [55] "Florida:Sanibel Island"          "Texas:Padre Island"             
##  [57] "Florida:Hutchinson Island"       "Florida:Hutchinson Island"      
##  [59] "Florida:Hutchinson Island"       "Florida:Hutchinson Island"      
##  [61] "Texas:Matagorda Island"          "Florida:Hutchinson Island"      
##  [63] "Texas:Matagorda Island"          "Texas:Matagorda Island"         
##  [65] "Texas:Matagorda Island"          "Texas:Matagorda Island"         
##  [67] "Texas:Matagorda Island"          "Texas:Matagorda Island"         
##  [69] "Texas:Matagorda Island"          "Texas:Matagorda Island"         
##  [71] "Texas:Matagorda Island"          "Texas:Matagorda Island"         
##  [73] "Texas:Matagorda Island"          "Texas:Matagorda Island"         
##  [75] "Texas:Matagorda Island"          "Texas:Matagorda Island"         
##  [77] "Texas:Matagorda Island"          "Florida:Hutchinson Island"      
##  [79] "Texas:Matagorda Island"          "Texas:Matagorda Island"         
##  [81] "Texas:Matagorda Island"          "Texas:Matagorda Island"         
##  [83] "Texas:Matagorda Island"          "Texas:Matagorda Island"         
##  [85] "Texas:Matagorda Island"          "Florida:Hutchinson Island"      
##  [87] "Texas:Matagorda Island"          "Texas:Matagorda Island"         
##  [89] "Texas:Matagorda Island"          "Texas:Matagorda Island"         
##  [91] "Texas:Matagorda Island"          "Texas:Matagorda Island"         
##  [93] "Texas:Matagorda Island"          "Texas:Matagorda Island"         
##  [95] "Texas:Matagorda Island"          "Texas:Matagorda Island"         
##  [97] "Texas:Matagorda Island"          "Texas:Matagorda Island"         
##  [99] "Texas:Matagorda Island"          "Texas:Matagorda Island"         
## [101] "Texas:Matagorda Island"          "Texas:Matagorda Island"         
## [103] "Texas:Matagorda Island"          "Texas:Matagorda Island"         
## [105] "Texas:Matagorda Island"          "Texas:Matagorda Island"         
## [107] "Texas:Matagorda Island"          "Texas:Matagorda Island"         
## [109] "Texas:Matagorda Island"          "Texas:Matagorda Island"         
## [111] "Texas:Matagorda Island"          "Texas:Matagorda Island"         
## [113] "Texas:Matagorda Island"          "Texas:Matagorda Island"         
## [115] "Texas:Matagorda Island"          "Texas:Matagorda Island"         
## [117] "Texas:Matagorda Island"          "Texas:Matagorda Island"         
## [119] "Texas:Matagorda Island"          "Texas:Matagorda Island"         
## [121] "Texas:Matagorda Island"          "Texas:Matagorda Island"         
## [123] "Texas:Matagorda Island"          "Texas:Matagorda Island"         
## [125] "Texas:Matagorda Island"          "Texas:Matagorda Island"         
## [127] "Texas:Matagorda Island"          "Texas:Matagorda Island"         
## [129] "Texas:Matagorda Island"          "Texas:Matagorda Island"         
## [131] "Texas:Matagorda Island"          "Texas:Matagorda Island"         
## [133] "Texas:Matagorda Island"          "Texas:Matagorda Island"         
## [135] "Texas:Matagorda Island"          "Texas:Matagorda Island"         
## [137] "Texas:Matagorda Island"          "Texas:Matagorda Island"         
## [139] "Texas:Matagorda Island"          "Texas:Matagorda Island"         
## [141] "Texas:Matagorda Island"          "Florida:Hutchinson Island"      
## [143] "Florida:Hutchinson Island"       "Florida:Hutchinson Island"      
## [145] "Texas:Matagorda Island"          "Texas:Matagorda Island"         
## [147] "Texas:Matagorda Island"          "Texas:Matagorda Island"         
## [149] "Texas:Matagorda Island"          "Texas:Matagorda Island"         
## [151] "Texas:Matagorda Island"          "Texas:Matagorda Island"         
## [153] "Texas:Matagorda Island"          "Texas:Matagorda Island"         
## [155] "Texas:Matagorda Island"          "Texas:Matagorda Island"         
## [157] "Texas:Matagorda Island"          "Texas:Matagorda Island"         
## [159] "Texas:Matagorda Island"          "Texas:Matagorda Island"         
## [161] "Florida:Hutchinson Island"       "Texas:Matagorda Island"         
## [163] "Texas:Matagorda Island"          "Florida:Hutchinson Island"      
## [165] "Texas:Matagorda Island"          "Texas:Matagorda Island"         
## [167] "Texas:Matagorda Island"          "Texas:Matagorda Island"         
## [169] "Texas:Matagorda Island"          "Texas:Matagorda Island"         
## [171] "Florida:Hutchinson Island"       "Texas:San Jose Island"          
## [173] "Texas:Matagorda"                 "Texas:Matagorda"                
## [175] "Texas:Galveston Island"          "Texas:Galveston Island"         
## [177] "Texas:Galveston Island"          "Texas:Galveston Island"         
## [179] "Texas:Galveston Island"          "Texas:Galveston Island"         
## [181] "Texas:Galveston Island"          "Texas:Galveston Island"         
## [183] "Texas:Galveston Island"          "Texas:Galveston Island"         
## [185] "Texas:Galveston Island"          "Texas:Galveston Island"         
## [187] "Texas:Galveston Island"          "Texas:Galveston Island"         
## [189] "Texas:Galveston Island"          "Texas:Galveston Island"         
## [191] "Texas:Galveston Island"          "Texas:Galveston Island"         
## [193] "Texas:Galveston Island"          "Texas:Galveston Island"         
## [195] "Texas:Galveston Island"          "Texas:Galveston Island"         
## [197] "Texas:Galveston Island"          "Texas:Galveston Island"         
## [199] "Texas:Galveston Island"          "Texas:Galveston Island"         
## [201] "Texas:Galveston Island"          "Texas:Galveston Island"         
## [203] "Texas:Galveston Island"          "Texas:Galveston Island"         
## [205] "Texas:Galveston Island"          "Texas:Galveston Island"         
## [207] "Texas:Galveston Island"          "Texas:Galveston Island"         
## [209] "Texas:Galveston Island"          "Texas:Galveston Island"         
## [211] "Texas:Galveston Island"          "Texas:Galveston Island"         
## [213] "Texas:Galveston Island"          "Texas:Galveston Island"         
## [215] "Texas:Galveston Island"          "Texas:Galveston Island"         
## [217] "Texas:Galveston Island"          "Texas:Galveston Island"         
## [219] "Texas:Galveston Island"          "Texas:Galveston Island"         
## [221] "Texas:Galveston Island"          "Texas:Galveston Island"         
## [223] "Texas:Galveston Island"          "Texas:Galveston Island"         
## [225] "Texas:Galveston Island"          "Texas:Galveston Island"         
## [227] "Texas:Galveston Island"          "Texas:Galveston Island"         
## [229] "Texas:Galveston Island"          "Texas:Galveston Island"         
## [231] "Texas:Galveston Island"          "Texas:Galveston Island"         
## [233] "Texas:Galveston Island"          "Texas:Galveston Island"         
## [235] "Texas:Galveston Island"          "Texas:Galveston Island"         
## [237] "Texas:Galveston Island"          "Texas:Galveston Island"         
## [239] "Texas:Galveston Island"          "Texas:Galveston Island"         
## [241] "Texas:Galveston Island"          "Texas:Galveston Island"         
## [243] "Texas:Galveston Island"          "Texas:Galveston Island"         
## [245] "Texas:Galveston Island"          "Texas:Galveston Island"         
## [247] "Texas:Galveston Island"          "Texas:Galveston Island"         
## [249] "Texas:Galveston Island"          "Texas:Galveston Island"         
## [251] "Texas:Galveston Island"          "Texas:Galveston Island"         
## [253] "Texas:Galveston Island"          "Texas:Galveston Island"         
## [255] "Texas:Galveston Island"          "Texas:Galveston Island"         
## [257] "Texas:Galveston Island"          "Texas:Galveston Island"         
## [259] "Texas:Galveston Island"          "Texas:Galveston Island"         
## [261] "Texas:Galveston Island"          "Texas:Galveston Island"         
## [263] "Texas:Galveston Island"          "Texas:Galveston Island"         
## [265] "Texas:Galveston Island"          "Texas:Galveston Island"         
## [267] "Texas:Galveston Island"          "Texas:Galveston Island"         
## [269] "Texas:Galveston Island"          "Texas:Galveston Island"         
## [271] "Texas:Galveston Island"          "Texas:Galveston Island"         
## [273] "Texas:Galveston Island"          "Texas:Galveston Island"         
## [275] "Texas:Galveston Island"          "Texas:Galveston Island"         
## [277] "Texas:Galveston Island"          "Texas:Galveston Island"         
## [279] "Texas:Galveston Island"          "Texas:Galveston Island"         
## [281] "Texas:Galveston Island"          "Texas:Galveston Island"         
## [283] "Texas:Galveston Island"          "Texas:Galveston Island"         
## [285] "Texas:Galveston Island"          "Texas:Galveston Island"         
## [287] "Texas:Galveston Island"          "Texas:Galveston Island"         
## [289] "Texas:Galveston Island"          "Texas:Galveston Island"         
## [291] "Texas:Galveston Island"          "Texas:Galveston Island"         
## [293] "Texas:Galveston Island"          "Texas:Galveston Island"         
## [295] "Texas:Galveston Island"          "Texas:Galveston Island"         
## [297] "Texas:Galveston Island"          "Texas:Galveston Island"         
## [299] "Texas:Galveston Island"          "Texas:Galveston Island"         
## [301] "Texas:Galveston Island"          "Texas:Galveston Island"         
## [303] "Texas:Galveston Island"          "Texas:Galveston Island"         
## [305] "Texas:Galveston Island"          "Texas:Galveston Island"         
## [307] "Texas:Galveston Island"          "Texas:Galveston Island"         
## [309] "Texas:Galveston Island"          "Texas:Galveston Island"         
## [311] "Texas:Galveston Island"          "Louisiana:Marsh Island"         
## [313] "Alabama:Dauphin Island"          "Mississippi:Horn Island"        
## [315] "Alabama:Dauphin Island"          "Alabama:Dauphin Island"         
## [317] "Alabama:Dauphin Island"          "Alabama:Dauphin Island"         
## [319] "Alabama:Dauphin Island"          "Alabama:Dauphin Island"         
## [321] "Alabama:Dauphin Island"          "Alabama:Dauphin Island"         
## [323] "Alabama:Dauphin Island"          "Alabama:Dauphin Island"         
## [325] "Alabama:Dauphin Island"          "Alabama:Dauphin Island"         
## [327] "Alabama:Dauphin Island"          "Alabama:Dauphin Island"         
## [329] "Alabama:Dauphin Island"          "Alabama:Dauphin Island"         
## [331] "Alabama:Dauphin Island"          "Alabama:Dauphin Island"         
## [333] "Alabama:Dauphin Island"          "Alabama:Dauphin Island"         
## [335] "Alabama:Dauphin Island"          "Alabama:Dauphin Island"         
## [337] "Alabama:Dauphin Island"          "Alabama:Dauphin Island"         
## [339] "Alabama:Dauphin Island"          "Alabama:Dauphin Island"         
## [341] "Alabama:Dauphin Island"          "Alabama:Dauphin Island"         
## [343] "Alabama:Dauphin Island"          "Alabama:Dauphin Island"         
## [345] "Alabama:Dauphin Island"          "Alabama:Dauphin Island"         
## [347] "Alabama:Dauphin Island"          "Alabama:Dauphin Island"         
## [349] "Alabama:Dauphin Island"          "Alabama:Dauphin Island"         
## [351] "Alabama:Dauphin Island"          "Alabama:Dauphin Island"         
## [353] "Alabama:Dauphin Island"          "Alabama:Dauphin Island"         
## [355] "Alabama:Dauphin Island"          "Alabama:Dauphin Island"         
## [357] "Alabama:Dauphin Island"          "Alabama:Dauphin Island"         
## [359] "Alabama:Dauphin Island"          "Alabama:Dauphin Island"         
## [361] "Alabama:Dauphin Island"          "Alabama:Dauphin Island"         
## [363] "Alabama:Dauphin Island"          "Alabama:Dauphin Island"         
## [365] "Alabama:Dauphin Island"          "Alabama:Dauphin Island"         
## [367] "Alabama:Dauphin Island"          "Alabama:Dauphin Island"         
## [369] "Alabama:Dauphin Island"          "Alabama:Dauphin Island"         
## [371] "Alabama:Dauphin Island"          "Alabama:Dauphin Island"         
## [373] "Alabama:Dauphin Island"          "Alabama:Dauphin Island"         
## [375] "Alabama:Dauphin Island"          "Alabama:Dauphin Island"         
## [377] "Alabama:Dauphin Island"          "Alabama:Dauphin Island"         
## [379] "Alabama:Dauphin Island"          "Alabama:Dauphin Island"         
## [381] "Alabama:Dauphin Island"          "Alabama:Dauphin Island"         
## [383] "Alabama:Dauphin Island"          "Alabama:Dauphin Island"         
## [385] "Alabama:Dauphin Island"          "Alabama:Dauphin Island"         
## [387] "Alabama:Dauphin Island"          "Alabama:Dauphin Island"         
## [389] "Alabama:Dauphin Island"          "Alabama:Dauphin Island"         
## [391] "Alabama:Dauphin Island"          "Alabama:Dauphin Island"         
## [393] "Alabama:Dauphin Island"          "Alabama:Dauphin Island"         
## [395] "Alabama:Dauphin Island"          "Alabama:Dauphin Island"         
## [397] "Alabama:Dauphin Island"          "Alabama:Dauphin Island"         
## [399] "30"                              "30"                             
## [401] "30"                              "30"                             
## [403] "30"                              "30"                             
## [405] "30"                              "30"                             
## [407] "30"                              "30"                             
## [409] "30"                              "30"                             
## [411] "30"                              "30"                             
## [413] "30"                              "30"                             
## [415] "30"                              "30"                             
## [417] "30"                              "30"                             
## [419] "30"                              "30"                             
## [421] "30"                              "30"                             
## [423] "California:San Clemente Island"  "California:San Clemente Island" 
## [425] "California:San Clemente Island"  "California:San Clemente Island" 
## [427] "California:San Clemente Island"  "California:San Clemente Island" 
## [429] "California:San Clemente Island"  "California:San Clemente Island" 
## [431] "California:San Clemente Island"  "California:Santa Rosa Island"   
## [433] "California:Santa Cruz Island"    "California:Santa Cruz Island"   
## [435] "37"                              "37"                             
## [437] "37"                              "37"                             
## [439] "37"                              "Core Banks"                     
## [441] "Pea Island"                      "Pea Island"                     
## [443] "Pea Island"                      "Pea Island"                     
## [445] "Pea Island"                      "Pea Island"                     
## [447] "Pea Island"                      "Pea Island"                     
## [449] "Pea Island"                      "Pea Island"                     
## [451] "Pea Island"                      "Pea Island"                     
## [453] "Pea Island"                      "41"                             
## [455] "41"                              "41"                             
## [457] "41"                              "41"                             
## [459] "41"                              "41"                             
## [461] "41"                              "41"                             
## [463] "41"                              "41"                             
## [465] "41"                              "41"                             
## [467] "41"                              "41"                             
## [469] "41"                              "41"                             
## [471] "41"                              "41"                             
## [473] "41"                              "41"                             
## [475] "41"                              "Long Beach Island"              
## [477] "44"                              "44"                             
## [479] "New York:Long Island"            "New York:Long Island"           
## [481] "New York:Long Island"            "New York:Long Island"           
## [483] "New York:Long Island"            "New York:Long Island"           
## [485] "New York:Long Island"            "New York:Long Island"           
## [487] "New York:Long Island"            "New York:Long Island"           
## [489] "New York:Long Island"            "New York:Long Island"           
## [491] "New York:Long Island"            "New York:Long Island"           
## [493] "New York:Long Island"            "New York:Long Island"           
## [495] "New York:Long Island"            "New York:Long Island"           
## [497] "New York:Long Island"            "New York:Long Island"           
## [499] "New York:Long Island"            "New York:Long Island"           
## [501] "New York:Long Island"            "New York:Long Island"           
## [503] "New York:Long Island"            "New York:Long Island"           
## [505] "New York:Long Island"            "New York:Long Island"           
## [507] "New York:Long Island"            "New York:Long Island"           
## [509] "New York:Long Island"            "New York:Long Island"           
## [511] "New York:Long Island"            "New York:Long Island"           
## [513] "New York:Long Island"            "New York:Long Island"           
## [515] "New York:Long Island"            "Massachusettes:Nantucket Island"
## [517] "Massachusettes:Nantucket Island" "Massachusettes:Nantucket Island"
## [519] "Massachusettes:Nantucket Island" "Massachusettes:Nantucket Island"
## [521] "Massachusettes:Nantucket Island" "Massachusettes:Nantucket Island"
## [523] "Massachusettes:Nantucket Island" "Massachusettes:Nantucket Island"
## [525] "Massachusettes:Nantucket Island" "Massachusettes:Nantucket Island"
## [527] "Massachusettes:Nantucket Island" "Massachusettes:Nantucket Island"
## [529] "Massachusettes:Nantucket Island" "Massachusettes:Nantucket Island"
## [531] "Massachusettes:Nantucket Island" "49"                             
## [533] "49"                              "49"                             
## [535] "49"                              "49"                             
## [537] "49"                              "49"                             
## [539] "49"                              "49"                             
## [541] "49"                              "49"                             
## [543] "49"                              "49"                             
## [545] "49"                              "48"                             
## [547] "49"                              "49"                             
## [549] "48"                              "49"                             
## [551] "49"                              "49"                             
## [553] "49"                              "49"                             
## [555] "49"                              "49"                             
## [557] "49"                              "48"                             
## [559] "48"                              "48"                             
## [561] "48"                              "48"                             
## [563] "48"                              "48"                             
## [565] "48"                              "48"                             
## [567] "48"                              "48"                             
## [569] "48"                              "48"                             
## [571] "Maine:Mount Desert Island"       "Maine:Mount Desert Island"      
## [573] "Maine:Mount Desert Island"       "Maine:Mount Desert Island"      
## [575] "Maine:Mount Desert Island"       "Maine:Mount Desert Island"      
## [577] "Maine:Mount Desert Island"
#finding the number of unique names in the vector list to see the city names
# Find unique city names
unique_cities <- unique(city_name_filtered)

# Count the number of unique city names
num_unique_cities <- length(unique_cities)

# Print the number of unique cities
cat("Number of unique cities:", num_unique_cities, "\n")
## Number of unique cities: 33
# Print the unique city names
cat("Unique cities:\n")
## Unique cities:
cat(unique_cities, sep = "\n")
## Guatemala
## Florida:Key West
## Florida:Sugarloaf Key
## Florida:Big Pine Key
## Florida:Marathon Key
## Florida:Long Key
## 13
## Florida:Key Largo
## Texas:Padre Island
## Florida:Sanibel Island
## Florida:Hutchinson Island
## Texas:Matagorda Island
## Texas:San Jose Island
## Texas:Matagorda
## Texas:Galveston Island
## Louisiana:Marsh Island
## Alabama:Dauphin Island
## Mississippi:Horn Island
## 30
## California:San Clemente Island
## California:Santa Rosa Island
## California:Santa Cruz Island
## 37
## Core Banks
## Pea Island
## 41
## Long Beach Island
## 44
## New York:Long Island
## Massachusettes:Nantucket Island
## 49
## 48
## Maine:Mount Desert Island
#now i wanna see which three of those unique cities is the most populated. i tried doing this using the geonames api but it wouldnt work. I'm just gonna do it manually.

#the three cities with the highest sightings of painted buntings are (1) South Padre Island, TX, (2) Galveston, TX, and (3), New Orleans, LA

#the cities that are the most populated near the largest set of observations for painted buntings are (1) Brownsville, TX (closest to South Padre Island, TX), (2) Houston, TX (closest to Galveston, TX), and (3) New Orleans, LA (it is the most populated, with Baton Rouge following New Orleans)
#i am going to add labels to those three cities on my leaflet from earlier:
library(leaflet)

# Create Leaflet map
map <- leaflet() %>%
  addTiles()

# Function to create popup content with date information
createPopupContent <- function(lat, lon, data) {
  paste("Date:", data$month, "/", data$day, "/", data$year)
}

# Add clustered markers with popup including date information
map <- addMarkers(
  map = map,
  data = passerina_data_subset_filtered,
  lat = ~decimalLatitude,
  lng = ~decimalLongitude,
  clusterOptions = markerClusterOptions(),
  popup = ~createPopupContent(decimalLatitude, decimalLongitude, data = passerina_data_subset_filtered))

# Add labels for cities
cities <- data.frame(
  city = c("Brownsville, TX", "Houston, TX", "New Orleans, LA"),
  lat = c(25.9018, 29.7604, 29.9511),
  lon = c(-97.4975, -95.3698, -90.0715)
)

map <- addLabelOnlyMarkers(
  map = map,
  data = cities,
  lat = ~lat,
  lng = ~lon,
  label = ~city,
  labelOptions = labelOptions(noHide = TRUE)
)

# Print map
map