#By Theoneste RUTAYISIRE, R codes with charts 
#Individual Assignment: Data Visualization
#1. Data set Overview
#Information on volcanic eruptions, events, sulfur contents, tree rings, and volcano locations are all included in the dataset, which comes from the TidyTuesday project. 
#Key variables:
# volcano_name: The volcano's name.
# vei: Index of Volcanic Explosivity.
#start_year, end_year: Eruptive times.
#The number of people who reside within a given radius of a volcano is indicated by the variables population_within_5_km, population_within_10_km, etc.
#sulfur_concentration: Sulfur concentration in the atmosphere.
#Europe_temp_index: Tree-ring-based temperature index.)
#Data quality: Some missing data and some need reprocessing, I think data quality is really important. 

{r} # 2&3 Data Handling Section& Visualization Section

rm(list = ls())
library(tidyr)
library(dplyr)

Attaching package: 'dplyr'
The following objects are masked from 'package:stats':

    filter, lag
The following objects are masked from 'package:base':

    intersect, setdiff, setequal, union
library(ggplot2)
library(ggmap)
ℹ Google's Terms of Service: <https://mapsplatform.google.com>
  Stadia Maps' Terms of Service: <https://stadiamaps.com/terms-of-service/>
  OpenStreetMap's Tile Usage Policy: <https://operations.osmfoundation.org/policies/tiles/>
ℹ Please cite ggmap if you use it! Use `citation("ggmap")` for details.
library(maps)
library(scales)
library(ggplot2)
library(patchwork) 
library(forcats)
#Datasets from assignment, eruption downloaded from github raw data
eruptions_url <- "https://raw.githubusercontent.com/rfordatascience/tidytuesday/main/data/2020/2020-05-12/eruptions.csv"
events_url <- "https://raw.githubusercontent.com/rfordatascience/tidytuesday/main/data/2020/2020-05-12/events.csv"
sulfur_url <- "https://raw.githubusercontent.com/rfordatascience/tidytuesday/main/data/2020/2020-05-12/sulfur.csv"
tree_rings_url <- "https://raw.githubusercontent.com/rfordatascience/tidytuesday/main/data/2020/2020-05-12/tree_rings.csv"
volcano_url <- "https://raw.githubusercontent.com/rfordatascience/tidytuesday/main/data/2020/2020-05-12/volcano.csv"
eruptions <- read.csv(eruptions_url)
events <- read.csv(events_url)
sulfur <- read.csv(sulfur_url)
tree_rings <- read.csv(tree_rings_url)
volcano <- read.csv(volcano_url)
# Activity 1: Summarized eruption counts by volcano name
eruption_summary <- eruptions %>%
  group_by(volcano_name) %>%
  summarize(
    total_eruptions = n(),
    avg_vei = mean(as.numeric(vei), na.rm = TRUE)
  ) %>%
  arrange(desc(total_eruptions))

print(eruption_summary)
# A tibble: 921 × 3
   volcano_name           total_eruptions avg_vei
   <chr>                            <int>   <dbl>
 1 Etna                               241    1.88
 2 Fournaise, Piton de la             194    1.18
 3 Asosan                             186    1.88
 4 Villarrica                         164    1.78
 5 Asamayama                          147    2.03
 6 Katla                              132    3.66
 7 Klyuchevskoy                       111    2.05
 8 Mauna Loa                          110    0.1 
 9 Merapi                             110    2.09
10 Izu-Oshima                         108    1.95
# ℹ 911 more rows
# Plotted volcanoes with the most eruptions
top_volcanoes <- eruption_summary %>%
  slice_max(order_by = total_eruptions, n = 10) 

