---
title: "Visualizaations for *Characteristics of macroplastic debris in eight diverse river systems across four continents*"
output: html_document
---

```{r setup, include=FALSE}
knitr::opts_chunk$set(echo = TRUE, results='hide', fig.keep='all', message=FALSE, warning=FALSE)
```

**Packages**
```{r}
library(tidyverse)
library(dplyr)
library(zoo)
library(devtools)
library(fastR2)
library(ggplot2)
library(scales)
library(RColorBrewer)
```

**Datasets**

fig2_data.csv

fig3_data.csv

fig4_data.csv

fig5_data.csv


### Figure 2. Total Debris & Plastic Debris Collected Visualization

*Use fig2_data.csv for simplicity*
```{r}
#Load data
fig2_data <- read_csv("fig2_data.csv")

#Change river_id to class(factor)
fig2_data$river_id <- as.factor(fig2_data$river_id)

#Set levels of river_id factor for proper x-axis order
fig2_data$river_id <- factor(fig2_data$river_id, levels = c("Athi", "Tijuana", "Kingston", "Lat Phrao", "Citarum", "Song Hong", "Portoviejo", "Juan Díaz"))

#Set a coefficient that will be used to scale the secondary y-axis to visually match the primary y-axis
coeff <- 200

#Create figure in ggplot
figure_2 <- ggplot(fig2_data, aes(river_id, mean_kg, fill = debris_type)) + #river_id on x-axis, mean_kg on y-axis, box color filled based on total debris or plastic debris mean
  geom_col(position = position_dodge(1)) + #displays mean debris and mean plastic for each river side by side
  geom_errorbar(aes(x = river_id, ymin = mean_kg - ci95_kg, ymax = mean_kg + ci95_kg), color = "#636363", size = 0.4, width = 0.1, position = position_dodge(1)) + #95% error bars
  geom_point(data = ~filter(.x, debris_type == "plastic_debris"), aes(x = river_id, y = urban_population/coeff), colour = "#636363", fill = "#cd3746", shape = 21, size = 5) + #adds red dot for population size using the coefficient to match to primary x-axis units
  scale_fill_manual(values = c("#09aeb2", "#92e9eb")) + #colors for total debris and plastic debris bars
  theme_classic() +
  theme(axis.text.x = element_blank()) +
  theme(axis.title.x = element_blank()) +
  theme(axis.title.y = element_blank()) + #keeping everything blank because these details are designed in Canva
  theme(axis.text.y = element_text(face = "bold", colour = "#09aeb2")) + #primary x-axis color
  theme(axis.text.y.right = element_text(face = "bold", colour = "#cd3746")) + #secondary y-axis color
  theme(legend.position = "none") + #no legend, made in Canva
  geom_vline(xintercept = c(2.5, 6.5), size = 2, colour = "black", lty = 5) + #vertical dashed black lines dividing the categories of technical complexity groupings
  scale_y_continuous(labels = comma, breaks = seq(0,55000, 5000), limits = c(0, 55000), expand = c(0.01,0.01), sec.axis = sec_axis(~.*coeff, labels = comma, breaks = seq(0,12000000, 1000000))) #primary y-axis labels and creating secondary y-axis with labels
```


### Figure 3. Polymer Composition of Plastic Debris Collected Visualization

*Use fig3_data.csv for simplicity*
```{r}
#Load data
fig3_data <- read_csv("fig3_data.csv")

#Change polymer to class(factor)
fig3_data$polymer <- as.factor(fig3_data$polymer)

#Set levels of polymer factor for proper stacked bar graph order (y-axis)
fig3_data$polymer <- factor(fig3_data$polymer , levels = c("plastic_other", "plastic_ps", "plastic_pp", "plastic_ldpe", "plastic_pvc", "plastic_hdpe", "plastic_pet"))

#Change river_id to class(factor)
fig3_data$river_id <- as.factor(fig3_data$river_id)

#Set levels of river_id factor for proper x-axis order
fig3_data$river_id <- factor(fig3_data$river_id, levels = c("Athi", "Juan Díaz", "Kingston", "Lat Phrao", "Tijuana", "Citarum", "Portoviejo", "Song Hong"))

#Prioritize river_id factor first, then polymer
fig3_data <- fig3_data[order(fig3_data$river_id, fig3_data$polymer),]

#Create figure in ggplot
figure_3 <- ggplot(fig3_data, aes(fill = polymer, y = polymer_proportions, x = river_id, group = polymer)) + #river_id on x-axis, polymer props on y-axis, box color filled based on polymer type, grouped by polymer because we're going to stack the boxes by river_id
  geom_bar(position = "fill", stat = "identity", color = "#636363", linewidth = 0.4, alpha = 0.8) + #stack polymer proportions (y-axis) for each location (x-axis)
  theme_classic() +
  scale_fill_manual(values = c("#C5CDCE", "#125A56", "#238F9D", "#60BCE9", "#ADC2D4", "#F9D576", "#F57634")) + #color blind friendly pallet emphasizing PET with standout color
  scale_y_continuous(labels = percent, breaks = seq(0,1, .1), limits = c(0, 1), expand = c(0.005,0.005)) +
  theme(axis.text.x = element_blank()) +
  theme(axis.title.x = element_blank()) +
  theme(axis.title.y = element_blank()) + #keeping everything blank because these details are designed in Canva
  theme(axis.text.y = element_text(face = "bold")) +
  theme(legend.position = "none") + #no legend, made in Canva
  geom_vline(xintercept = c(3.5, 5.5), size = 2, colour = "black", lty = 5) #vertical dashed black lines dividing the categories of waste picker activity groupings
```


