This project demonstrates the use of the ggplot2 package
in R for visualizing climate change data such as temperature anomalies,
CO₂ levels, and sea level rise from 1980 to 2020.
ggplot2 is a data visualization package for the
statistical programming language R. It is based on the grammar of
graphics and allows users to layer components to build complex plots
easily.
The dataset contains: - Temperature Anomaly (°C): Difference from average global temperature - CO2 Level (ppm): Carbon dioxide concentration - Sea Level Rise (mm): Relative increase in sea levels
library(ggplot2)
## Warning: package 'ggplot2' was built under R version 4.4.3
library(dplyr)
## Warning: package 'dplyr' was built under R version 4.4.3
##
## 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(tidyr)
## Warning: package 'tidyr' was built under R version 4.4.3
# Simulated Climate Data
climate_df <- data.frame(
Year = 1980:2020,
Temp_Anomaly = c(0.12, 0.14, 0.18, 0.2, 0.22, 0.25, 0.27, 0.28, 0.31, 0.33,
0.35, 0.38, 0.4, 0.41, 0.43, 0.45, 0.47, 0.5, 0.52, 0.55,
0.57, 0.59, 0.6, 0.62, 0.65, 0.67, 0.68, 0.7, 0.72, 0.74,
0.76, 0.78, 0.8, 0.81, 0.83, 0.85, 0.87, 0.9, 0.91, 0.93, 0.95),
CO2_Level = c(338, 340, 342, 344, 346, 349, 351, 353, 355, 357,
360, 362, 364, 367, 369, 371, 373, 375, 377, 380,
382, 384, 386, 389, 391, 393, 395, 398, 400, 403,
405, 407, 409, 412, 414, 417, 419, 421, 423, 425, 428),
Sea_Level = c(0, 1, 2, 3, 5, 6, 7, 9, 10, 12,
14, 15, 17, 18, 20, 22, 23, 25, 26, 28,
30, 32, 33, 35, 36, 38, 40, 41, 43, 45,
47, 48, 50, 51, 53, 55, 56, 58, 59, 61, 63)
)
# Convert to long format for plotting
climate_long <- pivot_longer(climate_df, cols = -Year, names_to = "Metric", values_to = "Value")
# Plot
ggplot(climate_long, aes(x = Year, y = Value, color = Metric)) +
geom_line(size = 1) +
geom_point(size = 1.5) +
facet_wrap(~ Metric, scales = "free_y") +
labs(
title = "Climate Change Trends (1980–2020)",
subtitle = "Global Temperature Anomaly, CO2 Concentration, and Sea Level Rise",
x = "Year",
y = "Value",
caption = "Simulated data for STAT 484 project"
) +
theme_minimal() +
theme(
plot.title = element_text(size = 16, face = "bold"),
strip.text = element_text(size = 12, face = "bold"),
legend.position = "none"
)
## 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.
This demonstration shows how ggplot2 can be used to
reveal long-term patterns in climate data using minimal and clean
code.