#'
#' Create summaries of the proportion of species uniquely found
#' on each Island, Plot, Habitat. The function does not have any arguments
#' yet. I may change this at a later stage.
#'
#'
specprop<-function(.){
dat %>%
dplyr::select(Species,Coloniz,IND.NON.IND,Island) %>%
unique() %>%
rename(species=Species) ->statusinfo
#------ Island scale: all species
pardat %>%
gather(species,abundance,-c(Archip,Island,habitat,MACDIVCode,sampleno)) %>%
group_by(Archip,Island,species) %>%
summarise(ab=sum(abundance)) %>%
mutate(ab=ifelse(ab > 0,1,ab)) %>%
group_by(Archip,Island,species) %>%
summarise(abflag=sum(ab)) %>%
filter(abflag > 0) %>%
dplyr::select(Archip,Island,species) %>%
ungroup() -> tmp1
tmp1 %>%
mutate(counter=1) %>%
group_by(Archip,species) %>%
summarise(tot=sum(counter)) %>%
filter(tot==1) %>%
left_join(unique(tmp1)) %>%
group_by(Archip,Island) %>%
summarise(nsp=n_distinct(species)) %>%
ungroup() ->tmp2
pardat %>%
gather(species,abundance,-c(Archip,Island,habitat,MACDIVCode,sampleno)) %>%
filter(abundance > 0) %>%
group_by(Archip) %>%
summarise(tot=n_distinct(species)) %>%
ungroup() %>%
left_join(tmp2) %>%
mutate(prop=nsp/tot) %>%
dplyr::select(Island,tot,nsp,prop) %>%
mutate(group="All species") ->prop.allsp
#------ Island scale: other groups
# get data in the right format
pardat %>%
gather(species,abundance,-c(Archip,Island,habitat,MACDIVCode,sampleno)) %>%
group_by(Archip,Island,species) %>%
summarise(ab=sum(abundance)) %>%
mutate(ab=ifelse(ab > 0,1,ab)) %>%
group_by(Archip,Island,species) %>%
summarise(abflag=sum(ab)) %>%
filter(abflag > 0) %>%
inner_join(statusinfo) %>%
dplyr::select(species,Coloniz,IND.NON.IND,Island,Archip) %>%
ungroup() %>%
gather(origin1,origin,-c(species,Island,Archip)) %>%
filter(origin!="NON-IND" & origin!="IND?" & origin!="??") %>%
dplyr::select(-c(origin1)) -> tmp1
# unique species per Island
tmp1 %>%
mutate(counter=1) %>%
group_by(Archip,species,origin) %>%
summarise(tot=sum(counter)) %>%
filter(tot==1) %>%
left_join(unique(tmp1)) %>%
group_by(Archip,Island,origin) %>%
summarise(nsp=n_distinct(species)) %>%
ungroup() ->tmp2
# division with totals by Island
tmp1 %>%
group_by(Archip,origin) %>%
summarise(tot=n_distinct(species)) %>%
ungroup() %>%
left_join(tmp2) %>%
mutate(prop=nsp/tot) %>%
dplyr::select(Island,tot,nsp,prop,origin) %>%
rename(group=origin) %>%
bind_rows(prop.allsp) ->prop.allsp1
writeWorksheetToFile(data=prop.allsp1,file="/mnt/data1tb/Dropbox/macdiv/betaprops/betatprops.xlsx",
sheet = "summary", header = TRUE,startCol=1,startRow=1,styleAction =XLC$"STYLE_ACTION.NONE")
#------ Plot scale: all species
pardat %>%
gather(species,abundance,-c(Archip,Island,habitat,MACDIVCode,sampleno)) %>%
group_by(Archip,Island,species,MACDIVCode) %>%
summarise(ab=sum(abundance)) %>%
mutate(ab=ifelse(ab > 0,1,ab)) %>%
group_by(Archip,Island,species,MACDIVCode) %>%
summarise(abflag=sum(ab)) %>%
filter(abflag > 0) %>%
dplyr::select(Archip,Island,species,MACDIVCode) %>%
ungroup() -> tmp1
tmp1 %>%
mutate(counter=1) %>%
group_by(Island,species) %>%
summarise(tot=sum(counter)) %>%
filter(tot==1) %>%
left_join(unique(tmp1)) %>%
group_by(Archip,Island,MACDIVCode) %>%
summarise(nsp=n_distinct(species)) %>%
ungroup() ->tmp2
pardat %>%
gather(species,abundance,-c(Archip,Island,habitat,MACDIVCode,sampleno)) %>%
filter(abundance > 0) %>%
group_by(Archip,Island) %>%
summarise(tot=n_distinct(species)) %>%
ungroup() %>%
left_join(tmp2) %>%
mutate(prop=nsp/tot) %>%
dplyr::select(Island,tot,nsp,prop,MACDIVCode) %>%
mutate(group="All species") ->prop.allsp.plots
#------ Plot scale: other groups
pardat %>%
gather(species,abundance,-c(Archip,Island,habitat,MACDIVCode,sampleno)) %>%
group_by(Archip,Island,species,MACDIVCode) %>%
summarise(ab=sum(abundance)) %>%
mutate(ab=ifelse(ab > 0,1,ab)) %>%
group_by(Archip,Island,species,MACDIVCode) %>%
summarise(abflag=sum(ab)) %>%
filter(abflag > 0) %>%
inner_join(statusinfo) %>%
dplyr::select(species,Coloniz,IND.NON.IND,Island,Archip,MACDIVCode) %>%
ungroup() %>%
gather(origin1,origin,-c(species,Island,Archip,MACDIVCode)) %>%
filter(origin!="NON-IND" & origin!="IND?" & origin!="??") %>%
dplyr::select(-c(origin1)) -> tmp1
# unique species per plot
tmp1 %>%
mutate(counter=1) %>%
group_by(Island,species,origin) %>%
summarise(tot=sum(counter)) %>%
filter(tot==1) %>%
left_join(unique(tmp1)) %>%
group_by(Archip,Island,origin,MACDIVCode) %>%
summarise(nsp=n_distinct(species)) %>%
ungroup() ->tmp2
# division with totals by plot
tmp1 %>%
group_by(Archip,Island,origin) %>%
summarise(tot=n_distinct(species)) %>%
ungroup() %>%
left_join(tmp2) %>%
mutate(prop=nsp/tot) %>%
dplyr::select(Island,tot,nsp,prop,MACDIVCode,origin) %>%
rename(group=origin) %>%
bind_rows(prop.allsp.plots) ->prop.allsp.plots1
writeWorksheetToFile(data=prop.allsp.plots1,file="/mnt/data1tb/Dropbox/macdiv/betaprops/betatprops.xlsx",
sheet = "summary", header = TRUE,startCol=7,startRow=1,styleAction =XLC$"STYLE_ACTION.NONE")
#------ Habitat scale: all species
pardat %>%
gather(species,abundance,-c(Archip,Island,habitat,MACDIVCode,sampleno)) %>%
filter(habitat!="Herbaceous") %>%
group_by(Archip,Island,species,MACDIVCode,habitat) %>%
summarise(ab=sum(abundance)) %>%
mutate(ab=ifelse(ab > 0,1,ab)) %>%
group_by(Archip,Island,species,MACDIVCode,habitat) %>%
summarise(abflag=sum(ab)) %>%
filter(abflag > 0) %>%
dplyr::select(Archip,Island,species,MACDIVCode,habitat) %>%
ungroup() -> tmp1
tmp1 %>%
mutate(counter=1) %>%
group_by(Island,species,MACDIVCode) %>%
summarise(tot=sum(counter)) %>%
filter(tot==1) %>%
left_join(unique(tmp1)) %>%
group_by(Archip,Island,MACDIVCode,habitat) %>%
summarise(nsp=n_distinct(species)) %>%
ungroup() ->tmp2
pardat %>%
gather(species,abundance,-c(Archip,Island,habitat,MACDIVCode,sampleno,habitat)) %>%
filter(habitat!="Herbaceous") %>%
filter(abundance > 0) %>%
group_by(Archip,Island,MACDIVCode) %>%
summarise(tot=n_distinct(species)) %>%
ungroup() %>%
left_join(tmp2) %>%
mutate(prop=nsp/tot) %>%
dplyr::select(Island,tot,nsp,prop,MACDIVCode,habitat) %>%
mutate(group="All species") ->prop.allsp.plots.habitats
#------ Habitat scale: other groups
pardat %>%
gather(species,abundance,-c(Archip,Island,habitat,MACDIVCode,sampleno)) %>%
filter(habitat!="Herbaceous") %>%
group_by(Archip,Island,species,MACDIVCode,habitat) %>%
summarise(ab=sum(abundance)) %>%
mutate(ab=ifelse(ab > 0,1,ab)) %>%
group_by(Archip,Island,species,MACDIVCode,habitat) %>%
summarise(abflag=sum(ab)) %>%
filter(abflag > 0) %>%
inner_join(statusinfo) %>%
dplyr::select(species,Coloniz,IND.NON.IND,Island,Archip,MACDIVCode,habitat) %>%
ungroup() %>%
gather(origin1,origin,-c(species,Island,Archip,MACDIVCode,habitat)) %>%
filter(origin!="NON-IND" & origin!="IND?" & origin!="??") %>%
dplyr::select(-c(origin1)) -> tmp1
# unique species per plot
tmp1 %>%
mutate(counter=1) %>%
group_by(Island,species,MACDIVCode) %>%
summarise(tot=sum(counter)) %>%
filter(tot==1) %>%
left_join(unique(tmp1)) %>%
group_by(Archip,Island,origin,MACDIVCode,habitat) %>%
summarise(nsp=n_distinct(species)) %>%
ungroup() ->tmp2
# division with totals by plot
tmp1 %>%
group_by(Archip,Island,origin,MACDIVCode) %>%
summarise(tot=n_distinct(species)) %>%
ungroup() %>%
left_join(tmp2) %>%
mutate(prop=nsp/tot) %>%
dplyr::select(Island,tot,nsp,prop,MACDIVCode,habitat,origin) %>%
rename(group=origin) %>%
bind_rows(prop.allsp.plots.habitats) ->prop.allsp.plots.habitats1
writeWorksheetToFile(data=prop.allsp.plots.habitats1,file="/mnt/data1tb/Dropbox/macdiv/betaprops/betatprops.xlsx",
sheet = "summary", header = TRUE,startCol=14,startRow=1,styleAction =XLC$"STYLE_ACTION.NONE")
}
Add the following code to your website.
For more information on customizing the embed code, read Embedding Snippets.