### Figure 4. Proportion of Single-Use Plastic Item Categories in Plastic Debris Collected Visualization

*Use fig4_data.csv for simplicity*
```{r}
#Load data
fig4_data <- read_csv("fig4_data.csv")

#Change item to class(factor)
fig4_data$item <- as.factor(fig4_data$item)

#Set levels of item factor for proper stacked bar graph order (y-axis)
fig4_data$item <- factor(fig4_data$item, levels = c("plastic_item_other", "plastic_item_foam", "plastic_item_bottles", "plastic_item_wrappers", "plastic_item_bags"))

#Change river_id to class(factor)
fig4_data$river_id <- as.factor(fig4_data$river_id)

#Set levels of river_id factor for proper x-axis order
fig4_data$river_id <- factor(fig4_data$river_id, levels = c("Song Hong", "Portoviejo", "Citarum", "Athi"))

#Create figure in ggplot
figure_4 <- ggplot(fig4_data, aes(fill = item, y = item_proportions, x = river_id, group = item)) + #river_id on x-axis, item props on y-axis, box color filled based on item type, grouped by item because we're going to stack the boxes by river_id
  geom_bar(stat = "identity", color = "#636363", linewidth = 0.4, alpha = 0.7) + #stack item proportions (y-axis) for each location (x-axis)
  theme_classic() +
  scale_fill_manual(values = c("#DEDEDE", "#00a37d","#18a94e", "#5eb200","#d6ca00")) + #color blind friendly pallet emphasizing plastic bags with standout color
  theme(axis.text.x = element_blank()) +
  theme(axis.title.x = element_blank()) +
  theme(axis.title.y = element_blank()) +
  theme(legend.position = "none") + #keeping everything blank because these details are designed in Canva
  theme(axis.text.y = element_text(face = "bold", colour = "#d6ca00")) +
  scale_y_continuous(labels = percent, breaks = seq(0,1, .1), limits = c(0, 1.0000000001), expand = c(0.005,0.005)) +
  geom_vline(xintercept = c(1.5), size = 2, colour = "black", lty = 5) #vertical dashed black lines dividing the categories of plastic bag policy groupings
```


### Figure 5. End-of-Life Fate of Plastic Debris Collected Visualization

*Use fig5_data.csv for simplicity*
```{r}
#Load data
fig5_data <- read_csv("fig5_data.csv")

#Change item to class(factor)
fig5_data$fate <- as.factor(fig5_data$fate)

#Set levels of item factor for proper stacked bar graph order (y-axis)
fig5_data$fate <- factor(fig5_data$fate, levels = c("plastic_fate_landfill_incineration", "plastic_fate_energy", "plastic_fate_reused", "plastic_fate_downcycled", "plastic_fate_recycled"))

#Change river_id to class(factor)
fig5_data$river_id <- as.factor(fig5_data$river_id)

#Set levels of river_id factor for proper x-axis order
fig5_data$river_id <- factor(fig5_data$river_id, levels = c("Athi", "Lat Phrao", "Song Hong", "Juan Díaz", "Citarum", "Tijuana", "Kingston", "Portoviejo"))

#Create figure in ggplot
figure_5 <- ggplot(fig5_data, aes(fill = fate, y = fate_proportions, x = river_id, group = fate)) + #river_id on x-axis, fate props on y-axis, box color filled based on fate type, grouped by fate because we're going to stack the boxes by river_id
  geom_bar(position = "fill", stat = "identity", color = "#636363", linewidth = 0.4, alpha = 0.9) + #stack fate proportions (y-axis) for each location (x-axis)
  theme_classic() +
  scale_fill_manual(values = c("#DEDEDE", "#9ecfdc", "#c2c6f6","#acc5f1", "#e5c1ec")) + #color blind friendly pallet emphasizing recycling with standout color
  scale_y_continuous(labels = percent, breaks = seq(0,1, .1), limits = c(0, 1), expand = c(0.005,0.005)) +
  theme(axis.text.x = element_blank()) +
  theme(axis.title.x = element_blank()) +
  theme(axis.title.y = element_blank()) +
  theme(axis.text.y = element_text(face = "bold")) +
  theme(legend.position = "none") #keeping everything blank because these details are designed in Canva
```

**View Visualizations**

*Figure 2. Total Debris & Plastic Debris Collected*
```{r, results='markup'}
figure_2
```

*Figure 3. Polymer Composition of Plastic Debris Collected*
```{r, results='markup'}
figure_3
```

*Figure 4. Proportion of Single-Use Plastic Item Categories in Plastic Debris Collected*
```{r, results='markup'}
figure_4
```

*Figure 5. End-of-Life Fate of Plastic Debris Collected*
```{r, results='markup'}
figure_5
```