ggplot(top_volcanoes, aes(x = reorder(volcano_name, -total_eruptions), y = total_eruptions)) +
  geom_bar(stat = "identity", fill = "steelblue", width = 0.7) + 
  labs(
    title = "Top 10 Volcanoes by Total Eruptions",
    x = "Volcano Name",
    y = "Total Eruptions"
  ) +
  theme_minimal() +
  theme(
    plot.title = element_text(size = 10, margin = margin(t = 2, b = 2), family = "serif"), # Title font
    axis.title.x = element_text(size = 10, margin = margin(t = 1), family = "serif"), # X-axis title font
    axis.title.y = element_text(size = 10, margin = margin(r = 2), family = "serif"), # Y-axis title font
    axis.text.x = element_text(size = 10, angle = 45, hjust = 1, margin = margin(t = 2), family = "serif"), # X-axis text font
    axis.text.y = element_text(size = 10, margin = margin(r = 5), family = "serif"), # Corrected y-axis font size
    panel.background = element_rect(fill = "white", color = "black", size = 0.1), # Add border around panel
    plot.background = element_rect(fill = "white", color = "black", size = 0.1),   # Add border around plot
    plot.margin = margin(t = 1, r = 2, b = 3, l = 3)  # Added bottom and left margin
  ) +
  scale_x_discrete(labels = function(x) sprintf("%s", x)) +  # Allow font size customization on x-axis
  scale_y_continuous(expand = expansion(mult = 0.1), labels = scales::comma) +  # Removed incorrect guide_axis argument
  annotate("text", y = Inf,x = Inf , label = "b)", hjust = 1.2, vjust = 1.2, size = 4, fontface = "bold", family = "serif")  # Add plot label (b) in top-right corner
Warning: The `size` argument of `element_rect()` is deprecated as of ggplot2 3.4.0.
ℹ Please use the `linewidth` argument instead.

# Activity 2: Summarize volcanic events by type (grouped into 15 categories)
event_summary <- events %>%
  group_by(event_type) %>%
  summarize(
    total_events = n()
  ) %>%
  arrange(desc(total_events)) %>%
  mutate(event_type = ifelse(row_number() > 14, "Other", event_type)) %>%  # Group all except top 14 as "Other"
  group_by(event_type) %>%
  summarize(total_events = sum(total_events)) %>%  # Recalculate counts for "Other"
  arrange(desc(total_events))

print(event_summary)
# A tibble: 15 × 2
   event_type              total_events
   <chr>                          <int>
 1 VEI (Explosivity Index)         9194
 2 Explosion                       7825
 3 Other                           7136
 4 Ash                             4110
 5 Lava flow(s)                    3195
 6 Earthquakes (undefined)         1777
 7 Phreatic activity               1508
 8 Pyroclastic flow                1399
 9 Property damage                 1079
10 Lahar or mudflow                 785
11 Pumice                           716
12 Scoria                           682
13 Lava dome formation              668
14 Blocks                           632
15 Lapilli                          616
# Plot of distributed  event types (grouped into categories)
ggplot(event_summary, aes(x = reorder(event_type, -total_events), y = total_events)) +
  geom_bar(stat = "identity", fill = "darkorange", width = 0.7) + 
  labs(
    title = "Distribution of Volcanic Event Types",
    x = "Event Type",
    y = "Total Events"
  ) +
  theme_minimal() +
  theme(
    plot.title = element_text(size = 8, margin = margin(t = 5, b = 10), family = "serif", face = "bold"), # Enhanced title
    axis.title.x = element_text(size = 8, margin = margin(t = 5), family = "serif"), # X-axis title font
    axis.title.y = element_text(size = 8, margin = margin(r = 5), family = "serif"), # Y-axis title font
    axis.text.x = element_text(size = 8, angle = 45, hjust = 1, family = "serif"), # X-axis text font
    axis.text.y = element_text(size = 8, family = "serif"), # Y-axis text font
    panel.grid.major = element_blank(),  # Remove major grid lines for a cleaner look
    panel.grid.minor = element_blank(),  # Remove minor grid lines
    panel.border = element_blank(),  # Remove panel border
    panel.background = element_blank(),  # Remove panel background
    plot.background = element_blank(),   # Remove overall plot background
    plot.margin = margin(t = 5, r = 10, b = 5, l = 10)  # Proper spacing for clean alignment
  ) +
  scale_x_discrete(labels = function(x) sprintf("%s", x)) +  # Ensure clean x-axis labels
  scale_y_continuous(expand = expansion(mult = 0.05), labels = scales::comma) +  # Adjust y-axis spacing
  annotate("text", x = Inf, y = Inf, label = "b)", hjust = 1.2, vjust = 1.2, size = 6, fontface = "bold", family = "serif")  # Customizable plot label in top-right corner

#Activity 3:Sulfur emissions over time
sulfur_long <- sulfur %>%
  pivot_longer(cols = c(neem, wdc), names_to = "source", values_to = "sulfur_concentration")

