Skip to content

Instantly share code, notes, and snippets.

@sdtaylor
Last active December 2, 2020 14:33
Show Gist options
  • Save sdtaylor/e267fa68da7732ae2c2aa39f69181a6e to your computer and use it in GitHub Desktop.
Save sdtaylor/e267fa68da7732ae2c2aa39f69181a6e to your computer and use it in GitHub Desktop.
North Florida Winter's
library(tidyverse)
library(cowplot)
#########################
# Get data
zipped_files = c("1997_daily.csv.zip", "1998_daily.csv.zip", "1999_daily.csv.zip", "2000_daily.csv.zip", "2001_daily.csv.zip", "2002_daily.csv.zip",
"2003_daily.csv.zip", "2004_daily.csv.zip", "2005_daily.csv.zip", "2006_daily.csv.zip", "2007_daily.csv.zip", "2008_daily.csv.zip",
"2009_daily.csv.zip", "2010_daily.csv.zip", "2011_daily.csv.zip", "2012_daily.csv.zip", "2013_daily.zip", "2014_daily.zip",
"2015_daily.zip", "2016_daily.zip", "2017_daily.zip", "2018_daily.zip","2019_daily.zip","2020_daily.zip")
data_folder = 'fawn_data/'
dir.create(data_folder)
map(zipped_files, ~download.file(paste0("https://fawn.ifas.ufl.edu/data/fawnpub/daily_summaries/",.x), paste0(data_folder,.x)))
map(zipped_files, ~unzip(paste0(data_folder,.x), exdir = data_folder))
#########################
# Compile to data frame
# a function to read and get desired columns. for use inside map_df
ingest = function(x){
read_csv(x) %>%
select(StationID, date, min_temp_air_2m_C)
}
# list of files which exclude downloaded zipped files
csv_files = list.files(data_folder, full.names = T, recursive = T)
csv_files = csv_files[!grepl('*zip',csv_files)]
# Read all the csv's and subset to the Alachua station (id 260)
all_data = map_df(csv_files, ingest) %>%
filter(StationID==260)
############################
# Deal with winter being across 2 years
all_data = all_data %>%
mutate(doy = lubridate::yday(date), year = lubridate::year(date)) %>%
mutate(winter_year = ifelse(doy>180, year+1, year)) %>%
mutate(winter_year_name = paste0(winter_year-1,'/',winter_year))
# The number of days each year that the minimum temperature is below freezing
num_freezing_days = all_data %>%
group_by(winter_year_name) %>%
summarise(n_days_below_freezing = sum(min_temp_air_2m_C<0, na.rm=T),
total_days_with_data = sum(!is.na(min_temp_air_2m_C))) %>%
ungroup()
# 2020/2021 winter not complete yet
num_freezing_days = num_freezing_days %>%
filter(winter_year_name != '2020/2021')
# An index since the x axis cannot be a catagorical variable
num_freezing_days$winter_index = 1:nrow(num_freezing_days)
base_figure = ggplot(num_freezing_days, aes(x=winter_index, y=n_days_below_freezing)) +
geom_line(size=1) +
geom_point(size=3) +
scale_x_continuous(breaks=num_freezing_days$winter_index, labels=num_freezing_days$winter_year_name) +
geom_hline(yintercept=mean(num_freezing_days$n_days_below_freezing), linetype=2) +
labs(y='Number of days with \n minimum temperature below freezing',
x='Winter') +
annotate('text', y=19, x=18.5, label='Long term average of 20 days every winter', size=3.5) +
theme_bw() +
theme(plot.caption = element_text(hjust = 0.5),
axis.text = element_text(size=12),
axis.text.x = element_text(angle=45, hjust=1),
axis.title = element_text(size=12))
box_text = 'Number of days below freezing (0°C/32°F) at the\nUF-IFAS FAWN Station 260 in Alachua County'
ggdraw(base_figure) +
geom_rect(data=data.frame(xmin=0.625,ymin=0.725), aes(xmin=xmin,ymin=ymin, xmax=xmin+0.35,ymax=ymin+0.15),alpha=0.9, fill='white', color='black') +
draw_text(box_text, x=0.63, y=0.8, size=12, hjust = 0)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment