Skip to content

Instantly share code, notes, and snippets.

@jshannon75
Created November 8, 2021 20:31
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save jshannon75/83cab4378bfe6cf20472d14303f3cac6 to your computer and use it in GitHub Desktop.
Save jshannon75/83cab4378bfe6cf20472d14303f3cac6 to your computer and use it in GitHub Desktop.
library(tidyverse)
library(extrafont)
library(sf)
ebird<-read_csv("ebird_GA_Sep2019.csv") #Individual observation data
species<-ebird %>%
distinct(`COMMON NAME`)
species_blue<-species %>%
filter(str_detect(tolower(`COMMON NAME`),"blue"))
common_blue<-ebird %>%
filter(`COMMON NAME` %in% species_blue$`COMMON NAME`) %>%
count(`COMMON NAME`) %>%
filter(n>15000)
ebird_map<-ebird %>%
filter(`COMMON NAME` %in% common_blue$`COMMON NAME`) %>%
st_as_sf(coords=c("LONGITUDE","LATITUDE"),crs=4326,remove=FALSE)
#Binned map
ga<-st_read("data/US_county_2012_Georgia.shp") %>% #County shapefile
distinct() %>%
st_transform(2240) %>%
summarise()
library(tmap)
tm_shape(ga) + tm_polygons()
hex<-st_make_grid(ga,cellsize=30000,square=FALSE) %>%
st_intersection(ga) %>%
st_sf() %>%
mutate(hexrow=row_number())
tm_shape(hex) + tm_polygons()
bluehex<-ebird_map %>%
st_transform(2240) %>%
st_join(hex,join=st_within) %>%
rename(Bird=`COMMON NAME`) %>%
count(Bird,hexrow,name="count")
hex_count<-hex %>%
left_join(bluehex %>%
st_set_geometry(NULL) %>%
filter(is.na(Bird)==FALSE)) %>%
group_by(Bird) %>%
mutate(total=sum(count)) %>%
ungroup() %>%
mutate(title=paste(Bird," (",
formatC(total, format="d", big.mark=","),
")",sep="")) %>%
filter(is.na(Bird)==FALSE) %>%
arrange(-total)
bluemap<-tm_shape(ga) +
tm_polygons(fill="grey") +
tm_shape(hex_count) +
tm_polygons("count",style="quantile",palette="Blues",
border.alpha=0.1) +
tm_facets(by="title",free.scales.fill=TRUE,free.coords=FALSE,
nrow=2)+
tm_legend(show=FALSE)+
tm_layout(main.title = "Where to see blue birds in Georgia, based on eBird", main.title.position = "left",
attr.outside=TRUE,panel.label.size = 1.1)+
tm_credits("Data: eBird | Tool: R (sf, tmap) | Map by @jerry_shannon",
position = c("left", "BOTTOM"),size=1)
bluemap
tmap_save(bluemap,"bluebirds.pdf",width=11,height=6)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment