Last updated: 2021-05-20

Checks: 7 0

Knit directory: booksn_dispersantes/

This reproducible R Markdown analysis was created with workflowr (version 1.6.2). The Checks tab describes the reproducibility checks that were applied when the results were created. The Past versions tab lists the development history.


Great! Since the R Markdown file has been committed to the Git repository, you know the exact version of the code that produced these results.

Great job! The global environment was empty. Objects defined in the global environment can affect the analysis in your R Markdown file in unknown ways. For reproduciblity it’s best to always run the code in an empty environment.

The command set.seed(20210428) was run prior to running the code in the R Markdown file. Setting a seed ensures that any results that rely on randomness, e.g. subsampling or permutations, are reproducible.

Great job! Recording the operating system, R version, and package versions is critical for reproducibility.

Nice! There were no cached chunks for this analysis, so you can be confident that you successfully produced the results during this run.

Great job! Using relative paths to the files within your workflowr project makes it easier to run your code on other machines.

Great! You are using Git for version control. Tracking code development and connecting the code version to the results is critical for reproducibility.

The results in this page were generated with repository version 0bb400c. See the Past versions tab to see a history of the changes made to the R Markdown and HTML files.

Note that you need to be careful to ensure that all relevant files for the analysis have been committed to Git prior to generating the results (you can use wflow_publish or wflow_git_commit). workflowr only checks the R Markdown file, but you know if there are other scripts or data files that it depends on. Below is the status of the Git repository when the results were generated:


Ignored files:
    Ignored:    .Rhistory
    Ignored:    .Rproj.user/
    Ignored:    data/.DS_Store

Unstaged changes:
    Modified:   data/passerine_ab.csv
    Modified:   output/abundance_time/passerine_abtotal.csv
    Modified:   output/abundance_time/passerine_abtotal_compare.csv
    Modified:   output/betadiversity/betapart.csv
    Modified:   output/betadiversity/plot_betadiversity.pdf
    Modified:   output/betadiversity/tbi.csv
    Modified:   output/compose_plot.pdf
    Modified:   output/diversity/alfa_diversity.csv
    Modified:   output/diversity/diversity_habitat.pdf
    Modified:   output/diversity/richness.csv
    Modified:   output/mktrend/plot_mktrend.pdf

Note that any generated files, e.g. HTML, png, CSS, etc., are not included in this status report because it is ok for generated content to have uncommitted changes.


These are the previous versions of the repository in which changes were made to the R Markdown (analysis/diversity.Rmd) and HTML (docs/diversity.html) files. If you’ve configured a remote Git repository (see ?wflow_git_remote), click on the hyperlinks in the table below to view the files as they were in that past version.

File Version Author Date Message
html b847df4 Antonio J Perez-Luque 2021-05-20 Build site.
Rmd 6724023 Antonio J Perez-Luque 2021-05-20 filter Delichon urbicum
html 4d72c1e Antonio J Perez-Luque 2021-05-10 Build site.
Rmd dee25a5 Antonio J Perez-Luque 2021-05-10 update diversity index
html 684a3f9 Antonio J Perez-Luque 2021-05-07 Build site.
html ce17a57 Antonio J Perez-Luque 2021-05-07 Build site.
Rmd 73addc3 Antonio J Perez-Luque 2021-05-07 add richness analysis
html dad5db5 Antonio J Perez-Luque 2021-05-06 Build site.
Rmd 4ef40a7 Antonio J Perez-Luque 2021-05-06 compare diversity by temporal steps
html 12af27d Antonio J Perez-Luque 2021-05-03 Build site.
Rmd 5306189 Antonio J Perez-Luque 2021-05-03 compute diversity (alpha) analysis

Diversity analysis

  • Compute Simpson and Shannon diversity index. See diversity functions in vegan pkg.
knitr::opts_chunk$set(echo = TRUE, 
                      warning = FALSE, 
                      message = FALSE)
library("tidyverse")
library("here")
library("vegan")
passerine <- read_csv(here::here("data/passerine.csv")) 
passerine_abbreviations <- read_csv(here::here("data/passerine_abbrev.csv")) 


passerine.ab <- passerine %>% 
  rename(sp = sp.abb) %>% 
  dplyr::select(-especie) %>% 
  group_by(sp, year, habitat) %>% 
  summarise(ab_avg = round(mean(den, na.rm=TRUE),2), 
            sd = sd(den, na.rm = TRUE), 
            se = sd/sqrt(length(den)), 
            n = length(den))