print(sulfur_long)
# A tibble: 4,504 × 3
    year source sulfur_concentration
   <dbl> <chr>                 <dbl>
 1  706. neem                  13.6 
 2  706. wdc                   14.6 
 3  706. neem                  14.4 
 4  706. wdc                   12.8 
 5  706. neem                  15.8 
 6  706. wdc                   11.3 
 7  706. neem                  17.4 
 8  706. wdc                    9.87
 9  706. neem                  18.3 
10  706. wdc                    8.78
# ℹ 4,494 more rows
# Define the label (e.g., "a)") and its properties
plot_label <- "a)"  # Change this for different labels
label_font_size <- 6  # Adjust the font size
label_font_family <- "serif"  # Adjust font family
# Plot sulfur concentration over time
ggplot(sulfur_long, aes(x = year, y = sulfur_concentration, color = source)) +
  geom_line(size = 1) +
  labs(
    title = "Sulfur Concentration Over Time",
    x = "Year",
    y = "Sulfur Concentration (arbitrary units)"
  ) +
  theme_minimal() +
  scale_color_manual(values = c("blue", "red")) +
  scale_x_continuous(expand = expansion(mult = c(0.02, 0.02))) +  # Adds slight margin to x-axis
  scale_y_continuous(expand = expansion(mult = c(0.05, 0.05))) +  # Adds slight margin to y-axis
  theme(
    plot.title = element_text(size = 10, margin = margin(t = 10, b = 12), family = "serif", face = "bold"),
    axis.title.x = element_text(size = 10, margin = margin(t = 8), family = "serif"),
    axis.title.y = element_text(size = 10, margin = margin(r = 8), family = "serif"),
    axis.text.x = element_text(size = 9, family = "serif", margin = margin(t = 3)),
    axis.text.y = element_text(size = 9, family = "serif", margin = margin(r = 3)),
    legend.title = element_blank(),
    legend.text = element_text(size = 9, family = "serif"),
    plot.margin = margin(10, 10, 10, 10)  # Adjusts overall plot margins
  ) +
  # Add customizable plot label in the top-right corner
  annotate(
    "text", x = max(sulfur_long$year) + 1, y = max(sulfur_long$sulfur_concentration) + 0.1,
    label = plot_label, hjust = 1, vjust = 1,
    size = label_font_size, family = label_font_family, fontface = "bold"
  ) +
  annotate("text", y = Inf, x = Inf, label = "b)", hjust = 1.2, vjust = 1.2, size = 4, fontface = "bold", family = "serif")  # Add plot label (b) in top-right corner
Warning: Using `size` aesthetic for lines was deprecated in ggplot2 3.4.0.
ℹ Please use `linewidth` instead.
Warning: Removed 580 rows containing missing values or values outside the scale range
(`geom_line()`).
Warning: Removed 1 row containing missing values or values outside the scale range
(`geom_text()`).

# Activity 4: To analyze tree ring data and its relationship with European temperature index
tree_summary <- tree_rings %>%
  group_by(year) %>%
  summarize(
    avg_temp_index = mean(europe_temp_index, na.rm = TRUE),
    total_trees = sum(n_tree, na.rm = TRUE)
  )
# Define the label (e.g., "a)") and its properties
plot_label <- "a)"  # Customizable label text
label_font_size <- 5  # Adjust the font size
label_font_family <- "serif"  # Adjust font family

# Get the maximum x (year) and y (temperature index) values for positioning
max_year <- max(tree_summary$year, na.rm = TRUE)
max_temp_index <- max(tree_summary$avg_temp_index, na.rm = TRUE)
# Plot tree ring data over time with top-right label
ggplot(tree_summary, aes(x = year)) +
  geom_line(aes(y = avg_temp_index, color = "Average Temperature Index"), size = 1) +
  geom_line(aes(y = total_trees / 100, color = "Total Trees (scaled)"), size = 1, linetype = "dashed") +
  scale_y_continuous(
    name = "Average Temperature Index",
    sec.axis = sec_axis(~ . * 100, name = "Total Trees")
  ) +
  labs(
    title = "Tree Rings and European Temperature Index",
    x = "Year"
  ) +
  theme_minimal() +
  scale_color_manual(values = c("#2596be", "purple")) +
  scale_x_continuous(expand = expansion(mult = c(0.02, 0.02))) +  # Adds slight margin to x-axis
  scale_y_continuous(expand = expansion(mult = c(0.05, 0.05))) +  # Adds slight margin to y-axis
  theme(
    plot.title = element_text(size = 10, margin = margin(t = 10, b = 15), family = "serif", face = "bold"),
    axis.title.x = element_text(size = 10, margin = margin(t = 8), family = "serif"),
    axis.title.y = element_text(size = 10, margin = margin(r = 8), family = "serif"),
    axis.text.x = element_text(size = 9, family = "serif", margin = margin(t = 3)),
    axis.text.y = element_text(size = 9, family = "serif", margin = margin(r = 3)),
    legend.title = element_blank(),
    legend.text = element_text(size = 9, family = "serif"),
    plot.margin = margin(10, 10, 10, 10)  # Adjusts overall plot margins
  ) +
  # Add customizable plot label in the top-right corner
  annotate(
    "text", x = max_year, y = max_temp_index, 
    label = plot_label, hjust = 1.2, vjust = -0.5,
    size = label_font_size, family = label_font_family, fontface = "bold"
  )
