require(plyr) require(doBy) require(ggplot2) reproanalysis<-read.csv('ReproAnalysis.csv', header=T) print(reproanalysis) #First we will find the time in days between the threshold temp and the peak brooding peakbrood<-ddply(reproanalysis,.(Site,Pop),subset,Brooders==max(Brooders, na.rm=T)) print(peakbrood) #find the dates with maximum number of brooders oysthresh<-ddply(oysmintemp,.(Date),subset,min_temp>=12.5) #create a list of temps to find those above threshold temp for minimum daily temps at each site print(oysthresh) fidthresh<-ddply(fidmintemp,.(Date),subset,min_temp>=12.5) View(fidthresh) manthresh<-ddply(manmintemp,.(Date),subset,min_temp>=12.5) View(manthresh) peakbrood$Date<-as.Date(peakbrood$Date) oysthresh$Date<-as.Date(oysthresh$Date) fidthresh$Date<-as.Date(fidthresh$Date) manthresh$Date<-as.Date(manthresh$Date) #make sure everything works as a Date in R after producing all the threshold temp data d<-c("2014-06-03","2014-06-08","2014-05-14") #dates visually confirmed for threshold temps p<-c("Fidalgo","Manchester","Oyster Bay") thresholddate<-data.frame(p,d) thresholddate$d<-as.Date(thresholddate$d) peakthresh<-merge(peakbrood,thresholddate,by.x="Site",by.y="p",all=F) print(peakthresh) #create a data frame that compares dates for threshold and peak spawning dates peakthresh$time_to_peak<-difftime(peakthresh$Date,peakthresh$d,units="days") #finds the difference in days between threshold temp and peak spawning print(peakthresh) peakthresh2<-peakthresh[c("Date","Site","Pop","Brooders","d","time_to_peak")] #subsets df to only relevant information peakthresh2<-rename(peakthresh2,c('Date'='Peak_Date',"Site"="Site","Pop"="Pop","Brooders"="Brooders","d"="Threshold Date","time_to_peak"="Days_to_Peak")) #renames df columns to more meaningful names print(peakthresh2) #next we want to find the degree days from minimum winter temp to spawning peak #looking at previously generated temp graphs we decided that 8 was minimum winter temp #we have to visually confirm when the temps continually increase from 8 to spawning oysdd<-ddply(oysmintemp,.(Date),subset,min_temp>=8) #subsets minimum temp data to find dates with temps above 8 C. oysdd<-oysmintemp[c(oysmintemp$Date>="2014-03-06"),] #after visually confirming the initial temp date we then subset the data from this point on print(oysdd) #we have to subset temp data to just the time frame between 8C beginning and peak spawn for each pop at each site #luckily two pops at each site had the same spawn time data so we use that oyshndd<-oysdd[c(oysdd$Date<="2014-07-10"),] oyssdd<-oysdd[c(oysdd$Date<="2014-06-19"),] print(oyshndd) print(oyssdd) #once these subsets are created we need to create a column of the difference between the 8 C minimum #and the daily minimum temp for each subsets oyshndd$tempdiff<-oyshndd$min_temp-8 oyssdd$tempdiff<-oyssdd$min_temp-8 #use this temp diff column to create the degree days between 8C minimum and the peak threshold colSums(oyshndd[,-1]) colSums(oyssdd[,-1]) #we generate this same info for all pops at all sites fiddd<-ddply(fidmintemp,.(Date),subset,min_temp>=8) fiddd<-fidmintemp[c(fidmintemp$Date>="2014-03-06"),] View(fiddd) fidhndd<-fiddd[c(fiddd$Date<="2014-08-08"),] fidsdd<-fiddd[c(fiddd$Date<="2014-07-11"),] View(fidhndd) View(fidsdd) fidhndd$tempdiff<-fidhndd$min_temp-8 fidsdd$tempdiff<-fidsdd$min_temp-8 colSums(fidhndd[,-1]) colSums(fidsdd[,-1]) mandd<-ddply(manmintemp,.(Date),subset,min_temp>=8) mandd<-manmintemp[c(manmintemp$Date>="2014-03-06"),] View(mandd) manhsdd<-mandd[c(mandd$Date<="2014-08-06"),] manndd<-mandd[c(mandd$Date<="2014-06-25"),] View(manhsdd) View(manndd) manhsdd$tempdiff<-manhsdd$min_temp-8 manndd$tempdiff<-manndd$min_temp-8 colSums(manhsdd[,-1]) colSums(manndd[,-1]) #due to how R works its easier to just copy these numbers and create a data frame to merge with the peak threshold info DegreeDays<-c("512.999","512.999","354.156","453.021","453.021","307.894","377.561","175.322","377.561") Pop<-c("H","N","S") Site<-c("Oyster Bay","Oyster Bay","Oyster Bay","Fidalgo","Fidalgo","Fidalgo","Manchester","Manchester","Manchester") Degree<-data.frame(Site,Pop,DegreeDays) #onces the Degree data frame is created it can be merged with the peakthresh2 data frame to show degree days and time to peak in the same table peakthresh3<-merge(peakthresh2,Degree,by.x=c("Site","Pop"),by.y=c("Site","Pop"),all=T) print(peakthresh3) #now we need to make a graph because nothing is good unless its a graph #first we merge the three longest time frame tempdiff to create a data frame that works with ggplot2 odd<-oysdd[c(oysdd$Date<="2014-08-15"),] odd$tempdiff<-odd$min_temp-8 fdd<-fiddd[c(fiddd$Date<="2014-08-15"),] fdd$tempdiff<-fdd$min_temp-8 mdd<-mandd[c(mandd$Date<="2014-08-15"),] mdd$tempdiff<-mdd$min_temp-8 of<-merge(odd,fdd,by="Date",all=T,incomparables="0") dddf<-merge(of,mdd,by="Date",all=T,incomparables="0") #we need to clean up the NAs produced so that these can be graphed in ggplot2 dddf[is.na(dddf)]<-0 #Now we rename the columns to meaningful titles dddf<-rename(dddf,c('Date'='Date','min_temp.x'='oysmin','tempdiff.x'='oystempdiff','min_temp.y'='fidmin','tempdiff.y'='fidtempdiff','min_temp'='manmin','tempdiff'='mantempdiff')) #check the data frame to make sure that everything aligns to the X axis dates of interest with the right tempdiff numbers View(dddf) #using ggplot and cumsum(cumulativesum) we can create cumulative lines of the tempdiffs #we have to manually add points to the line through annotate to show the threshold temps and peak brooding for each pop ggplot(dddf)+ geom_line(aes(x=Date,y=cumsum(dddf$oystempdiff)),color="orange",size=2)+ geom_line(aes(x=Date,y=cumsum(dddf$fidtempdiff)),color="purple",size=2)+ geom_line(aes(x=Date,y=cumsum(dddf$mantempdif)),color="red",size=2)+ annotate("point",x=as.Date("2014-06-03",'%Y-%m-%d'),y=133,size=5,color='red',pch=15)+ annotate("point",x=as.Date("2014-05-14",'%Y-%m-%d'),y=143,size=5,color='red',pch=15)+ annotate("point",x=as.Date("2014-06-08",'%Y-%m-%d'),y=113,size=5,color='red',pch=15)+ annotate("point",x=as.Date("2014-08-08",'%Y-%m-%d'),y=460,size=10,color='blue',pch=13)+ annotate("point",x=as.Date("2014-08-06",'%Y-%m-%d'),y=383,size=10,color='blue',pch=13)+ annotate("point",x=as.Date("2014-07-10",'%Y-%m-%d'),y=520,size=10,color='blue',pch=13)+ annotate("point",x=as.Date("2014-08-08",'%Y-%m-%d'),y=453.021,size=10,color='purple',pch=13)+ annotate("point",x=as.Date("2014-06-25",'%Y-%m-%d'),y=175.322,size=10,color='purple',pch=13)+ annotate("point",x=as.Date("2014-07-10",'%Y-%m-%d'),y=512.999,size=10,color='purple',pch=13)+ annotate("point",x=as.Date("2014-07-11",'%Y-%m-%d'),y=307.894,size=10,color='orange',pch=13)+ annotate("point",x=as.Date("2014-08-06",'%Y-%m-%d'),y=377.561,size=10,color='orange',pch=13)+ annotate("point",x=as.Date("2014-06-19",'%Y-%m-%d'),y=354.156,size=10,color='orange',pch=13)+ theme_bw()+ labs(title="Degree Days compared between Sites and Populations",x="Date",y="Cumulative Degrees over 8 C Minimum") #each red square represents the date when the threshold 12.5 C spawning temp was reached #the orange, purple, and red lines are Oyster Bay, Fidalgo, and Manchester Sites respectfull #the orange, blue, and purple crosshairs are peak brooding for Oyster Bay, Dabob, and Fidalgo pops at each site respectfully #