# ---
# title: Mice ggplot
# author: Michelle María Early Capistrán
# email: earlycapistran@comunidad.unam.mx
# date: March 2021
# Script and data info:
# - This script plots observed and multiply imputed values and trend lines
# using results from ~/R/cf_mice_analysis.R and ~/R/cons_mice_analysis.R
# - Trend lines and confidence intervals are plotted with results from
# ~/R/mice_pred_ci.R
# - Data consists of green turtle (Chelonia mydas) CPUE data from monitoring
# and derived from LEK.
# - 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 is from Early-Capistrán et al.
# (PeerJ, 2020)
# - Multiply imputed data is generated by "~R/cons_mice_analysis.R" and
# "~R/cf_mice_analysis.R" and processed with "~/R/imputed_data_wrangling.R"
# - - -
# Install and load libraries --------------------------------------------------
# Check if required libraries are installed and install
# if necessary
# packages <- c("ggthemes", "patchwork", "tidyverse", "here")
#
# if (length(setdiff(packages, rownames(installed.packages()))) > 0) {
# install.packages(setdiff(packages, rownames(installed.packages())))
# }
#
# devtools::install('consLettersUtils')
# 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 -------------------------------------------------
# Load observed and imputed data points from "~/R/imputed_data_wrangling.R"
obs_imp_data <- readRDS("results/obs_imp_data")
# Load pooled predicted values and confidence bounds
# Load observed and imputed data points generated with
# "~/R/imputed_data_wrangling.R"
obs_imp_data <- readRDS("results/obs_imp_data")
# Load pooled predicted values and confidence bounds generated with
# "~/R/mice_pred_ci.R"
cf_pred_ci <- readRDS("results/cf_pred_ci")
cons_pred_ci <- readRDS("results/cons_pred_ci")
# Start plotting -------------------------------------------------------------
# Store "year" values as label to replace serialized values
year_labels <- seq(1950, 2020, by=5)
# Plot pooled predicted values and confidence bounds
mice_ci <- ggplot()+
geom_line(data = cons_pred_ci, aes(x = yearSerial, y = means),
color = "darkgrey") +
geom_line(data = cons_pred_ci, aes(x = yearSerial, y = lwr),
linetype = 2, color = "darkgrey") +
geom_line(data = cons_pred_ci, aes(x = yearSerial, y = upr),
linetype = 2, color = "darkgrey") +
geom_line(data = cf_pred_ci, aes(x = yearSerial, y = means),
color = "darkgrey") +
geom_line(data = cf_pred_ci, aes(x = yearSerial, y = lwr),
linetype = 2, color = "darkgrey") +
geom_line(data = cf_pred_ci, aes(x = yearSerial, y = upr),
linetype = 2, color = "darkgrey")
mice_ci
# Add scatter plot of observed values and mean imputed values
impMeanScatter <- mice_ci +
geom_point(data= obs_imp_data %>%
filter(type == "observed" | type == "imputed_means"),
aes(x = yearSerial, y = cpue, shape = type, color = dataSource)) +
scale_shape_manual(labels = c("Mean imputed values", "Observed values"),
values = c(1, 16)) +
labs(y = "CPUE (turtles/12 hr)",
x = "Year",
color = "Source",
shape = "Data type",
text=element_text(size=5)) +
scale_color_tableau() +
theme_cmydas()
impMeanScatter
# Add marginal rug plots to show density distribution of imputed values
miceRug <- impMeanScatter +
geom_rug(data=obs_imp_data %>% # Rug plot for imputed monitoring data
filter(type == "imputed" & dataSource == "Monitoring"),
aes(yearSerial, cpue),
alpha = 0.05,
position = "jitter",
color = "#F28E2B",
sides ="r",
outside = FALSE) +
geom_rug(data=obs_imp_data %>% # Rug plot imputed for LEK data
filter(type == "imputed" & dataSource == "LEK"),
aes(yearSerial, cpue),
alpha = 0.05,
position = "jitter",
color = "#4E79A7",
sides ="l",
outside = FALSE) +
coord_cartesian(clip = "off") +
theme(plot.margin = margin(0.5, 1.0, 0.5, 0.5, "cm"))
miceRug
# Change x-axis labels to calendar years
miceRug <- miceRug +
scale_x_continuous(
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
)
miceRug
# Save plot as svg
ggsave("figures/trend_plot.svg", device = svg)
Add the following code to your website.
For more information on customizing the embed code, read Embedding Snippets.