Scale for y is already present.
Adding another scale for y, which will replace the existing scale.
Warning: Removed 1 row containing missing values or values outside the scale range
(`geom_line()`).
Removed 1 row containing missing values or values outside the scale range
(`geom_line()`).

# Activity 5: To analyze population within different distances of volcanoes
population_summary <- volcano %>%
  summarize(
    avg_population_5km = mean(population_within_5_km, na.rm = TRUE),
    avg_population_10km = mean(population_within_10_km, na.rm = TRUE),
    avg_population_30km = mean(population_within_30_km, na.rm = TRUE),
    avg_population_100km = mean(population_within_100_km, na.rm = TRUE)
  )

# Data for visualization
population_df <- data.frame(
  distance = c("5 km", "10 km", "30 km", "100 km"),
  population = c(
    population_summary$avg_population_5km,
    population_summary$avg_population_10km,
    population_summary$avg_population_30km,
    population_summary$avg_population_100km
  )
)
# Plotting average population within different distances
ggplot(population_df, aes(x = distance, y = population)) +
  geom_bar(stat = "identity", fill = "#2596be", width = 0.6) +
  labs(
    title = "Average Population Within Different Distances of Volcanoes",
    x = "Distance from Volcano",
    y = "Average Population"
  ) +
  theme_minimal() +
  theme(
    plot.title = element_text(size = 10, margin = margin(t = 5, b = 5), family = "serif", face = "bold"),
    axis.title.x = element_text(size = 10, margin = margin(t = 5), family = "serif"),
    axis.title.y = element_text(size = 10, margin = margin(r = 5), family = "serif"),
    axis.text.x = element_text(size = 10, family = "serif"),
    axis.text.y = element_text(size = 10, family = "serif"),
    panel.background = element_rect(fill = "white", color = "black", size = 0.5),
    plot.background = element_rect(fill = "white", color = "black", size = 0.5),
    plot.margin = margin(t = 2, r = 2, b = 2, l = 3)
  ) +
  scale_y_continuous(
    expand = expansion(mult = 0.1), 
    labels = scales::comma,
    breaks = seq(0, max(population_df$population, na.rm = TRUE), length.out = 4) # Adding scale breaks
  ) +
  scale_x_discrete(labels = function(x) paste0(x, " Radius")) + # Adding more descriptive x-axis labels
  geom_text(
    aes(label = scales::comma(population)), 
    vjust = -0.5, 
    size = 4, 
    family = "serif"
  ) +  # Annotate bars with population values
  annotate(
    "text", 
    x = 4, 
    y = max(population_df$population, na.rm = TRUE) * 0.9, 
    label = "Data Source: Volcanic Activity Database", 
    size = 1.5, 
    fontface = "italic", 
    family = "serif",
    color = "gray60"
  )  # Adding a data source annotation

# Activity 6: To analyze population within different distances of volcanoes
population_summary <- volcano %>%
  summarize(
    avg_population_5km = mean(population_within_5_km, na.rm = TRUE),
    avg_population_10km = mean(population_within_10_km, na.rm = TRUE),
    avg_population_30km = mean(population_within_30_km, na.rm = TRUE),
    avg_population_100km = mean(population_within_100_km, na.rm = TRUE)
  )
