plotting_04_imputed_ci_ggplot.R

# ---
# title: Imputed values and confidence intervals
# author: Michelle María Early Capistrán 
# email:  earlycapistran@comunidad.unam.mx
# date: March 2021
# Script and data info:
#   - This script makes a scatterplot for imputed values with 95% confidence
#     intervals and a scatterplot with all imputed and observed values.
#   - Multiply imputed data is generated by "~R/analysis/cons_mice_analysis.R" and 
#     "~R/analysis/cf_mice_analysis.R" and processed with 
#     "~/R/analysis/imputed_data_wrangling.R"
#   - Observed data were obtained from ecological monitoring (1995-2018) in 
#     Bahía de los Ángeles, Baja California, Mexico by Dr. J. Seminoff, Grupo 
#     Tortuguero de Bahía de los Ángeles, and Comisión Nacional de Áreas 
#     Naturales Protegidas and LEK-derived data from Early-Capistrán et al. 
#     (PeerJ,2020)
# - - -

# Load libraries and packages
library("ggthemes")
library("tidyverse")
library("here")
library("devtools")
load_all("consLettersUtils")

# Source ggplot theme ---------------------------------------------------------
# source("R/functions/theme_cmydas.R")

#  Load data and prepare data -------------------------------------------------
# Store "year" values as label to replace serialized values
year_labels <- seq(1950, 2020, by=5)

# Load observed and imputed data generated by "R/imputed_data_wrangling.R"
obs_imp_data <- readRDS("results/obs_imp_data")

# Calculate confidence intervals
imp_ci <- obs_imp_data  %>% 
  filter(type == "imputed") %>% 
  group_by(yearSerial, dataSource) %>%
  summarise(n = n(), 
            mean = mean(cpue),
            sd = sd(cpue)) %>%
  mutate(se = sd/sqrt(n), # Calculate standard error
         tScore = qt(p = 0.05/2, df = n -1, lower.tail=F),
         ci = tScore * se) %>% 
  suppressWarnings()

# Plot error bars from computed confidence intervals
ggplot(imp_ci, aes(x=yearSerial, y=mean, color=dataSource)) +
  geom_point() +
  geom_pointrange(aes(x=yearSerial, 
                      y = mean, 
                      ymin = mean - ci, 
                      ymax = mean + ci)) +
  labs(y = "CPUE (turtles/12 hr)", 
       x = "Year", 
       color = "Source", 
       shape = "Data type",
       text=element_text(size=5)) +
  theme_cmydas()

# Make plots with all imputed points ------------------------------------------
# You can also view a scatterplot of all values imputed with 'mice'. 
# using "jitter" distribution to avoid overplotting
all_scatter <- ggplot(aes(x=yearSerial, y=cpue), 
                      data=obs_imp_data %>% 
                        filter(type == "imputed")) +
  geom_jitter(alpha = 0.25,  col = "#00BFC4") + 
  labs(y = "CPUE (turtles/12 hr)", x = "Year") +
  theme_cmydas() +
  scale_x_continuous( # Change x-axis label to calendar years
    breaks = seq(-2, 69, by=5),  # 1950 and 1985 in 'year'
    labels = year_labels,
    limits=c(-2, 69) # Adjust to display labels from 1950-1985
  )  
all_scatter
earlycapistran/miceNls documentation built on March 29, 2023, 2:04 a.m.