####This code is for SIAR analysis of individual animals to compare their consumption of 5 sources setwd("C:/Stikine/Stikine grizzly isotope paper/SIAR") library(siar) graphics.off() ##Load Data for CNS ##The file below excludes the samples that were likely not annual samples consumers_annual<-read.table("Annual values for annual and uncertain samples.txt",header=TRUE) sources_CNS<-read.table("sources.txt",header=TRUE) corrections_CNS<-read.table("corrections.txt",header=TRUE) ##run model model1<-siarmcmcdirichletv4(consumers_annual, sources_CNS, corrections_CNS) ##create plots ##The line below plots the data similar to figure 1 but figure 1 includes all the data siarplotdata(model1) siarhistograms(model1) siarproportionbygroupplot(model1) siarproportionbysourceplot(model1,grp=1) ##view data siarhdrs(model1) ###Make summarized data ##THANKS TO CLAYTON LAMB FOR WRITING THIS CODE out<-model1$output Source_Group<-colnames(out) mean_diet<-data.frame() library(coda) ##Loop for(i in 1:length(Source_Group)){ mean<-mean(out[,i]) ##calculate highest density intervals for error bars lower<-HPDinterval(mcmc(out[,i]))[1] upper<-HPDinterval(mcmc(out[,i]))[2] a<-data.frame(Source_Group=Source_Group[[i]], mean=mean, lower95CI=lower, upper95CI=upper ) mean_diet<-rbind(mean_diet,a) } ###Extract group from Source_Group column b<-as.character(mean_diet$Source_Group) mean_diet$Group<-substr(b, nchar(b)-1, nchar(b)) mean_diet$Source<-substr(b, 1, nchar(b)-2) ####add new group names mean_diet$Groups<-NA mean_diet$Groups<-ifelse(mean_diet$Group=="G1","Interior Female",mean_diet$Groups) mean_diet$Groups<-ifelse(mean_diet$Group=="G2","Interior Male",mean_diet$Groups) mean_diet$Groups<-ifelse(mean_diet$Group=="G3","Coast Female",mean_diet$Groups) mean_diet$Groups<-ifelse(mean_diet$Group=="G4","Coast Male",mean_diet$Groups) ####add Sex mean_diet$Sex<-NA mean_diet$Sex<-ifelse(mean_diet$Group%in%c("G1","G3"),"Female",mean_diet$Sex) mean_diet$Sex<-ifelse(mean_diet$Group%in%c("G2","G4"),"Male",mean_diet$Sex) ####add Ecotype mean_diet$Ecotype<-NA mean_diet$Ecotype<-ifelse(mean_diet$Group%in%c("G1","G2"),"Interior",mean_diet$Ecotype) mean_diet$Ecotype<-ifelse(mean_diet$Group%in%c("G3","G4"),"Coast",mean_diet$Ecotype) ####REMOVE SD1 - SD3 mean_diet<-subset(mean_diet,!Source%in%c("SD1","SD2","SD3")) ##PLOT MEAN DIET FRACTIONS FOR COAST AND INTERIOR BY SEX ##THESE ARE NOT IN THE PAPER ##PLOT1 library(ggplot2) limits <- aes(ymax = upper95CI , ymin=lower95CI) dodge <- position_dodge(width=0.9) p <- ggplot(mean_diet, aes(fill=Groups, y=mean, x=Source))+ geom_bar(position=dodge, stat="identity") + theme_grey(base_size = 18) + theme(axis.title.y=element_text(vjust=1.5),axis.title.x=element_text(vjust=-0.8))+ xlab("SOURCE") + ylab("PROPORTION") +geom_errorbar(limits, position=dodge, width=0.25)+ geom_errorbar(limits, position=dodge, width=0.25) + theme(panel.grid.major = element_blank(), panel.grid.minor = element_blank(), panel.background = element_blank(), axis.line = element_line(colour = "black")) pdf(file="C://Stikine//Stikine grizzly isotope paper//SIAR//ISO_Proportion_ByEcotypePlusSex.pdf", width=11, height=6) print(p) dev.off() ##PLOT2 ##### Growth dodge <- position_dodge(width=0.4) p = ggplot(mean_diet, aes(fill=Sex,y=mean, x=Source, color=Sex)) + geom_point(position=dodge, stat="identity",size=3.4)+ geom_errorbar(aes(ymax = upper95CI , ymin=lower95CI), width=.1,position=dodge)+ theme_bw() + theme(panel.grid.major = element_blank(), panel.grid.minor = element_blank(), axis.line = element_line(colour = "black"), axis.title.x = element_text( vjust=-0.3, size=25), axis.text.x = element_text( size=13), axis.title.y = element_text( vjust=1.5, size=25), axis.text.y = element_text( size=16), strip.text = element_text(size=20)) + ylab("Proportion")+ xlab("Source") pdf(file="C://Stikine//Stikine grizzly isotope paper//SIAR//ISO_Proportion_ByEcotype.pdf", width=11, height=6) print(p + facet_wrap(~ Ecotype) ) dev.off()