# Prepare data for visualization
population_df <- data.frame(
  distance = c("5 km", "10 km", "30 km", "100 km"),
  population = c(
    population_summary$avg_population_5km,
    population_summary$avg_population_10km,
    population_summary$avg_population_30km,
    population_summary$avg_population_100km
  )
)
# Average population within different distances
ggplot(population_df, aes(x = distance, y = population)) +
  geom_bar(stat = "identity", fill = "#2596be", width = 0.6) +
  labs(
    title = "Average Population Within Different Distances of Volcanoes",
    x = "Distance from Volcano",
    y = "Average Population"
  ) +
  theme_minimal() +
  theme(
    plot.title = element_text(size = 10, margin = margin(t = 6, b = 6), family = "serif", face = "bold"),
    axis.title.x = element_text(size = 10, margin = margin(t = 6), family = "serif"),
    axis.title.y = element_text(size = 10, margin = margin(r = 8), family = "serif"),
    axis.text.x = element_text(size = 10, family = "serif", face = "bold"),  # Increase X-axis font size
    axis.text.y = element_text(size = 10, family = "serif", face = "bold"),  # Increase Y-axis font size
    panel.background = element_rect(fill = "white", color = "black", size = 0.5),
    plot.background = element_rect(fill = "white", color = "black", size = 0.5),
    plot.margin = margin(t = 3, r = 3, b = 3, l = 3)
  ) +
  scale_y_continuous(
    expand = expansion(mult = 0.1), 
    labels = scales::comma,
    breaks = seq(0, max(population_df$population, na.rm = TRUE), length.out = 4) # Adding scale breaks
  ) +
  scale_x_discrete(labels = function(x) paste0(x, " Radius")) + # Adding more descriptive x-axis labels
  geom_text(
    aes(label = scales::comma(population)), 
    vjust = -0.5, 
    size = 2,  # Increase numeric value font size on top of bars
    fontface = "bold",
    family = "serif"
  ) +  # Annotate bars with population values
  annotate(
    "text", 
    x = 3, 
    y = max(population_df$population, na.rm = TRUE) * 0.8, 
    label = "Data Source: Volcanic Activity Database", 
    size = 3,  # Increase annotation font size
    fontface = "italic", 
    family = "serif",
    color = "gray30"
  )  # Adding a data source annotation

# Activity 7: To analyze volcano population data and transform using pivot_longer
volcano_population <- volcano %>%
  select(volcano_name, population_within_5_km, population_within_10_km, population_within_30_km, population_within_100_km) %>%
  pivot_longer(
    cols = starts_with("population_within"),
    names_to = "distance", 
    values_to = "population"
  )
# Plot population distribution by distance from volcanoes
ggplot(volcano_population, aes(x = distance, y = population, fill = distance)) +
  geom_boxplot(outlier.shape = 21, outlier.color = "red", outlier.size = 2) +
  labs(
    title = "Population Distribution by Distance from Volcanoes",
    x = "Distance from Volcano",
    y = "Population"
  ) +
  theme_minimal() +
  theme(
    plot.title = element_text(size = 8, margin = margin(t = 8, b = 9), family = "serif", face = "bold"),
    axis.title.x = element_text(size = 8, margin = margin(t = 8), family = "serif"),
    axis.title.y = element_text(size = 8, margin = margin(r = 8), family = "serif"),
    axis.text.x = element_text(size = 8, angle = 45, hjust = 1, family = "serif", face = "bold"),  # X-axis label styling
    axis.text.y = element_text(size = 8, family = "serif", face = "bold"),  # Y-axis label styling
    legend.title = element_text(size = 8, family = "serif"),  # Legend title font size
    legend.text = element_text(size = 8, family = "serif"),   # Legend text font size
    panel.background = element_rect(fill = "white", color = "black", size = 0.5),
    plot.background = element_rect(fill = "white", color = "black", size = 0.5),
    plot.margin = margin(t = 5, r = 5, b = 5, l = 5)
  ) +
  scale_y_continuous(
    labels = scales::comma,
    breaks = seq(0, max(volcano_population$population, na.rm = TRUE), length.out = 5) # Adjust Y-axis breaks
  ) +
  scale_x_discrete(
    labels = c("5 km Radius", "10 km Radius", "30 km Radius", "100 km Radius") # More descriptive X-axis labels
  ) +
  annotate(
    "text", 
    x = 4, 
    y = max(volcano_population$population, na.rm = TRUE) * 0.9, 
    label = "Note: Outliers marked in red", 
    size = 2, 
    fontface = "italic", 
    family = "serif",
    color = "gray60"
  )  # Adding annotation for outliers

