Last updated: 2023-07-26

Checks: 7 0

Knit directory: ms_mariposas_pheno/

This reproducible R Markdown analysis was created with workflowr (version 1.7.0). 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(20230601) 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 af43feb. 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/raw_data/.DS_Store

Untracked files:
    Untracked:  data/best_models_prec.csv
    Untracked:  data/best_models_prec.xlsx
    Untracked:  data/best_models_temp.csv
    Untracked:  data/best_models_temp.xlsx
    Untracked:  data/doy_medio_sp.csv
    Untracked:  data/doy_medio_sp.xlsx
    Untracked:  data/models_prec_scaled.csv
    Untracked:  data/models_prec_scaled.xlsx
    Untracked:  data/models_tmed_scaled.csv
    Untracked:  data/models_tmed_scaled.xlsx

Unstaged changes:
    Modified:   analysis/climate_sensibility.Rmd
    Modified:   analysis/index.Rmd
    Modified:   data/models_prec.csv
    Modified:   data/models_prec.xlsx
    Modified:   data/models_tmed.csv
    Modified:   data/models_tmed.xlsx
    Modified:   data/selected_species.csv

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/analysis_peak.Rmd) and HTML (docs/analysis_peak.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
Rmd af43feb ajpelu 2023-07-26 add peak analysis
Rmd fad23da ajpelu 2023-07-26 update
html fad23da ajpelu 2023-07-26 update
html 51eb91c ajpelu 2023-06-05 Build site.
Rmd 41ef77d ajpelu 2023-06-05 add mean doy vs trend
html c483988 ajpelu 2023-06-05 Build site.
Rmd 2b31d24 ajpelu 2023-06-05 add selected species
html 0e4287f ajpelu 2023-06-02 Build site.
Rmd 7ca3dcc ajpelu 2023-06-02 update
Rmd 1175615 ajpelu 2023-06-02 update peak
Rmd 1418e7a ajpelu 2023-06-01 update computation of trends
Rmd e3a3af1 ajpelu 2023-06-01 add peak analysis
Rmd d06a05b ajpelu 2023-06-01 update

Introduction

library(tidyverse)
library(here)
library(janitor) 
library(lubridate)
library(ggpubr)
library(Kendall)
library(trend)
library(DT)
library(vroom)
df_doy <- vroom::vroom(here::here("data/doy_peak_sps.csv"))
transectos <- vroom::vroom(here::here("data/transectos_metadata.csv"))

Exploratory analysis

First, we examine how the day of the year (doy) at which the maximum peak occurs varies throughout the years for each species and site. We create a custom function to generate a plot for each species faceted by site. All the pdfs generated are stored here.

plot_trend_doy_all <- function(df, sp){ 
  p <- df |> 
    filter(species == sp) |> 
    mutate(abb = fct_reorder(abb_elev, altitud)) |> 
    ggplot(aes(x=year, y=doy)) + 
    geom_point() + 
    facet_wrap(~abb) +
    geom_smooth(method = "lm", se = FALSE) +
    ggpubr::stat_cor(
      cor.coef.name = "R"
      # aes(label = after_stat(r.label))
      ) + 
    theme_bw() +
    theme(
    panel.grid= element_blank(), 
    strip.background = element_blank()) + 
    ylab("Day of year") + xlab("Year") + 
  labs(
    title = sp
  )  +
    scale_x_continuous(breaks = seq(min(df$year), max(df$year), by = 2)) 
  
  return(p)
  }

For instance, the following figure shows the data plot for Aglais urticae

# Get species 
species <- unique(df_doy$species)

plot_trend_doy_all(df_doy, species[1])

Version Author Date
0e4287f ajpelu 2023-06-02
# Generate PDF files for each species using purrr::map()
map(species, ~{
  sp <- .
  p <- plot_trend_doy_all(df_doy, sp)
  pdf_file <- here::here("output/pheno_peak_doy/", paste0(sp, ".pdf"))
  ggsave(filename = pdf_file, plot = p, device = "pdf")
})

How many data?

We would like to determine the number of sites where each species is present (nsites), and also compute the number of sites where the species has been recorded for at least 5 years (nsites_higher4points)

npoints_sp_site <- df_doy |> 
    ungroup() |> 
    group_by(species, site_id, abb_elev) |> 
  summarise(num_points = n())

nsites_by_sps <- npoints_sp_site |> 
  group_by(species) |> 
  summarise(nsites = n())

nsites_by_sps_4points <- npoints_sp_site |>
  filter(num_points > 4) |> 
  group_by(species) |> 
  summarise(nsites_higher4points = n())


ns <- nsites_by_sps |> 
  full_join(nsites_by_sps_4points)

write_csv(ns, here::here("data/trends_peak_nsites.csv"))