df <- passerine.ab %>% 
  unite("site", habitat:year, sep="_", remove = FALSE) %>% 
  dplyr::select(-sd, -se, -n) %>% 
  pivot_wider(names_from = sp, 
              values_from = ab_avg, 
              values_fill = 0)

Create a function to compute Simpson and Shannon diversity indices

computeDiversity <- function(d, hab, ...){
  
  df.habitat <- d %>% 
    filter(habitat == hab) %>% 
  dplyr::select(-habitat, -site) %>% 
  dplyr::select_if(
    negate(function(x) is.numeric(x) && sum(x) == 0)) %>%
    column_to_rownames("year")
  

  shannon <- diversity(df.habitat, "shannon") %>%
    as.data.frame() %>% rownames_to_column("year") %>%
    rename(shannon = starts_with("."))

  simpson <- diversity(df.habitat, "simpson") %>%
    as.data.frame() %>%
    rownames_to_column("year") %>%
    rename(simpson = starts_with("."))

  diver <- shannon %>%
  inner_join(simpson) %>%
  mutate(habitat = hab)
  
  return(diver)
}
  • Compute diversity index for each site
diver.summits <- computeDiversity(df, "cumbres")
diver.juniper <- computeDiversity(df, "enebral")
diver.robledal <- computeDiversity(df, "robledal")

diversity_index <- bind_rows(diver.summits, diver.juniper, diver.robledal)
write_csv(diversity_index, here::here("output/diversity/alfa_diversity.csv"))
diversity_plot <- diversity_index %>% 
  filter(!(habitat == "cumbres" & year == 2013)) %>% 
  ggplot(
  aes(x=year, y=shannon, colour=habitat, group=habitat)) +
    geom_point() + geom_line() +
  geom_smooth(method = "lm", 
              se=FALSE, size = .5, linetype="dashed") + 
  ylab("Shannon Index (H')") + 
  theme_bw() + 
  theme(
    panel.grid = element_blank(),
    #panel.grid.major.x = element_blank(),
    strip.background = element_blank()
  )


ggsave(here::here("output/diversity/diversity_habitat.pdf"),
       width = 18, height = 12, units = "cm")
diversity_plot

Version Author Date
b847df4 Antonio J Perez-Luque 2021-05-20
12af27d Antonio J Perez-Luque 2021-05-03
dev.off()
null device 
          1 

Compare three temporal times

  • Generate three temporal times: 1983; 2009: mean for 2008 - 2009; and 2018: mean for 2017 - 2018.
shannon <- diversity_index %>% 
  filter(year %in% c(1981:1985, 2008:2009, 2017:2018)) %>% 
  mutate(yearF = case_when(
    year %in% c(1981:1985) ~ as.character(1983), 
    year %in% c(2008:2009) ~ as.character(2009),
    year %in% c(2017:2018) ~ as.character(2018)
  )) %>% 
  group_by(yearF, habitat) %>% 
  summarise(mean = mean(shannon, na.rm=TRUE), 
            sd = sd(shannon, na.rm=TRUE),
            se = sd/sqrt(length(shannon)))

shannon %>% 
  ggplot(aes(x=yearF, y=mean, group=1)) + 
  geom_point() +
  geom_line() + 
  # geom_bar(stat="identity", fill="black") + 
  geom_errorbar(aes(ymax = mean + se, ymin= mean - se), 
                width = .2, size = .3) + 
  facet_wrap(~habitat, ncol = 1) + 
  theme_bw() +
  theme(
    panel.grid.minor = element_blank(),
    strip.background = element_blank()
  ) + 
  ylab("Shannon Index (H')") + xlab("")

Version Author Date
b847df4 Antonio J Perez-Luque 2021-05-20
dad5db5 Antonio J Perez-Luque 2021-05-06

Richness

richness <- passerine.ab %>% 
  group_by(habitat, year) %>% 
  summarise(richness = length(sp))
write_csv(richness, here::here("output/diversity/richness.csv"))
richness %>% 
  ggplot(aes(as.factor(year), richness)) +
  geom_point() + 
  geom_path(aes(group=habitat)) + 
  facet_wrap(~habitat, nrow = 3) + 
  ylab("Richness (n sps)") + xlab("") + 
  theme_bw() + 
  theme(
    panel.grid.minor = element_blank(),
    strip.background = element_blank()
  )

