require(dplyr) require(stringr) ebird<-read.csv("//Users//ajblake//Documents//Programs//Life List//MyEBirdData 16-02-22.csv", header=T, encoding = "UTF-8") names(ebird) head(ebird) #Deletes unwanted ebird columns ebird<-ebird[c(seq(1,12,1),20)] #Split State.Province column into Country and State/Prov columns ebird$Country<-str_split_fixed(ebird$State.Province, "-", 2)[,1] ebird$State<-str_split_fixed(ebird$State.Province, "-", 2)[,2] ebird$State.Province<-NULL #Split date column into year, month, date columns & rejoined into new date column ebird$Year<-str_split_fixed(ebird$Date, "-", 3)[,3] ebird$Month<-str_split_fixed(ebird$Date, "-", 3)[,1] ebird$Day<-str_split_fixed(ebird$Date, "-", 3)[,2] ebird$Date<-paste(ebird$Year,ebird$Month,ebird$Day, sep="/") ebird$Time<- #Extracts time from Species comments where available ifelse( #Logical test if species comments contains a time for ind. record grepl("\\d{1,2}:\\d{2}", ebird$Species.Comments), ifelse( #Logical test if species comments contains a 12H or 24H time for the ind. record grepl("\\d{1,2}:\\d{2} (AM|PM)", ebird$Species.Comments), #Extracts 12H time from species comment, converts to 24H time, strips date info substr(strptime(str_extract(ebird$Species.Comments, "\\d{1,2}:\\d{2} (AM|PM)"), "%I:%M %p"), 12, 16), #Extracts 24H time from species comment, converts to 24H time, strips date info substr(strptime(str_extract(ebird$Species.Comments, "\\d{1,2}:\\d{2}"), "%H:%M"), 12, 16) ), #Converts checklist time to 24 hour time, strips date info substr(strptime(ebird$Time, "%I:%M %p"), 12, 16) ) ebird$Latitude<- #Extracts latitude from Species comments where available ifelse( #Logical test if species comments contains GPS coordinates for ind. record grepl("\\(\\d*.\\d*, -?\\d*.\\d*\\)", ebird$Species.Comments), #Extracts latitude from species comment, includes "(" & substr(str_extract(ebird$Species.Comments, "\\(\\d*.\\d*,"), 2, nchar(str_extract(ebird$Species.Comments, "\\(\\d*.\\d*,"))-1), #Uses checklist latitude if no individual record latitude available ebird$Latitude ) ebird$Longitude<- #Extracts longitude from Species comments where available ifelse( #Logical test if species comments contains GPS coordinates for ind. record grepl("\\(\\d*.\\d*, -?\\d*.\\d*\\)", ebird$Species.Comments), #Extracts longitude from species comment, includes "(" & substr(str_extract(ebird$Species.Comments, ", -?\\d*.\\d*\\)"), 3, nchar(str_extract(ebird$Species.Comments, ", -?\\d*.\\d*\\)"))-1), #Uses checklist longitude if no individual record latitude available ebird$Longitude ) #Creates Heard column from Species comments ebird$Heard<-grepl("Heard", ebird$Species.Comments) ebird<-with(ebird, data.frame(Submission.ID, Common.Name, Scientific.Name, Taxonomic.Order, Count, Country, State, County, Location, Latitude, Longitude, Date, Year, Month, Day, Time, Heard)) #Removes records of indeterminate species ebird<-ebird[grep("sp\\.", ebird$Common.Name, invert=T),] ebird<-ebird[grep("/", ebird$Common.Name, invert=T),] #Removes (Feral Pigeon) from Scientific Name Column ebird$Scientific.Name<-sub(" \\(Feral Pigeon\\)", "", ebird$Scientific.Name) #Replaces taxonomic order of subspecies with that of the species when available ebird$Taxonomic.Order[grep("\\w* \\w* \\w*", ebird$Scientific.Name)]<- ifelse( is.na( match( #Matches scientific name against dataframe to give #vector index of first match when present otherwise returns NA str_extract( #Extracts first two words #Returns scientific names with three words grep("\\w* \\w* \\w*", ebird$Scientific.Name, value=T), "\\w* \\w*"), ebird$Scientific.Name) ), ebird$Taxonomic.Order[#Returns taxonomic order of subspecies #Returns vector index of records with scientific names with three words grep("\\w* \\w* \\w*", ebird$Scientific.Name) ] , ebird$Taxonomic.Order[#Returns taxonomic order of species match(#Same as above str_extract( grep("\\w* \\w* \\w*", ebird$Scientific.Name, value=T), "\\w* \\w*"), ebird$Scientific.Name) ] ) #Removes Sub Species Information from Common and Scientific Names ebird$Common.Name<-sub("(?! \\(Feral Pigeon\\)) \\(.*\\)", "", ebird$Common.Name, perl=T) ebird$Scientific.Name<- ifelse( #If scientific name has three words remove last word grepl("\\w* \\w* \\w*", ebird$Scientific.Name), sub(" \\S*$", "", ebird$Scientific.Name), ebird$Scientific.Name) #Current Year Locations ------------------ require(plotKML) require(sp) locations<-arrange(ebird, Year, Month, Day, Time) locations<-subset(locations, Year=="2015") locations<-locations[!duplicated(locations$Location),] locations<-data.frame(locations$Longitude, locations$Latitude, locations$Location) names(locations)<-c("Longitude", "Latitude", "Location") #&s cause problems with the XML they are replaced with the xml encoding & locations$Location<-sub("\\&", "&", locations$Location) locations<-with(locations, data.frame( Longitude = as.numeric(as.character(Longitude)), Latitude = as.numeric(as.character(Latitude)), Name = Location)) coordinates(locations)<- ~Longitude+Latitude proj4string(locations) <- CRS("+init=EPSG:4269") kml_open("//Users//ajblake//Documents//Programs//Life List//locations 2015.kml") kml_layer.SpatialPoints( obj=locations, points_names=locations$Name, shape="http://www.ajblake.info/wp-content/uploads/placemark_circle.png", colour="#FFFFFF", size=1, LabelScale=0.6) kml_close("//Users//ajblake//Documents//Programs//Life List//locations 2015.kml") #Global Calculations-------------- #Arranges life list so first sighting of each species is first in the list life.list<-arrange(ebird, Taxonomic.Order, Year, Month, Day, Time) #Narrows list to the first instance of each Taxonomic Order # in the list #effectively limiting list to first sighting of each species life.list<-life.list[!duplicated(ebird$Taxonomic.Order),] head(life.list) length(life.list[,1]) #Lifers by year lifers<-as.vector(tapply(life.list$Taxonomic.Order, life.list$Year, length)) #Calculates all lifers from a given year life.list<-arrange(life.list, Year, Taxonomic.Order) life.list.2015<-subset(life.list, Year=="2015") length(life.list.2014[,1]) #Required for Line Graph year<-sort(unique(ebird$Year)) year.list<-ddply(ebird, "Year", summarize, length(unique(Taxonomic.Order)))[,2] checklists<-ddply(ebird, "Year", summarize, length(unique(Submission.ID)))[,2] #Info above in table (life.list.table<-data.frame(year, year.list, lifers, checklists)) #North American Calculations--------------#Required for Line Graph #Limits sightings to NA by excluding CR & MX records. ebird.NA<-subset(ebird, Country!="CR") ebird.NA<-subset(ebird.NA, Country!="MX") #Also excludes current year for graphing purposes curr.year<-as.numeric(format(Sys.Date(), "%Y")) ebird.NA<-subset(ebird.NA, Year!=curr.year) #Arranges life list so first sighting of each species is first in the list life.list<-arrange(ebird.NA, Taxonomic.Order, Year, Month, Day, Time) #Narrows list to the first instance of each Taxonomic Order # in the list #effectively limiting list to first sighting of each species life.list<-life.list[!duplicated(ebird.NA$Taxonomic.Order),] head(life.list) length(life.list[,1]) #Calculates all lifers from a given year life.list<-arrange(life.list, Year, Month, Day, Time, Taxonomic.Order) (life.list.2016<-subset(life.list, Year=="2015")) #Lifers by year, tail limits to most recent 10 years lifers<-tail(as.vector(tapply(life.list$Taxonomic.Order, as.vector(life.list$Year), length)), 10) #Summarize by year, , tail limits to most recent 10 years year<-tail(as.vector(sort(unique(ebird.NA$Year))), 10) year.list<-tail(ddply(ebird.NA, "Year", summarize, length(unique(Taxonomic.Order)))[,2], 10) checklists<-tail(ddply(ebird.NA, "Year", summarize, length(unique(Submission.ID)))[,2], 10) (life.list.table<-data.frame(year, year.list, lifers, checklists)) #Line Graph--------------------------------------------------- #Set figure width and calculateplot sizes and figure height f_width<-7 (f_height<-f_width*0.66) #Sets the ranges for the two axes y1.range<-c(0,260) y1.int<-25 y1.seq<-seq(y1.range[1],y1.range[2],y1.int) y2.range<-c(0,100) y2.int<-(y2.range[2]-y2.range[1])/10 y2.seq<-seq(y2.range[1],y2.range[2],y2.int) #Shared Attributes col1<-"#377eb8" col2<-"#e41a1c" col3<-"#4daf4a" cols<-c(col1,col2,col3) lwd<-1 pch<-22 pt.cex<-0.8 mtext.cex<-1.1 axis.cex<-0.8 #Export figure to a png with the width and height from above svg("//Users//ajblake//Documents//Programs//Life List//life list graph 2015.svg", width=f_width, height=f_height) #Graph Function with overall axes labels par(mar = c(3.5, 3.5, 1.5, 3.5), xpd=T) #First line graph with year totals and lifers plot(year, year.list, col=col1, type="l", lwd=lwd, axes=F, ann=F, ylim=y1.range, yaxs="i") lines(year, lifers, lwd=lwd, col=col2) points(year, year.list, pch=pch, col=col1, cex=pt.cex) points(year, lifers, pch=pch, col=col2, cex=pt.cex) axis(1, at=year, las=1, lwd=0, lwd.ticks=1, tck=-0.015, mgp=c(3, 0.2, 0), cex.axis=axis.cex) axis(2, at=y1.seq, las=1, lwd=0, lwd.ticks=1, tck=-0.015, mgp=c(3, 0.6, 0), cex.axis=axis.cex) mtext("Year", side=1, line=1.75, cex=mtext.cex) mtext("Number of Bird Species", side=2, line=2.2, cex=mtext.cex) legend(2006, 255, c("Year Totals", "Lifers", "Checklists"), bty="n", col=cols, pch=pch, lty=1, lwd=lwd, pt.cex=pt.cex, cex=0.8) box() #Second Graph with the number of checklists per year par(new=T) plot(year, checklists, col=col3, type="l", lwd=lwd, axes=F, ann=F, ylim=y2.range, yaxs="i") points(year, checklists, pch=pch, col=col3, cex=pt.cex) axis(4, at=y2.seq, las=1, lwd=0, lwd.ticks=1, tck=-0.015, mgp=c(3, 0.6, 0), cex.axis=axis.cex) text(par("usr")[2]+1.0, 50, label="Number of Checklists", srt=-90, xpd=TRUE, cex=mtext.cex) dev.off() #-----------------Life List-------------------------------- require(dplyr) require(xtable) require(plotKML) require(sp) #---- HTML Table ---------- #Arranges life list so first sighting of each species is first in the list life.list<-arrange(ebird, Taxonomic.Order, Year, Month, Day, Time) #Narrows list to the first instance of each Taxonomic Order # in the list #effectively limiting list to first sighting of each species life.list<-life.list[!duplicated(life.list$Taxonomic.Order),] #Wraps the location in a google map link to lat long coordinates life.list$Location<-paste0('',life.list$Location,'') #Narrows to relevant fields for table output life.list<-life.list[c("Common.Name", "Scientific.Name", "Location", "State", "Country", "Date")] #Corrects with final column names names(life.list)<-c("Common Name", "Scientific Name", "Location", "State/Prov", "Country", "Date") #Outputs HTML table print(xtable(life.list), type="html", include.rownames = F, sanitize.text.function = function(x){x}, # Prevents text sanitizing of links html.table.attributes = "border=0 class=birding_list", #class allows for css styling file="//Users//ajblake//Documents//Programs//Life List//Final Lists//world life list.html") #---- Number of species in table length(life.list$"Common Name") #------ KML File --------- life.list.map<-arrange(ebird, Taxonomic.Order, Year, Month, Day, Time) life.list.map<-life.list.map[!duplicated(life.list.map$Taxonomic.Order),] life.list.map<-with(life.list.map, data.frame( #Sets longitude to "Longitude" column after the converting longitude data #from factor to character to number. Longitude = as.numeric(as.character(Longitude)), #As above with latitude Latitude = as.numeric(as.character(Latitude)), Name = Common.Name, #KML point name #Pastes together the KML description Description = paste(Date, ", ", Location, ", ", State, ", ", Country, sep=""))) #Sets coordinate fields coordinates(life.list.map)<- ~Longitude+Latitude #Sets projection for map to NAD83 forcing a reprojection to WGS84 proj4string(life.list.map) <- CRS("+init=EPSG:4269") #Opens (and creates) the kml file kml_open("//Users//ajblake//Documents//Programs//Life List//Final Lists//world life list.kml") #Creates a layer of spatial points. #Issues # Individual style specified for each point. # It would be nice if the kml snippet was set to 0 # The folder for spatial points is unnecessary when only one layer present kml_layer.SpatialPoints( obj=life.list.map, points_names=life.list.map$Name, html.table=life.list.map$Description, shape="http://www.ajblake.info/wp-content/uploads/placemark_circle.png", colour="#FFFFFF", size=1, LabelScale=0.6) #Closes kml file kml_close("//Users//ajblake//Documents//Programs//Life List//Final Lists//world life list.kml") #-------------------CR List------------------------------------ # Commands similar to life List above require(dplyr) require(xtable) require(plotKML) require(sp) #---- HTML Table ---------- #limits list to Costa Rica CR.list<-subset(ebird, Country=="CR") CR.list<-arrange(CR.list, Taxonomic.Order, Year, Month, Day, Time) CR.list<-CR.list[!duplicated(CR.list$Taxonomic.Order),] CR.list$Location<-paste0('',CR.list$Location,'') CR.list<-CR.list[c("Common.Name", "Scientific.Name", "Location", "State", "Country", "Date")] names(CR.list)<-c("Common Name", "Scientific Name", "Location", "State/Prov", "Country", "Date") print(xtable(CR.list), type="html", include.rownames = F, sanitize.text.function = function(x){x}, html.table.attributes = "border=0 class=birding_list", file="//Users//ajblake//Documents//Programs//Life List//Final Lists//CR list.html") #---- Number of species in table length(CR.list$"Common.Name") #------ KML File --------- CR.list.map<-subset(ebird, Country=="CR") CR.list.map<-arrange(CR.list.map, Taxonomic.Order, Year, Month, Day, Time) CR.list.map<-CR.list.map[!duplicated(CR.list.map$Taxonomic.Order),] CR.list.map<-with(CR.list.map, data.frame( Longitude = as.numeric(as.character(Longitude)), Latitude = as.numeric(as.character(Latitude)), Name = Common.Name, Description = paste(Date, ", ", Location, ", ", State, ", ", Country, sep=""))) coordinates(CR.list.map)<- ~Longitude+Latitude proj4string(CR.list.map) <- CRS("+init=EPSG:4269") kml_open("//Users//ajblake//Documents//Programs//Life List//Final Lists//CR list.kml") kml_layer.SpatialPoints( obj=CR.list.map, points_names=CR.list.map$Name, html.table=CR.list.map$Description, shape="http://www.ajblake.info/wp-content/uploads/placemark_circle.png", colour="#FFFFFF", size=1, LabelScale=0.6) kml_close("//Users//ajblake//Documents//Programs//Life List//Final Lists//CR list.kml") #-------------------MX List------------------------------------ # Commands similar to life List above require(dplyr) require(xtable) require(plotKML) require(sp) #---- HTML Table ---------- #limits list to Mexico MX.list<-subset(ebird, Country=="MX") MX.list<-arrange(MX.list, Taxonomic.Order, Year, Month, Day, Time) MX.list<-MX.list[!duplicated(MX.list$Taxonomic.Order),] MX.list$Location<-paste0('',MX.list$Location,'') MX.list<-MX.list[c("Common.Name", "Scientific.Name", "Location", "State", "Country", "Date")] names(MX.list)<-c("Common Name", "Scientific Name", "Location", "State/Prov", "Country", "Date") print(xtable(MX.list), type="html", include.rownames = F, sanitize.text.function = function(x){x}, html.table.attributes = "border=0 class=birding_list", file="//Users//ajblake//Documents//Programs//Life List//Final Lists//MX list.html") #---- Number of species in table length(MX.list$"Common Name") #------ KML File --------- MX.list.map<-subset(ebird, Country=="MX") MX.list.map<-arrange(MX.list.map, Taxonomic.Order, Year, Month, Day, Time) MX.list.map<-MX.list.map[!duplicated(MX.list.map$Taxonomic.Order),] MX.list.map<-with(MX.list.map, data.frame( Longitude = as.numeric(as.character(Longitude)), Latitude = as.numeric(as.character(Latitude)), Name = Common.Name, Description = paste(Date, ", ", Location, ", ", State, ", ", Country, sep=""))) coordinates(MX.list.map)<- ~Longitude+Latitude proj4string(MX.list.map) <- CRS("+init=EPSG:4269") kml_open("//Users//ajblake//Documents//Programs//Life List//Final Lists//MX list.kml") kml_layer.SpatialPoints( obj=MX.list.map, points_names=MX.list.map$Name, html.table=MX.list.map$Description, shape="http://www.ajblake.info/wp-content/uploads/placemark_circle.png", colour="#FFFFFF", size=1, LabelScale=0.6) kml_close("//Users//ajblake//Documents//Programs//Life List//Final Lists//MX list.kml") #-------------------NA List------------------------------------ # Commands similar to life List above require(dplyr) require(xtable) require(plotKML) require(sp) #---- HTML Table ---------- #limits list to North America north of Mexico NA.list<-subset(ebird, Country=="CA" | Country=="US") NA.list<-arrange(NA.list, Taxonomic.Order, Year, Month, Day, Time) NA.list<-NA.list[!duplicated(NA.list$Taxonomic.Order),] NA.list$Location<-paste0('',NA.list$Location,'') NA.list<-NA.list[c("Common.Name", "Scientific.Name", "Location", "State", "Country", "Date")] names(NA.list)<-c("Common Name", "Scientific Name", "Location", "State/Prov", "Country", "Date") print(xtable(NA.list), type="html", include.rownames = F, sanitize.text.function = function(x){x}, html.table.attributes = "border=0 class=birding_list", file="//Users//ajblake//Documents//Programs//Life List//Final Lists//NA list.html") #---- Number of species in table length(NA.list$"Common Name") #------ KML File --------- NA.list.map<-subset(ebird, Country=="CA" | Country=="US") NA.list.map<-arrange(NA.list.map, Taxonomic.Order, Year, Month, Day, Time) NA.list.map<-NA.list.map[!duplicated(NA.list.map$Taxonomic.Order),] NA.list.map<-with(NA.list.map, data.frame( Longitude = as.numeric(as.character(Longitude)), Latitude = as.numeric(as.character(Latitude)), Name = Common.Name, Description = paste(Date, ", ", Location, ", ", State, ", ", Country, sep=""))) coordinates(NA.list.map)<- ~Longitude+Latitude proj4string(NA.list.map) <- CRS("+init=EPSG:4269") kml_open("//Users//ajblake//Documents//Programs//Life List//Final Lists//NA list.kml") kml_layer.SpatialPoints( obj=NA.list.map, points_names=NA.list.map$Name, html.table=NA.list.map$Description, shape="http://www.ajblake.info/wp-content/uploads/placemark_circle.png", colour="#FFFFFF", size=1, LabelScale=0.6) kml_close("//Users//ajblake//Documents//Programs//Life List//Final Lists//NA list.kml") #-------------------AB List------------------------------------ # Commands similar to life List above require(dplyr) require(xtable) require(plotKML) require(sp) #---- HTML Table ---------- #limits list to Alberta AB.list<-subset(ebird, State=="AB") AB.list<-arrange(AB.list, Taxonomic.Order, Year, Month, Day, Time) AB.list<-AB.list[!duplicated(AB.list$Taxonomic.Order),] AB.list$Location<-paste0('',AB.list$Location,'') AB.list<-AB.list[c("Common.Name", "Scientific.Name", "Location", "State", "Country", "Date")] names(AB.list)<-c("Common Name", "Scientific Name", "Location", "State/Prov", "Country", "Date") print(xtable(AB.list), type="html", include.rownames = F, sanitize.text.function = function(x){x}, html.table.attributes = "border=0 class=birding_list", file="//Users//ajblake//Documents//Programs//Life List//Final Lists//AB list.html") #---- Number of species in table length(AB.list$"Common Name") #------ KML File --------- AB.list.map<-subset(ebird, State=="AB") AB.list.map<-arrange(AB.list.map, Taxonomic.Order, Year, Month, Day, Time) AB.list.map<-AB.list.map[!duplicated(AB.list.map$Taxonomic.Order),] AB.list.map<-with(AB.list.map, data.frame( Longitude = as.numeric(as.character(Longitude)), Latitude = as.numeric(as.character(Latitude)), Name = Common.Name, Description = paste(Date, ", ", Location, ", ", State, ", ", Country, sep=""))) coordinates(AB.list.map)<- ~Longitude+Latitude proj4string(AB.list.map) <- CRS("+init=EPSG:4269") kml_open("//Users//ajblake//Documents//Programs//Life List//Final Lists//AB list.kml") kml_layer.SpatialPoints( obj=AB.list.map, points_names=AB.list.map$Name, html.table=AB.list.map$Description, shape="http://www.ajblake.info/wp-content/uploads/placemark_circle.png", colour="#FFFFFF", size=1, LabelScale=0.6) kml_close("//Users//ajblake//Documents//Programs//Life List//Final Lists//AB list.kml") #-------------------BC List------------------------------------ # Commands similar to life List above require(dplyr) require(xtable) require(plotKML) require(sp) #---- HTML Table ---------- #limits list to BC BC.list<-subset(ebird, State=="BC") BC.list<-arrange(BC.list, Taxonomic.Order, Year, Month, Day, Time) BC.list<-BC.list[!duplicated(BC.list$Taxonomic.Order),] BC.list$Location<-paste0('',BC.list$Location,'') BC.list<-BC.list[c("Common.Name", "Scientific.Name", "Location", "State", "Country", "Date")] names(BC.list)<-c("Common Name", "Scientific Name", "Location", "State/Prov", "Country", "Date") print(xtable(BC.list), type="html", include.rownames = F, sanitize.text.function = function(x){x}, html.table.attributes = "border=0 class=birding_list", file="//Users//ajblake//Documents//Programs//Life List//Final Lists//BC list.html") #---- Number of species in table length(BC.list$"Common Name") #------ KML File --------- BC.list.map<-subset(ebird, State=="BC") BC.list.map<-arrange(BC.list.map, Taxonomic.Order, Year, Month, Day, Time) BC.list.map<-BC.list.map[!duplicated(BC.list.map$Taxonomic.Order),] BC.list.map<-with(BC.list.map, data.frame( Longitude = as.numeric(as.character(Longitude)), Latitude = as.numeric(as.character(Latitude)), Name = Common.Name, Description = paste(Date, ", ", Location, ", ", State, ", ", Country, sep=""))) coordinates(BC.list.map)<- ~Longitude+Latitude proj4string(BC.list.map) <- CRS("+init=EPSG:4269") kml_open("//Users//ajblake//Documents//Programs//Life List//Final Lists//BC list.kml") kml_layer.SpatialPoints( obj=BC.list.map, points_names=BC.list.map$Name, html.table=BC.list.map$Description, shape="http://www.ajblake.info/wp-content/uploads/placemark_circle.png", colour="#FFFFFF", size=1, LabelScale=0.6) kml_close("//Users//ajblake//Documents//Programs//Life List//Final Lists//BC list.kml") #-------------------2013 Year List------------------------------------ # Commands similar to life List above require(dplyr) require(xtable) require(plotKML) require(sp) #---- HTML Table ---------- #limits list to 2013 list.2013<-subset(ebird, Year=="2013") list.2013<-arrange(list.2013, Taxonomic.Order, Year, Month, Day, Time) list.2013<-list.2013[!duplicated(list.2013$Taxonomic.Order),] list.2013$Location<-paste0('',list.2013$Location,'') list.2013<-list.2013[c("Common.Name", "Scientific.Name", "Location", "State", "Country", "Date")] names(list.2013)<-c("Common Name", "Scientific Name", "Location", "State/Prov", "Country", "Date") print(xtable(list.2013), type="html", include.rownames = F, sanitize.text.function = function(x){x}, html.table.attributes = "border=0 class=birding_list", file="//Users//ajblake//Documents//Programs//Life List//Final Lists//2013 list.html") #---- Number of species in table length(list.2013$"Common Name") #------ KML File --------- list.2013.map<-subset(ebird, Year=="2013") list.2013.map<-arrange(list.2013.map, Taxonomic.Order, Year, Month, Day, Time) list.2013.map<-list.2013.map[!duplicated(list.2013.map$Taxonomic.Order),] list.2013.map<-with(list.2013.map, data.frame( Longitude = as.numeric(as.character(Longitude)), Latitude = as.numeric(as.character(Latitude)), Name = Common.Name, Description = paste(Date, ", ", Location, ", ", State, ", ", Country, sep=""))) coordinates(list.2013.map)<- ~Longitude+Latitude proj4string(list.2013.map) <- CRS("+init=EPSG:4269") kml_open("//Users//ajblake//Documents//Programs//Life List//Final Lists//2013 list.kml") kml_layer.SpatialPoints( obj=list.2013.map, points_names=list.2013.map$Name, html.table=list.2013.map$Description, shape="http://www.ajblake.info/wp-content/uploads/placemark_circle.png", colour="#FFFFFF", size=1, LabelScale=0.6) kml_close("//Users//ajblake//Documents//Programs//Life List//Final Lists//2013 list.kml") #-------------------2014 Year List------------------------------------ # Commands similar to life List above require(dplyr) require(xtable) require(plotKML) require(sp) #---- HTML Table ---------- #limits list to 2014 list.2014<-subset(ebird, Year=="2014") list.2014<-arrange(list.2014, Taxonomic.Order, Year, Month, Day, Time) list.2014<-list.2014[!duplicated(list.2014$Taxonomic.Order),] list.2014$Location<-paste0('',list.2014$Location,'') list.2014<-list.2014[c("Common.Name", "Scientific.Name", "Location", "State", "Country", "Date")] names(list.2014)<-c("Common Name", "Scientific Name", "Location", "State/Prov", "Country", "Date") print(xtable(list.2014), type="html", include.rownames = F, sanitize.text.function = function(x){x}, html.table.attributes = "border=0 class=birding_list", file="//Users//ajblake//Documents//Programs//Life List//Final Lists//2014 list.html") #---- Number of species in table length(list.2014$"Common Name") #------ KML File --------- list.2014.map<-subset(ebird, Year=="2014") list.2014.map<-arrange(list.2014.map, Taxonomic.Order, Year, Month, Day, Time) list.2014.map<-list.2014.map[!duplicated(list.2014.map$Taxonomic.Order),] list.2014.map<-with(list.2014.map, data.frame( Longitude = as.numeric(as.character(Longitude)), Latitude = as.numeric(as.character(Latitude)), Name = Common.Name, Description = paste(Date, ", ", Location, ", ", State, ", ", Country, sep=""))) coordinates(list.2014.map)<- ~Longitude+Latitude proj4string(list.2014.map) <- CRS("+init=EPSG:4269") kml_open("//Users//ajblake//Documents//Programs//Life List//Final Lists//2014 list.kml") kml_layer.SpatialPoints( obj=list.2014.map, points_names=list.2014.map$Name, html.table=list.2014.map$Description, shape="http://www.ajblake.info/wp-content/uploads/placemark_circle.png", colour="#FFFFFF", size=1, LabelScale=0.6) kml_close("//Users//ajblake//Documents//Programs//Life List//Final Lists//2014 list.kml") #-------------------2015 Year List------------------------------------ # Commands similar to life List above require(dplyr) require(xtable) require(plotKML) require(sp) #---- HTML Table ---------- #limits list to 2015 list.2015<-subset(ebird, Year=="2015") list.2015<-arrange(list.2015, Taxonomic.Order, Year, Month, Day, Time) list.2015<-list.2015[!duplicated(list.2015$Taxonomic.Order),] list.2015$Location<-paste0('',list.2015$Location,'') list.2015<-list.2015[c("Common.Name", "Scientific.Name", "Location", "State", "Country", "Date")] names(list.2015)<-c("Common Name", "Scientific Name", "Location", "State/Prov", "Country", "Date") print(xtable(list.2015), type="html", include.rownames = F, sanitize.text.function = function(x){x}, html.table.attributes = "border=0 class=birding_list", file="//Users//ajblake//Documents//Programs//Life List//Final Lists//2015 list.html") #---- Number of species in table length(list.2015$"Common Name") #------ KML File --------- list.2015.map<-subset(ebird, Year=="2015") list.2015.map<-arrange(list.2015.map, Taxonomic.Order, Year, Month, Day, Time) list.2015.map<-list.2015.map[!duplicated(list.2015.map$Taxonomic.Order),] list.2015.map<-with(list.2015.map, data.frame( Longitude = as.numeric(as.character(Longitude)), Latitude = as.numeric(as.character(Latitude)), Name = Common.Name, Description = paste(Date, ", ", Location, ", ", State, ", ", Country, sep=""))) coordinates(list.2015.map)<- ~Longitude+Latitude proj4string(list.2015.map) <- CRS("+init=EPSG:4269") kml_open("//Users//ajblake//Documents//Programs//Life List//Final Lists//2015 list.kml") kml_layer.SpatialPoints( obj=list.2015.map, points_names=list.2015.map$Name, html.table=list.2015.map$Description, shape="http://www.ajblake.info/wp-content/uploads/placemark_circle.png", colour="#FFFFFF", size=1, LabelScale=0.6) kml_close("//Users//ajblake//Documents//Programs//Life List//Final Lists//2015 list.kml") #--------- # of Checklist vs # of Species Graph ---------- #-----2013 #Subsets to Metro Vancouver and orders taxonomically list.2013<-subset(ebird, County=="Metro Vancouver") list.2013<-subset(list.2013, Year=="2013") list.2013<-arrange(list.2013, Taxonomic.Order, Year, Month, Day, Time) checklists.2013<-list.2013[c(1,9,12,13,14,15,16)] #removes unnecessary columns #Converts "Submission.ID" from factor to character to allow sorting checklists.2013$Submission.ID<-as.character(checklists.2013$Submission.ID) #Sorts by "Submission.ID" so that checklists are ordered by date then checklist checklists.2013<-arrange(checklists.2013, Year, Month, Day, Time, Submission.ID) #Limits to unique checklists using Submission.ID checklists.2013<-checklists.2013[!duplicated(checklists.2013$Submission.ID),] #Converts time date fields to POSIXlt format checklists.2013$Date.Time<-strptime(paste(checklists.2013$Date,checklists.2013$Time), "%Y/%m/%d %H:%M") #Converts POSIXlt format into a Julian Date with 12 AM PST Jan 1, 2013 as origin checklists.2013$Julian<-julian(checklists.2013$Date.Time, origin = as.POSIXct("2013-01-01", tz = "Canada/Pacific")) #Function to calculate the year total after each checklist spp.by.chklst<-function(x){ length( # Calculates # of species by the length of resulting vector list.2013[ # The & results in a vector with TRUE values for the first # observations of species limited to the first "x" checklists # Creates vector with TRUE values for the first observation of a species !duplicated(list.2013$Taxonomic.Order) # Creates vector with TRUE values where the Submission.IDs match # the vector below & list.2013$Submission.ID %in% # Creates a vector of Submission.IDs from 1 to "x" checklists.2013[checklists.2013$Date.Time<=checklists.2013$Date.Time[x],1] ,2]# Limits to column name column )} spp.by.chklst.2013<-unlist( #Converts from list to vector lapply( #applies the above function to the vector below #Creates a vector of integers from 1, 2, ... # number of checklists seq(1,length(checklists.2013$Submission.ID),1), spp.by.chklst)) #-----2014 #comments the same as 2013 list.2014<-subset(ebird, County=="Metro Vancouver") list.2014<-subset(list.2014, Year=="2014") list.2014<-arrange(list.2014, Taxonomic.Order, Year, Month, Day, Time) checklists.2014<-list.2014[c(1,9,12,13,14,15,16)] #removes unnecessary columns checklists.2014$Submission.ID<-as.character(checklists.2014$Submission.ID) checklists.2014<-arrange(checklists.2014, Year, Month, Day, Time, Submission.ID) checklists.2014<-checklists.2014[!duplicated(checklists.2014$Submission.ID),] checklists.2014$Date.Time<-strptime(paste(checklists.2014$Date,checklists.2014$Time), "%Y/%m/%d %H:%M") checklists.2014$Julian<-julian(checklists.2014$Date.Time, origin = as.POSIXct("2014-01-01", tz = "Canada/Pacific")) spp.by.chklst<-function(x){ length( list.2014[!duplicated(list.2014$Taxonomic.Order) & list.2014$Submission.ID %in% checklists.2014[checklists.2014$Date.Time<=checklists.2014$Date.Time[x],1] ,2] )} spp.by.chklst.2014<-unlist(lapply(seq(1,length(checklists.2014$Submission.ID),1), spp.by.chklst)) #------2015 #comments the same as 2013 list.2015<-subset(ebird, County=="Metro Vancouver") list.2015<-subset(list.2015, Year=="2015") list.2015<-arrange(list.2015, Taxonomic.Order, Year, Month, Day, Time) checklists.2015<-list.2015[c(1,9,12,13,14,15,16)] #removes unnecessary columns checklists.2015$Submission.ID<-as.character(checklists.2015$Submission.ID) checklists.2015<-arrange(checklists.2015, Year, Month, Day, Time, Submission.ID) checklists.2015<-checklists.2015[!duplicated(checklists.2015$Submission.ID),] checklists.2015$Date.Time<-strptime(paste(checklists.2015$Date,checklists.2015$Time), "%Y/%m/%d %H:%M") checklists.2015$Julian<-julian(checklists.2015$Date.Time, origin = as.POSIXct("2015-01-01", tz = "Canada/Pacific")) spp.by.chklst<-function(x){ length( list.2015[!duplicated(list.2015$Taxonomic.Order) & list.2015$Submission.ID %in% checklists.2015[checklists.2015$Date.Time<=checklists.2015$Date.Time[x],1] ,2] )} spp.by.chklst.2015<-unlist(lapply(seq(1,length(checklists.2015$Submission.ID),1), spp.by.chklst)) #------ Graph #Set figure width and calculateplot sizes and figure height f_width<-7 (f_height<-f_width*0.66) #Shared Attributes col1<-"#377eb8" #Line colours col2<-"#e41a1c" col3<-"#4daf4a" cols<-c(col1,col2,col3) #Colour vector migrcol<-"#e6f9ff" #Colour of migratory rectangles pt.cex<-0.8 #Point size mtext.cex<-1.1 #Axis titles axis.cex<-0.8 #Axis labels lwd<-1 #Line width xlim<-c(0,366) #X axis range ylim<-c(0,225) #Y axis range # Month Labels Months<-c("Jan","Feb","Mar","Apr","May","Jun","Jul","Aug","Sept","Oct","Nov","Dec","") #Generate tick mark locations in julian dates for month labels Jul.Months<-paste(rep(2015,12),seq(1,12,1),rep(1,12), sep="-") Jul.Months<-strptime(Jul.Months, "%Y-%m-%d") Jul.Months<-c(julian(Jul.Months, origin = as.POSIXct("2015-01-01", tz = "Canada/Pacific")), 366) #Export figure to a png with the width and height from above svg("//Users//ajblake//Documents//Programs//Life List//spp by checklist.svg", width=f_width, height=f_height) #Figure margin in inches par(mar = c(3.5, 4.5, 1, 1)) #New blank plot plot.new() plot.window(xlim=xlim, ylim=ylim, xaxs="i", yaxs="i") #Migratory period rectangles rect(Jul.Months[4], ylim[1], Jul.Months[6], ylim[2], col=migrcol, border = NA) rect(Jul.Months[8], ylim[1], Jul.Months[11], ylim[2], col=migrcol, border = NA) #2013-2015 points & lines Julian date vs # of species points(checklists.2013$Julian, spp.by.chklst.2013, col=col1, pch=22, cex=pt.cex) lines(c(0,checklists.2013$Julian), c(0,spp.by.chklst.2013), col=col1, lwd=lwd) points(checklists.2014$Julian, spp.by.chklst.2014, col=col2, pch=22, cex=pt.cex) lines(c(0,checklists.2014$Julian), c(0,spp.by.chklst.2014), col=col2, lwd=lwd) points(checklists.2015$Julian, spp.by.chklst.2015, col=col3, pch=22, cex=pt.cex) lines(c(0,checklists.2015$Julian), c(0,spp.by.chklst.2015), col=col3, lwd=lwd) #X axis ticks without labels axis(1, las=1, lwd=0, lwd.ticks=1, at=Jul.Months, labels=FALSE, cex.axis=axis.cex) #X axis labels shifted 15 days to center without tick marks axis(1, las=1, lwd=0, lwd.ticks=1, mgp=c(3,0.5,0), at=Jul.Months+15, tick=FALSE, Months, cex.axis=axis.cex) #Y axis axis(2, las=1, lwd=0, lwd.ticks=1, mgp=c(3,0.75,0), at=seq(ylim[1], ylim[2], 25), cex.axis=axis.cex) box() #Axis titles mtext("Month", side=1, line=2.0, cex=mtext.cex) mtext("Number of Bird Species", side=2, line=2.8, cex=mtext.cex) #Legend legend(xlim[1]+10, ylim[2]-5, c("2013", "2014", "2015"), bty="n", col=cols, pch=22, lty=1, lwd=lwd, pt.cex=pt.cex, cex=axis.cex) dev.off() #----------- Metro Vancouver eBird top 100 Graph ------------- require(rvest) require(stringr) #Scrapes 2015 data from eBird url <- "http://ebird.org/ebird/canada/top100?locInfo.regionType=subnational2&locInfo.regionCode=CA-BC-GV&year=2015" Top100.2015 <- url %>% read_html() %>% html_nodes(xpath='//*[@id="sightingsTable"]') %>% html_table() Top100.2015 <- Top100.2015[[1]] rank.2015<-as.numeric(row.names(Top100.2015)[Top100.2015$Observer=="Adam Blake"]) Top100.2015 <- as.numeric(str_split_fixed(Top100.2015[,4]," ",2)[,1]) #Scrapes 2014 data from eBird url <- "http://ebird.org/ebird/canada/top100?locInfo.regionType=subnational2&locInfo.regionCode=CA-BC-GV&year=2014" Top100.2014 <- url %>% read_html() %>% html_nodes(xpath='//*[@id="sightingsTable"]') %>% html_table() Top100.2014 <- Top100.2014[[1]] rank.2014<-as.numeric(row.names(Top100.2014)[Top100.2014$Observer=="Adam Blake"]) Top100.2014 <- as.numeric(str_split_fixed(Top100.2014[,4]," ",2)[,1]) #Scrapes 2013 data from eBird url <- "http://ebird.org/ebird/canada/top100?locInfo.regionType=subnational2&locInfo.regionCode=CA-BC-GV&year=2013" Top100.2013 <- url %>% read_html() %>% html_nodes(xpath='//*[@id="sightingsTable"]') %>% html_table() Top100.2013 <- Top100.2013[[1]] rank.2013<-as.numeric(row.names(Top100.2013)[Top100.2013$Observer=="Adam Blake"]) Top100.2013 <- as.numeric(str_split_fixed(Top100.2013[,4]," ",2)[,1]) #Data for csv file scrapped from here #(http://ebird.org/ebird/canada/top100?locInfo.regionType=subnational2&locInfo.regionCode=CA-BC-GV&year=2015) top100<-read.csv("//Users//ajblake//Documents//Programs//Life List//ebird top 100.csv", header=T, encoding = "UTF-8") names(top100)<-c("2015", "2014", "2013") #Sets colours col1<-"#377eb8" col1b<-"#9dc3e2" col2<-"#e41a1c" col2b<-"#f28c8e" col3<-"#4daf4a" col3b<-"#a7d9a5" #Sets colour vectors inserting lighter colours at the ranks scraped above cols.2015<-rep(col3,100) cols.2015[rank.2015]<-col3b cols.2014<-rep(col2,100) cols.2014[rank.2015]<-col2b cols.2014[rank.2014]<-col2b cols.2013<-rep(col1,100) cols.2013[rank.2015]<-col1b cols.2013[rank.2014]<-col1b cols.2013[rank.2013]<-col1b #Set figure width and calculateplot sizes and figure height f_width<-7 (f_height<-f_width*0.5) ylim<-c(0,275) mtext.cex<-1.1 axis.cex<-0.8 #Export figure to a pdf with the width and height from above svg("//Users//ajblake//Documents//Programs//Life List//ebird top 100.svg", width=f_width, height=f_height) par(mar = c(3.5, 4.5, 1, 1)) #Plots with colour vectors from above bp<-barplot(top100$"2015", col=cols.2015, axes=F, border = NA, ylim=ylim, xaxs="i") par(new=T) barplot(top100$"2014", col=cols.2014, axes=F, border = NA, ylim=ylim, xaxs="i") par(new=T) barplot(top100$"2013", col=cols.2013, axes=F, border = NA, ylim=ylim, xaxs="i") #Axis labels axis(1, las=1, lwd=0, lwd.ticks=1, tck=-0.03, mgp=c(3,0.3,0), at=c(0.2,seq(12,120,12)), cex.axis=axis.cex, as.character(seq(0,100,10))) axis(2, las=1, lwd=0, lwd.ticks=1, tck=-0.03, mgp=c(3,0.65,0), at=seq(ylim[1], ylim[2], 25), cex.axis=axis.cex) box() #Axis Titles mtext("Rank of Top eBirders in Metro Vancouver", side=1, line=2.0, cex=mtext.cex) mtext("Bird Species Year Totals", side=2, line=2.8, cex=mtext.cex) #Manually drawn legend leg.top<-ylim[2]-10 leg.left<-112 leg.sp<-20 leg.off<-14 text(leg.left, leg.top-leg.off-0*leg.sp, "2013", cex=axis.cex, adj=c(0,0)) rect(leg.left-2, leg.top-0*leg.sp, leg.left-1, leg.top-3*leg.sp, col=col1, border=NA) text(leg.left, leg.top-leg.off-1*leg.sp, "2014", cex=axis.cex, adj=c(0,0)) rect(leg.left-2, leg.top-1*leg.sp, leg.left-1, leg.top-3*leg.sp, col=col2, border=NA) text(leg.left, leg.top-leg.off-2*leg.sp, "2015", cex=axis.cex, adj=c(0,0)) rect(leg.left-2, leg.top-2*leg.sp, leg.left-1, leg.top-3*leg.sp, col=col3, border=NA) # Coloured arrows arrows(bp[rank.2015], top100$"2015"[rank.2015]+25, bp[rank.2015], top100$"2015"[rank.2015]+5, length=0.05, col=col3) arrows(bp[rank.2014], top100$"2015"[rank.2014]+25, bp[rank.2014], top100$"2015"[rank.2014]+5, length=0.05, col=col2) arrows(bp[rank.2013], top100$"2015"[rank.2013]+25, bp[rank.2013], top100$"2015"[rank.2013]+5, length=0.05, col=col1) dev.off()