# Activity 8: To transform tree rings dataset using pivot_wider
tree_rings_summary <- tree_rings %>%
  group_by(year) %>%
  summarize(
    avg_temp_index = mean(europe_temp_index, na.rm = TRUE),
    total_trees = sum(n_tree, na.rm = TRUE)
  ) %>%
  drop_na(year)  # Remove any NA values in 'year' to avoid errors

# Ensure 'year' is numeric
tree_rings_summary$year <- as.numeric(tree_rings_summary$year)

# Categorize years into 5 bins
tree_rings_summary <- tree_rings_summary %>%
  mutate(year_category = cut(year, breaks = 5, labels = c("Very Old", "Old", "Mid", "Recent", "Modern")))

# Convert to wide format
tree_rings_wide <- tree_rings_summary %>%
  pivot_wider(names_from = year_category, values_from = c(avg_temp_index, total_trees))

# Check if year has valid finite values
if (all(is.finite(tree_rings_summary$year))) {
  
  # Plot transformed data using categorized years
  ggplot(tree_rings_summary, aes(x = year_category, y = avg_temp_index, fill = year_category)) +
    geom_boxplot() +
    labs(
      title = "Average Temperature Index by Year Categories",
      x = "Year Category",
      y = "Average Temperature Index"
    ) +
    theme_minimal() +
    theme(
      plot.title = element_text(size = 12, margin = margin(t = 10, b = 10), family = "serif", face = "bold"),
      axis.title.x = element_text(size = 11, margin = margin(t = 8), family = "serif"),
      axis.title.y = element_text(size = 11, margin = margin(r = 8), family = "serif"),
      axis.text.x = element_text(size = 12, family = "serif", face = "bold"),  # Increased X-axis font size
      axis.text.y = element_text(size = 12, family = "serif", face = "bold"),  # Increased Y-axis font size
      panel.background = element_rect(fill = "white", color = "black", size = 0.5),
      plot.background = element_rect(fill = "white", color = "black", size = 0.5),
      plot.margin = margin(t = 5, r = 5, b = 5, l = 5)
    ) +
    geom_text(
      aes(label = round(avg_temp_index, 2)), 
      vjust = -0.5, 
      size = 4,  # Font size for numeric values
      family = "serif"
    ) + 
    annotate(
      "text", 
      x = 4, 
      y = max(tree_rings_summary$avg_temp_index, na.rm = TRUE) * 0.9, 
      label = "Data Source: Historical Tree Rings", 
      size = 3.5, 
      fontface = "italic", 
      family = "serif",
      color = "gray60"
    )
  
} else {
  print("Error: 'year' contains non-finite values.")
}

# Activity 9: Spatial visualization (map) of volcano locations
# Prepare a world map

# Filter and prepare volcano data for mapping
volcano_map_data <- volcano %>%
  select(volcano_name, latitude, longitude, elevation) %>%
  filter(!is.na(latitude) & !is.na(longitude))

# Categorize elevation into 5 groups for better visualization
volcano_map_data <- volcano_map_data %>%
  mutate(elevation_category = cut(elevation, breaks = 5, 
                                  labels = c("Low", "Moderate", "High", "Very High", "Extreme")))
# Plot the map with volcano locations
ggplot() +
  borders("world", colour = "gray85", fill = "gray80") +
  geom_point(data = volcano_map_data, aes(x = longitude, y = latitude, size = elevation, color = elevation_category),
             alpha = 0.7) +
  scale_color_manual(
    values = c("Low" = "yellow", "Moderate" = "orange", "High" = "red", "Very High" = "darkred", "Extreme" = "black"),
    name = "Elevation Category"
  ) +  # Custom color scale for elevation categories
  scale_size_continuous(name = "Elevation (m)", range = c(1, 2)) +  # Adjust size scaling
  labs(
    title = "Global Distribution of Volcanoes",
    x = "Longitude",
    y = "Latitude"
  ) +
  theme_minimal() +
  theme(
    plot.title = element_text(size = 10, margin = margin(t = 10, b = 10), family = "serif", face = "bold"),
    axis.title.x = element_text(size = 10, margin = margin(t = 8), family = "serif"),
    axis.title.y = element_text(size = 10, margin = margin(r = 8), family = "serif"),
    axis.text.x = element_text(size = 10, family = "serif", face = "bold"),  # Increased X-axis font size
    axis.text.y = element_text(size = 10, family = "serif", face = "bold"),  # Increased Y-axis font size
    legend.title = element_text(size = 10, family = "serif", face = "bold"),  # Increased legend title size
    legend.text = element_text(size = 10, family = "serif"),  # Increased legend text size
    legend.position = "right",  # Keep legend on the right for better clarity
    panel.background = element_rect(fill = "lightblue"),  # Ocean color
    panel.grid = element_line(color = "white"),
    plot.background = element_rect(fill = "white", color = "black", size = 0.5),
    plot.margin = margin(t = 4, r = 4, b = 4, l = 2)
  ) +
  guides(
    size = guide_legend(override.aes = list(alpha = 0.5)),  # Ensure all sizes are visible in legend
    color = guide_legend(override.aes = list(size = 2))   # Set consistent color scale in legend
  )