Version Author Date
b847df4 Antonio J Perez-Luque 2021-05-20
ce17a57 Antonio J Perez-Luque 2021-05-07
richness %>% 
  filter(year %in% c(1981:1985, 2008:2009, 2017:2018)) %>% 
  mutate(yearF = case_when(
    year %in% c(1981:1985) ~ as.character(1983), 
    year %in% c(2008:2009) ~ as.character(2009),
    year %in% c(2017:2018) ~ as.character(2018)
  )) %>% 
  group_by(yearF, habitat) %>% 
  summarise(mean = mean(richness, na.rm=TRUE), 
            sd = sd(richness, na.rm=TRUE),
            se = sd/sqrt(length(richness))) %>% 
  ggplot(aes(x=yearF, y=mean, group=1)) + 
  geom_point() +
  geom_line() + 
  # geom_bar(stat="identity", fill="black") + 
  geom_errorbar(aes(ymax = mean + se, ymin= mean - se), 
                width = .2, size = .3) + 
  facet_wrap(~habitat, ncol = 1) + 
  theme_bw() +
  theme(
    panel.grid.minor = element_blank(),
    strip.background = element_blank()
  ) 

Version Author Date
b847df4 Antonio J Perez-Luque 2021-05-20
4d72c1e Antonio J Perez-Luque 2021-05-10

sessionInfo()
R version 4.0.2 (2020-06-22)
Platform: x86_64-apple-darwin17.0 (64-bit)
Running under: macOS Catalina 10.15.3

Matrix products: default
BLAS:   /Library/Frameworks/R.framework/Versions/4.0/Resources/lib/libRblas.dylib
LAPACK: /Library/Frameworks/R.framework/Versions/4.0/Resources/lib/libRlapack.dylib

locale:
[1] en_US.UTF-8/en_US.UTF-8/en_US.UTF-8/C/en_US.UTF-8/en_US.UTF-8

attached base packages:
[1] stats     graphics  grDevices utils     datasets  methods   base     

other attached packages:
 [1] vegan_2.5-7     lattice_0.20-41 permute_0.9-5   here_1.0.1     
 [5] forcats_0.5.1   stringr_1.4.0   dplyr_1.0.4     purrr_0.3.4    
 [9] readr_1.4.0     tidyr_1.1.2     tibble_3.0.6    ggplot2_3.3.3  
[13] tidyverse_1.3.0 workflowr_1.6.2

loaded via a namespace (and not attached):
 [1] Rcpp_1.0.6        lubridate_1.7.10  assertthat_0.2.1  rprojroot_2.0.2  
 [5] digest_0.6.27     R6_2.5.0          cellranger_1.1.0  backports_1.2.1  
 [9] reprex_1.0.0      evaluate_0.14     highr_0.8         httr_1.4.2       
[13] pillar_1.4.7      rlang_0.4.10      readxl_1.3.1      rstudioapi_0.13  
[17] whisker_0.4       jquerylib_0.1.3   Matrix_1.3-2      rmarkdown_2.6.6  
[21] labeling_0.4.2    splines_4.0.2     munsell_0.5.0     broom_0.7.4      
[25] compiler_4.0.2    httpuv_1.5.5      modelr_0.1.8      xfun_0.20        
[29] pkgconfig_2.0.3   mgcv_1.8-33       htmltools_0.5.1.1 tidyselect_1.1.0 
[33] crayon_1.4.1      dbplyr_2.1.0      withr_2.4.1       later_1.1.0.1    
[37] MASS_7.3-53       grid_4.0.2        nlme_3.1-152      jsonlite_1.7.2   
[41] gtable_0.3.0      lifecycle_1.0.0   DBI_1.1.1         git2r_0.28.0     
[45] magrittr_2.0.1    scales_1.1.1      cli_2.3.0         stringi_1.5.3    
[49] farver_2.0.3      fs_1.5.0          promises_1.2.0.1  xml2_1.3.2       
[53] bslib_0.2.4       ellipsis_0.3.1    generics_0.1.0    vctrs_0.3.6      
[57] tools_4.0.2       glue_1.4.2        hms_1.0.0         parallel_4.0.2   
[61] yaml_2.2.1        colorspace_2.0-0  cluster_2.1.0     rvest_0.3.6      
[65] knitr_1.31        haven_2.3.1       sass_0.3.1