Last active
January 4, 2023 00:51
-
-
Save DanielRuskin1/065b19b9c1bb99d618ceb2222d7e8fad to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
library(ggplot2) | |
library(maps) | |
library(tidyverse) | |
library(ggmap) | |
### Read and clean data | |
# Load CSV | |
birdingData <- read_csv("/Users/danielruskin/Downloads/DNSI Transects 2018-2022 (2).csv") | |
# Cleanup lat/long: remove degree symbols; convert to numbers | |
birdingData$Latitude <- as.numeric( | |
gsub('˚','',birdingData$Latitude) | |
) | |
birdingData$Longitude <- as.numeric( | |
gsub('˚','',birdingData$Longitude) | |
) | |
# Fix up some weird data | |
birdingData$Longitude[134] = -70.410519 | |
# Rename field for convenience later on | |
# (it's annoying to deal with spaces in field names, you have to use quotes) | |
birdingData <- rename( | |
birdingData, | |
"perVeg" = "Percent Vegetation (avg)", | |
"transectID" = "Transect ID" | |
) | |
# Add new Year column | |
birdingData$Year <- rep(0, nrow(birdingData)) | |
for(rowNum in 1:nrow(birdingData)) { | |
row <- birdingData[rowNum,] | |
date <- str_split_1(row$Date, '/') | |
if(length(date) == 1) { | |
birdingData$Year[rowNum] = as.numeric(date[1]) | |
} else { | |
birdingData$Year[rowNum] = as.numeric(date[3]) | |
} | |
} | |
# Get differentials for each row | |
birdingData$perVegDiff <- rep(0, nrow(birdingData)) | |
for(rowNum in 1:nrow(birdingData)) { | |
currRow <- birdingData[rowNum,] | |
prevRows <- filter( | |
birdingData, | |
Year < currRow$Year, | |
transectID == currRow$transectID | |
) | |
if(nrow(prevRows) >= 1) { | |
if(nrow(prevRows) > 1) { | |
prevRows <- prevRows[order(prevRows$Year, decreasing = TRUE),] | |
prevRows <- prevRows[1,] | |
} | |
birdingData$perVegDiff[rowNum] = | |
currRow$perVeg - | |
prevRows$perVeg | |
} | |
} | |
### Draw Map | |
# Fetch Map | |
localMap <- c( | |
left = min(birdingData$Longitude) - 0.02, | |
bottom = min(birdingData$Latitude) - 0.02, | |
right = max(birdingData$Longitude) + 0.02, | |
top = max(birdingData$Latitude) + 0.02 | |
) | |
localMap <- get_stamenmap(localMap, zoom = 14, maptype = "terrain") | |
# Draw map, plus plotted points | |
ggmap(localMap) + | |
geom_point( | |
aes(x = Longitude,y = Latitude, colour = 'red', alpha = 1), | |
birdingData | |
) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment