Load libraries
#Clear work environment
rm(list = ls())
ls()
## character(0)
# libraries
library(dplyr)
library(tidyr)
library(lubridate)
library(ggplot2)
format(Sys.Date(), "%d %b %Y") #sets the format of your dates
## [1] "30 Jun 2020"
Import datasets
# raw data set that imports dates in date format
winter.dat<-read.csv("E:/01_TaigaProjects/2020_05_ADFGWinterBats/Stata/Data/winter_files_Blejwas.csv", header = TRUE, as.is=TRUE, stringsAsFactors = FALSE) %>%
mutate(night=as.Date(night,"%m/%d/%Y"))
# modified dataset to create new variables for analysis - including scalers over groups
batactivity<-read.csv("E:/01_TaigaProjects/2020_05_ADFGWinterBats/Stata/Data/winter_files_Blejwas.csv", header = TRUE, as.is=TRUE, stringsAsFactors = FALSE) %>%
select(area, site, habitat, winter, night, date, time, LANO, MYSP, BAT, mday) %>%
mutate(jday=mday) %>%
mutate(night=as.Date(night,"%m/%d/%Y")) %>%
mutate(date=as.Date(date,"%m/%d/%Y")) %>%
mutate(year=year(night)) %>%
mutate(month=month(night)) %>%
mutate(month12 = ifelse(month<4, month+12, month)) %>%
# collapse total calls by site and night
group_by(area, habitat, site, month, jday, night) %>%
summarize(
lano = sum(LANO),
mysp = sum(MYSP),
bat = sum(BAT),
) %>%
ungroup(area, habitat, site, month, jday, night) %>%
# add tally column to count number of julian recordings per site (detectors over years per night)
group_by(site, jday) %>%
mutate(DTS_site=(count = n())) %>%
ungroup(site, jday) %>%
# add tally column to count number of julian recordings per habitat (detectors per habitat per year per night)
group_by(habitat, jday) %>%
mutate (DTS_habitat=(count = n())) %>%
ungroup(habitat, jday)
Create summary dataset for INDIVIDUAL SITES across years - Summarizes average number of bat passes per night per site across years
Totalbats_site<-batactivity %>%
group_by(area, habitat, month, jday, site) %>%
summarize(
DTS = min(DTS_site),
lanos = sum(lano),
mysps = sum(mysp),
bats = sum(bat),
) %>%
mutate(lanop = lanos/DTS) %>%
mutate(myspp = mysps/DTS) %>%
mutate(batp = bats/DTS) %>%
# Create date variable for plotting (makes everything 2013-2014 to account for winter season)
mutate(jdate = ifelse(month<4, (as.Date(paste(jday, "2014", sep="/") , format = "%m/%d/%Y")), (as.Date(paste(jday, "2013", sep="/") , format = "%m/%d/%Y")))) %>%
mutate(jdate2= as.Date(jdate, origin = "1970-01-01"))
#View(Totalbats_site) #remove the hashtag before 'View' and this code will let you see the entire new dataset 'Totalbats_site'
Create summary dataset for HABITAT TYPES across years - Summarizes average bat passes per night per habitat type across the number of sites deployed at one time in one habitat over all years
Totalbats_habitat<-batactivity %>%
group_by(month, jday, habitat) %>%
summarize(
DTShab = min(DTS_habitat),
lanos = sum(lano),
mysps = sum(mysp),
bats = sum(bat),
) %>%
mutate(lanop = lanos/DTShab) %>%
mutate(myspp = mysps/DTShab) %>%
mutate(batp = bats/DTShab) %>%
# Create date variable for plotting (makes everything 2013-2014 to account for winter season)
mutate(jdate = ifelse(month<4, (as.Date(paste(jday, "2014", sep="/") , format = "%m/%d/%Y")), (as.Date(paste(jday, "2013", sep="/") , format = "%m/%d/%Y")))) %>%
mutate(jdate2= as.Date(jdate, origin = "1970-01-01"))
#glimpse(Totalbats_habitat) #remove the hashtag before 'glimpse' and this code will let you see the first few rows of data in the new set.
Create a graph for BAT activity per INDIVIDUAL SITE (all years combined and adjusted for the number of years surveyed each night). Explanation of the graphics: Each dot represents the average number of bat passes per night at an individual site, averaged across years. The size of the dot represents the number of years the site was surveyed on a given night to give an idea of survey effort. The ‘Night date’ represents the julian date across years. E.g. If a site was surveyed Jan 1 - 30, 2013 and Jan 1 - Feb 28, 2014, the total number of bat passes recorded on each night between Jan 1 - 30, 2013 and 2014, would be summed for each night and divided by 2 (to scale for two years of deployment data). The total number of bat passes recorded on each night between Feb 1 - Feb 28, 2014 would be divided by 1 (no scaling needed since there is only one year of data). The dots on the graph representing the total number of passes recorded during January would be larger in size than the dots representing the total passes during February. This scaling effort prevents sites with heavier survey effort from being overrepresented in the plot, but still allows us to see a representation of survey effort alongside average bat activity.
gs<-ggplot(Totalbats_site, aes(jdate2, batp, size = DTS))
gs+geom_point(aes(color=habitat, alpha=1/10)) + labs(caption="Average number of bat passes recorded at each site per detector per year (color-coded by habitat type) around Southeast Alaska during winter 2011 - 2018. Dot size represents survey effort (larger = more years surveyed)") + ylab("Average bat passes (pass/night/year)") + xlab("Night date")
Create a graph for BAT activity per HABITAT (all years combined and adjusted for detectors deployed across sites). The ‘Night date’ represents the julian date across years. Explanation of the graphics: Each dot represents the average number of bat passes per night within a habitat type (averaged across sites and years). The size of the dot represents the number of detectors deployed in that habitat on that night (summed across all years) to give an idea of survey effort.
gh<-ggplot(Totalbats_habitat, aes(jdate2, batp, size = DTShab))
gh+geom_point(aes(col=habitat, alpha=1/10)) + labs(caption="Average number of nightly bat passes recorded in different habitats around Southeast Alaska during winter 2011 - 2018. Dot size represents survey effort (larger = more sites and years surveyed)") + ylab("Average bat passes recorded (pass/site/night/year)") + xlab("Night date")