####This code is for SIAR analysis of individual animals to compare their consumption 5 sourceS by season ##This file produces figure 4 in the paper ##There is also code to use a 2 isotope analysis for comparison #install.package(siar) setwd("C:/Stikine grizzly isotope paper/SIAR/seasonal_3 isotopes") library(siar) library(coda) library(reshape2) ##graphics.off() ##Load 3 Isotope Data ##Note you can only run 30 samples at a time through SIARsolo ##Break the data into 4 groups consumers_season_grp1<-read.table("Sectioned samples_CNS_Grp1.txt",header=TRUE) consumers_season_grp2<-read.table("Sectioned samples_CNS_Grp2.txt",header=TRUE) consumers_season_grp3<-read.table("Sectioned samples_CNS_Grp3.txt",header=TRUE) consumers_season_grp4<-read.table("Sectioned samples_CNS_Grp4.txt",header=TRUE) ##Load 2 Isotope Data need to change working directory consumers_season_grp1<-read.table("Sectioned samples_CN_Grp1.txt",header=TRUE) consumers_season_grp2<-read.table("Sectioned samples_CN_Grp2.txt",header=TRUE) consumers_season_grp3<-read.table("Sectioned samples_CN_Grp3.txt",header=TRUE) consumers_season_grp4<-read.table("Sectioned samples_CN_Grp4.txt",header=TRUE) ##Load source and fractionation data for 3 isotopes sources<-read.table("sources.txt",header=TRUE) corrections<-read.csv("corrections.csv",header=TRUE) ##Load source and fractionation data for 2 isotopes sources<-read.table("sources_CN.txt",header=TRUE) corrections<-read.table("corrections_CN.txt",header=TRUE) ##Load field data for seasonal samples seasonal<-read.csv("All sectioned samples.csv",header=TRUE) ##Load field data and use a name to denote we are only analyzing 2 isotopes seasonal_CN<-read.csv("All sectioned samples_CNS.csv",header=TRUE) ##note need to change extension ## on line 142 for CN analysis ##run model model1<-siarsolomcmcv4(consumers_season_grp1, sources, corrections,iterations=100000,burnin=10000) model2<-siarsolomcmcv4(consumers_season_grp2, sources, corrections,iterations=100000,burnin=10000) model3<-siarsolomcmcv4(consumers_season_grp3, sources, corrections,iterations=100000,burnin=10000) model4<-siarsolomcmcv4(consumers_season_grp4, sources, corrections,iterations=100000,burnin=10000) ##create plots siarplotdata(model1) siarhistograms(model1) siarproportionbygroupplot(model1) siarproportionbysourceplot(model1,grp=1) siarproportionbysourceplot(model1,grp=2) siarproportionbysourceplot(model1,grp=3) siarproportionbysourceplot(model1,grp=4) siarproportionbysourceplot(model1,grp=5) ##view data siarhdrs(model1) ######################################################### ######################################################### ###LOOP THROUGH MODELS 1-4, SUMMARIZE DATA FROM ###EACH MODEL AND BIND TOGETHER TO CREATE ONE LARGE DF ###THANKS TO CLAYTON LAMB FOR WRITING THIS CODE ######################################################### ######################################################### ##LIST ALL 4 MODELS model_list <- c("model1", "model2", "model3", "model4") ##CREATE EMPTY DATA FRAME TO DEPOSIT FINAL SUMMARIZED DATA mean_diet<-data.frame() ######################################### ##oUTER LOOP, LOOPS THROUGH MODELS 1-4 ######################################### for(j in 1:length(model_list)){ ##GET MODEL model_x <- get(model_list[j]) ##EXTRACT THE OUTPUT FROM MODEL out<-model_x$output ##IDENTIFY ALL THE sOURCE_GROUPS, SUCH AS sALMONG1,ETC, THIS IS ## WHAT WE'LL LOOP THROUGH NEXT TO SUMMARIZE THE DATA WITHIN ##EACH MODEL Source_Group<-colnames(out) ##CREATE EMPTY DATAFRAME TO DEPOSIT SUMMARIZED DATA IN THE INTERM b<-data.frame() ######################################### ##INNER LOOP, LOOPS THROUGH ALL SOURCE_GROUPS ##TO CREATE MEAN AND CONFIDENCE INTERVALS ######################################### for(i in 1:length(Source_Group)){ ##create mean mean<-mean(out[,i]) ##calculate highest density intervals for error bars lower<-HPDinterval(mcmc(out[,i]))[1] upper<-HPDinterval(mcmc(out[,i]))[2] ##CREATE INTERM DATAFRAME WITH A SINGLE ROW OF DATA FOR EACH iTH RECORD a<-data.frame(Source_Group=Source_Group[[i]], mean=mean, lower95CI=lower, upper95CI=upper ) ##BIND THE a DATAFRAME ONTO THE LARGER b DATAFRAME TO CREATE A FULL DATAFRAME FOR EACH MODEL b<-rbind(b,a) } ##BIND THE b DATAFRAME FROM EACH MODEL CREATED ABOVE INTO A LARGER DATA FRAME ##SO ALL RECORDS ARE IN 1 DATAFRAME mean_diet<-rbind(mean_diet,b) } ##CREATE UNIQUE ROW ID TO MATCH TO THE ROWS THE DATA CAME FROM ##IN THE "SEASONAL" DATAFRAME mean_diet$ID <- rep(1:120, each=8) ##this is 7 for 2 isotopes and 8 for 3 isotoeps ##RENAME SOURCES TO GET RID OF GROUPS mean_diet$Source <- rep(c("Salmon", "Moose", "Goat", "Marmot", "Vegetation", "SD1", "SD2", "SD3"), times=120) ## there are only SD1 and 2 for 2 isotopes and SD1-3 for 3 isotoeps , "SD3" ##subset out sd1-3 mean_diet <- subset(mean_diet, !Source %in% c("SD1", "SD2", "SD3")) #remove Source_Group Column mean_diet <- subset(mean_diet, select=-Source_Group ) ###MELT DATA USING RESHAPE2 PACKAGE THEN CAST THE DATA TO CREATE ##DATA IN LONG FORMAT, WITH ID AS THE ROW AND VARIABLE+SOURCE AS THE COLUMNS diet_melt <- melt(mean_diet, id.vars = c("ID", "Source")) diet_cast <- dcast(diet_melt, ID~variable+Source ) ##BIND THE WIDE-FORMAT DIET DATA ONTO SEASONAL USING COLUMN BIND (CBIND) seasonal_diet <- cbind(seasonal, diet_cast) ##SAVE DATA TO EXTERNAL DIRECTORY write.csv(seasonal_diet_CN, file = "C:/Stikine grizzly isotope paper/SIAR/seasonal_2 isotopes/stikineseasonaldiet.csv") ################################################################# ##GGPLOT ##MAKE FIGURE 4 ################################################################# library(ggplot2) ##SALMON## ggplot(seasonal_diet, aes(section.date.since.June1, mean_Salmon))+ geom_rect(aes(xmin=45, xmax=75, ymin=0, ymax=Inf), fill="pink", alpha=0.05)+ geom_rect(aes(xmin=75, xmax=Inf, ymin=0, ymax=Inf), fill="pink", alpha=0.5)+ geom_errorbar(aes(ymin=lower95CI_Salmon, ymax=upper95CI_Salmon), width=1, colour="grey")+ geom_point(aes(shape=Sex,colour=Coast),size=2)+ scale_color_manual(values=c("forestgreen","red"), labels=c("Interior", "Coast"))+ xlim(0,152)+ geom_line(aes(group=Individual, colour=Coast))+ ylab("salmon in diet (%)")+ xlab("Number of days after June 1")+ theme(axis.line.x = element_line(color="black"), axis.line.y = element_line(color="black")) ggsave(filename = "salmon.pdf") ##MARMOT### ggplot(seasonal_diet, aes(section.date.since.June1, mean_Marmot))+ geom_rect(aes(xmin=45, xmax=75, ymin=0, ymax=Inf), fill="pink", alpha=0.05)+ geom_rect(aes(xmin=75, xmax=Inf, ymin=0, ymax=Inf), fill="pink", alpha=0.5)+ geom_errorbar(aes(ymin=lower95CI_Marmot, ymax=upper95CI_Marmot), width=1, colour="grey")+ geom_point(aes(shape=Sex,colour=Coast),size=2)+ scale_color_manual(values=c("forestgreen","red"), labels=c("Interior", "Coast"))+ xlim(0,152)+ geom_line(aes(group=Individual, colour=Coast))+ ylab("marmot in diet (%)")+ xlab("Number of days after June 1")+ theme_classic()+ theme(axis.line.x = element_line(color="black"), axis.line.y = element_line(color="black")) ggsave(filename = "marmot.pdf") ##GOAT### ggplot(seasonal_diet, aes(section.date.since.June1, mean_Goat))+ geom_rect(aes(xmin=45, xmax=75, ymin=0, ymax=Inf), fill="pink", alpha=0.05)+ geom_rect(aes(xmin=75, xmax=Inf, ymin=0, ymax=Inf), fill="pink", alpha=0.5)+ geom_errorbar(aes(ymin=lower95CI_Goat, ymax=upper95CI_Goat), width=1, colour="grey")+ geom_point(aes(shape=Sex,colour=Coast),size=2)+ scale_color_manual(values=c("forestgreen","red"), labels=c("Interior", "Coast"))+ xlim(0,152)+ geom_line(aes(group=Individual, colour=Coast))+ ylab("mountain goat in diet (%)")+ xlab("Number of days after June 1")+ theme_classic()+ theme(axis.line.x = element_line(color="black"), axis.line.y = element_line(color="black")) ggsave(filename = "goat.pdf") ##MOOSE### ggplot(seasonal_diet, aes(section.date.since.June1, mean_Moose))+ geom_rect(aes(xmin=45, xmax=75, ymin=0, ymax=Inf), fill="pink", alpha=0.05)+ geom_rect(aes(xmin=75, xmax=Inf, ymin=0, ymax=Inf), fill="pink", alpha=0.5)+ geom_errorbar(aes(ymin=lower95CI_Moose, ymax=upper95CI_Moose), width=1, colour="grey")+ geom_point(aes(shape=Sex,colour=Coast),size=2)+ scale_color_manual(values=c("forestgreen","red"), labels=c("Interior", "Coast"))+ xlim(0,152)+ geom_line(aes(group=Individual, colour=Coast))+ ylab("moose in diet (%)")+ xlab("Number of days after June 1")+ theme_classic()+ theme(axis.line.x = element_line(color="black"), axis.line.y = element_line(color="black")) ggsave(filename = "moose.pdf") ##vEGETATION### ggplot(seasonal_diet, aes(section.date.since.June1, mean_Vegetation))+ geom_rect(aes(xmin=45, xmax=75, ymin=0, ymax=Inf), fill="pink", alpha=0.05)+ geom_rect(aes(xmin=75, xmax=Inf, ymin=0, ymax=Inf), fill="pink", alpha=0.5)+ geom_errorbar(aes(ymin=lower95CI_Vegetation, ymax=upper95CI_Vegetation), width=1, colour="grey")+ geom_point(aes(shape=Sex,colour=Coast),size=2)+ ##geom_pointrange(aes(x=section.date.since.June1, y=mean_Vegetation, ##ymin=lower95CI_Vegetation, ymax=upper95CI_Vegetation), size=1, color="blue", fill="white", shape=22)+ ##geom_jitter(aes(shape=Sex,colour=Coast),size=2, width = 20)+ scale_color_manual(values=c("forestgreen","red"), labels=c("Interior", "Coast"))+ xlim(0,152)+ geom_line(aes(group=Individual, colour=Coast))+ ylab("vegetation in diet (%)")+ xlab("Number of days after June 1")+ theme_classic()+ theme(axis.line.x = element_line(color="black"), axis.line.y = element_line(color="black")) ggsave(filename = "vegetation.pdf")