plot1 <- ggplot(top_volcanoes, aes(x = reorder(volcano_name, -total_eruptions), y = total_eruptions)) +
  geom_col(fill = "steelblue", width = 0.7) + 
  labs(title = " Top 10 Volcanoes by Total Eruptions", x = "Volcano Name", y = "Total Eruptions") +
  theme_minimal() +
  theme(
    plot.title = element_text(size = 7),
    axis.text.x = element_text(size = 6, angle = 45, hjust = 1),
    axis.text.y = element_text(size = 6),
    axis.title = element_text(size = 6)
  )
plot2 <- ggplot(event_summary, aes(x = reorder(event_type, -total_events), y = total_events)) +
  geom_col(fill = "darkorange", width = 0.7) + 
  labs(title = " Distribution of Volcanic Event Types", x = "Event Type", y = "Total Events") +
  theme_minimal() +
  theme(
    plot.title = element_text(size = 7),
    axis.text.x = element_text(size = 6, angle = 45, hjust = 1),
    axis.text.y = element_text(size = 6),
    axis.title = element_text(size = 6)
  )
plot3 <- ggplot(sulfur_long, aes(x = year, y = sulfur_concentration, color = source)) +
  geom_line(size = 1) +
  labs(title = " Sulfur Concentration Over Time", x = "Year", y = "Sulfur Concentration") +
  theme_minimal() +
  theme(
    plot.title = element_text(size = 7),
    axis.text.x = element_text(size = 6),
    axis.text.y = element_text(size = 6),
    axis.title = element_text(size = 6),
    legend.text = element_text(size = 6),
    legend.title = element_text(size = 6)
  )

plot4 <- ggplot() +
  borders("world", colour = "gray85", fill = "gray80") +
  geom_point(data = volcano_map_data, 
             aes(x = longitude, y = latitude, size = elevation / 13, color = elevation_category),  # Further reduce point size
             alpha = 0.5) +  # Slightly transparent for clarity
  labs(title = " Global Distribution of Volcanoes", x = "Longitude", y = "Latitude") +
  theme_minimal() +
  theme(
    plot.title = element_text(size = 7),
    axis.text.x = element_text(size = 6),
    axis.text.y = element_text(size = 6),
    axis.title = element_text(size = 6),
    legend.text = element_text(size = 6),
    legend.title = element_text(size = 6)
  ) +
  scale_size_continuous(range = c(0.3, 2.5))  # Further limit max size

final_plot <- (plot1 + plot2) / (plot3 + plot4) +
  plot_annotation(title = "Volcanic Activity and Environmental Impact", tag_levels = 'a') &
  theme(plot.title = element_text(size = 8))  # Reduce main title size

# Print combined plot
print(final_plot)
Warning: Removed 580 rows containing missing values or values outside the scale range
(`geom_line()`).

#4.Reflection Section
#Skills
#I learnt data manipulation, codes reformulation depending on the purpose, Github and manipulating online data.
#Create several types of maps with their manipulation such as tittle,sizing,font stle,…
#Challenges
#Finding right codes for guidance.
#R clashed with 00 Lock file, submission still problem.not familiar with quarto work space, fear to loose everything and stopped much manupulation of quarto work space,pivot_wider failed to understand and manage to get understandable
#Future
#Wish to let us have access on course, Heading and subheading. 

gether content and executable code into a finished presentation. To learn more about Quarto presentations see https://quarto.org/docs/presentations/.

Bullets

When you click the Render button a document will be generated that includes:

  • Content authored with markdown
  • Output from executable code

Code

When you click the Render button a presentation will be generated that includes both content and the output of embedded code. You can embed code like this:

1 + 1
[1] 2