R Markdown

This is an R Markdown document. Markdown is a simple formatting syntax for authoring HTML, PDF, and MS Word documents. For more details on using R Markdown see http://rmarkdown.rstudio.com.

When you click the Knit button a document will be generated that includes both content as well as the output of any embedded R code chunks within the document. You can embed an R code chunk like this:

Note that the echo = FALSE parameter was added to the code chunk to prevent printing of the R code that generated the plot.

#update

task 1:

 # Read data files
Coastal_cities_capstone <- read.csv("coast_100000.csv")
microplastic_data_capstone <- read.csv("Marine Microplastic Concentrations.csv")
data <- read.csv("C:/Users/dwvil/Documents/SPRING 2025/CAPSTONE/Microplastic maps/Capstone Edit_AOMI_grid_data.csv")  
coastal_megacities <- Coastal_cities_capstone %>%
  filter(pop_max > 10000000) %>%
  mutate(
    lon = longitude,
    lat = latitude
  )

task 2:

#NOAA BASED DENSITY MAP


# Clean and prepare city data (>10M population only)
cities_clean <- Coastal_cities_capstone %>%
  filter(!is.na(longitude), 
         !is.na(latitude), 
         !is.na(pop_max),
         as.numeric(pop_max) > 10000000) %>%
  mutate(population = as.numeric(pop_max))

# Clean microplastic data with MEASUREMEN focus
microplastic_clean <- microplastic_data_capstone %>%
  filter(!is.na(Latitude), 
         !is.na(Longitude), 
         !is.na(MEASUREMEN),
         MEASUREMEN > 0) %>%
  mutate(
    MEASUREMEN = as.numeric(MEASUREMEN),
    #  categories
    concentration_cat = cut(MEASUREMEN,
                            breaks = c(0, 0.01, 0.1, 1, 10, 100, 1000, 10000, Inf),
                            labels = c("0-0.01", "0.01-0.1", "0.1-1", "1-10", 
                                       "10-100", "100-1k", "1k-10k", "10k+")),
    year = as.numeric(format(as.Date(Date), "%Y"))
  )


















world_map <- map_data("world")

# NOAA Map with fixes
ggplot() +
  # Base world map
  geom_polygon(data = world_map, 
               aes(x = long, y = lat, group = group),
               fill = "lightgray", color = "white", linewidth = 0.1) +
  
  # Microplastic measurements
  geom_point(data = microplastic_clean,
             aes(x = Longitude, y = Latitude, 
                 color = MEASUREMEN, size = MEASUREMEN),
             alpha = 0.7, shape = 16) +
  
  # Coastal megacities
  geom_point(data = coastal_megacities,
             aes(x = lon, y = lat),
             shape = 21, color = "black", fill = NA, size = 3, stroke = 0.8) +
  
  # City labels
  geom_text_repel(
    data = coastal_megacities %>% arrange(-pop_max) %>% head(15),
    aes(x = lon, y = lat, label = nameascii),
    size = 3, force = 0.1, min.segment.length = 0.1, box.padding = 0.3,
    segment.color = "grey40"
  ) +
  
  # Fixed color scale
  scale_color_gradientn(
    name = expression(bold("Microplastics (Particles/m³)")),
    colors = c("#4575b4", "#74add1", "#e0f3f8", "#fee090", "#f46d43", "#d73027"),
    trans = "log10",
    breaks = c(0.001, 0.01, 0.1, 1, 10, 100, 1000, 10000, 1e5),
    labels = c("0.001", "0.01", "0.1", "1", "10", "100", "1k", "10k", "100k"),
    limits = c(0.000676, 8e5)
  ) +
  
  # Size scale
  scale_size_continuous(range = c(1, 12), guide = "none") +
  
  # Coordinates
  coord_fixed(ratio = 1.3, xlim = c(-180, 180), ylim = c(-90, 90)) +
  
  # Improved labels
  labs(
    title = "Global Microplastic Density (NOAA)",
    subtitle = "Coastal megacities (>10M population) marked",
    caption = paste(
      "Data range:", 
      format(min(microplastic_clean$MEASUREMEN, na.rm = TRUE), scientific = FALSE, digits = 3), "-",
      format(max(microplastic_clean$MEASUREMEN, na.rm = TRUE), scientific = FALSE, big.mark = ","), "particles/m³\n",
      "Log10 scale shows exponential concentration differences",
      "\nCities: Top 15 by population"
    ),
    x = NULL, y = NULL
  ) +
  
  theme_minimal() +
  theme(
    panel.background = element_rect(fill = "aliceblue"),
    legend.position = "right",
    plot.title = element_text(face = "bold", hjust = 0.5),
    plot.subtitle = element_text(hjust = 0.5, margin = margin(b = 10)),
    plot.caption = element_text(face = "italic", hjust = 0.5),
    legend.key.height = unit(1.5, "cm"),
    legend.title = element_text(face = "bold")
  )

task 3:

# Required libraries
library(ggplot2)
library(dplyr)
library(maps)
## Warning: package 'maps' was built under R version 4.4.3
## 
## Attaching package: 'maps'
## The following object is masked from 'package:purrr':
## 
##     map
# World map data (from base R maps package)
world_map <- map_data("world")

#  coordinates to region for aggregation
microplastic_clean$region <- map.where("world", microplastic_clean$Longitude, microplastic_clean$Latitude)

# Summarize total microplastic per region
region_summary <- microplastic_clean %>%
  filter(!is.na(region)) %>%
  group_by(region) %>%
  summarise(total_microplastic = sum(MEASUREMEN, na.rm = TRUE)) %>%
  mutate(percent = 100 * total_microplastic / sum(total_microplastic, na.rm = TRUE))

# Merge with map data
map_data_with_percent <- left_join(world_map, region_summary, by = c("region"))

# Plot map
ggplot() +
  geom_polygon(data = map_data_with_percent,
               aes(x = long, y = lat, group = group, fill = percent),
               color = "white", size = 0.2) +
  scale_fill_viridis_c(option = "plasma", na.value = "gray90", 
                       name = "Microplastic %", direction = -1) +
  labs(
    title = "Global Microplastic Distribution by Region",
    subtitle = "Based on total measurement percentages",
    caption = "Data aggregated from marine sampling (regions with microplastic data only)"
  ) +
  coord_fixed(1.3) +
  theme_minimal() +
  theme(
    plot.title = element_text(face = "bold", hjust = 0.5),
    plot.subtitle = element_text(hjust = 0.5),
    axis.title = element_blank(),
    axis.text = element_blank(),
    axis.ticks = element_blank()
  )
## Warning: Using `size` aesthetic for lines was deprecated in ggplot2 3.4.0.
## ℹ Please use `linewidth` instead.
## This warning is displayed once every 8 hours.
## Call `lifecycle::last_lifecycle_warnings()` to see where this warning was
## generated.