R
Completed
-
Section 1: Getting Started
-
Section 2: Core Syntax
-
Section 3: Data Structures
-
Section 4: Data Manipulation
-
Section 5: Visualization and Statistics
-
Section 6: Advanced R
-
Section 7: Practical Projects
-
Section 8: Interview Practice
-
Section 9: More Practice Exercises
-
Section 10: Shiny Apps in Depth
-
Section 11: More Data Wrangling
-
Section 12: Tidyverse Deep Dive
-
Section 13: Statistical Modeling Deep Dive
-
Section 14: Machine Learning in R
-
Section 15: R Visualization Deep Dive
-
Section 16: R Package Development Deep Dive
-
Section 17: R for Reproducible Research
-
Section 18: R and Databases
-
Section 19: R Performance Optimization
-
Section 20: Bioinformatics and Specialized R
-
Section 21: More Shiny Practice
-
Section 22: More Practice Exercises
-
Section 23: R Data Structures Deep Dive
-
Section 24: More Interview and Review
-
Section 25: R for Business Analytics
-
Section 26: R Text Mining and NLP
-
Section 27: R Spatial Data Analysis
-
Section 28: R Deep Learning
-
Section 29: Advanced Statistical Techniques
-
Section 30: R Object Systems Deep Dive
-
Section 31: R Environments and Metaprogramming
-
Section 32: R for Finance
-
Section 33: R for Clinical and Health Data
-
Section 34: More Shiny Advanced Practice
-
Section 35: R Data Cleaning Deep Dive
-
Section 36: R Reporting Automation
-
Section 37: More Practical Projects Round 2
-
Section 38: R Ecosystem and Career
-
Section 39: More Visualization Practice
-
Section 40: R for Experimentation
-
Section 41: R for Genomics and Bioinformatics Deep Dive
-
Section 42: R for Marketing Analytics
-
Section 43: R Data Import/Export Deep Dive
-
Section 44: R String Processing Deep Dive
-
Section 45: R for Actuarial and Insurance Analytics
-
Section 46: R Testing and Quality Assurance Deep Dive
-
Section 47: R Version Control and Collaboration
-
Section 48: R Advanced Functional Programming
-
Section 49: R for Supply Chain and Operations
-
Section 50: More Practice Exercises Round 3
-
Section 51: R Dashboards and BI Integration
-
Section 52: R Data Governance and Ethics
-
Section 53: More Modeling Practice
-
Section 54: R Final Capstone Projects
-
Section 55: R for Sports Analytics
-
Section 56: More Interview Practice Round 2
-
Section 57: R Networking and APIs
-
Section 58: R for Environmental Science
-
Section 59: R Notebook and Documentation Practices
-
Section 60: More Data Wrangling Mastery
-
Section 61: R for A/B Testing at Scale
-
Section 62: R Package Ecosystem Deep Dive
231: Climate Data Analysis in R
Climate data is notoriously messy. Whether you're dealing with satellite readings or old handwritten logs from weather stations, the challenge is usually the same: extracting a signal from a massive amount of noise. Today, we're going to analyze global temperature anomalies—which is just a fancy way of saying "how much the temperature deviated from a long-term average."
Getting our temperature data into R
I've put together a small dataset representing yearly global temperature anomalies from 1880 to 2023. In a real-world scenario, you'd likely be pulling this from a NASA or NOAA CSV, but for this exercise, we'll create a data frame directly. I prefer using the tidyverse suite for this because the piping operator makes the data flow much easier to follow.
library(tidyverse)
# Creating a simulated dataset of global temperature anomalies
set.seed(42)
climate_data <- data.frame(
year = 1880:2023,
anomaly = seq(-0.3, 1.1, length.out = 144) + rnorm(144, 0, 0.15)
)
head(climate_data)
Visualizing the raw noise
The first thing I always do with climate data is plot it. You can't trust summary statistics alone because a few extreme years can skew your perception of the trend. I'll use ggplot2 to get a quick look at the trajectory.
ggplot(climate_data, aes(x = year, y = anomaly)) +
geom_point(alpha = 0.5, color = "steelblue") +
labs(title = "Global Temperature Anomaly (1880-2023)",
x = "Year",
y = "Deviation from Baseline (°C)") +
theme_minimal()
Correcting a plotting glitch
Here is where I usually trip up. I wanted to add a trend line to this plot to see the overall slope, so I tried adding a geom_line() call. But when I ran the code below, I got a blank plot with no line, only the points.
# My first attempt (which failed)
ggplot(climate_data, aes(x = year, y = anomaly)) +
geom_point() +
geom_line()
I stared at it for a minute before remembering that ggplot sometimes struggles when it thinks the x-axis is a discrete factor rather than a continuous number, or when it's confused about how to group the points. Even though year is numeric here, it's a good habit to be explicit. The fix is simple: I need to tell R that all these points belong to the same group.
# The corrected version
ggplot(climate_data, aes(x = year, y = anomaly, group = 1)) +
geom_point(alpha = 0.5) +
geom_line(color = "red") +
theme_minimal()
Smoothing the trend with a moving average
The raw line is still too "spiky" to be useful for a presentation. To see the actual climate signal, we need a rolling average. I'll use a 5-year window. This smooths out the short-term volatility (like El Niño years) and reveals the long-term warming trend.
Since we don't have a built-in rolling function in base R, I'll use slider, which is a fantastic package for this kind of windowed calculation. If you don't have it, install.packages("slider") first.
library(slider)
climate_smoothed <- climate_data %>%
mutate(smoothed_anomaly = slide_dbl(anomaly, mean, .before = 2, .after = 2))
ggplot(climate_smoothed, aes(x = year)) +
geom_point(aes(y = anomaly), alpha = 0.3) +
geom_line(aes(y = smoothed_anomaly), color = "darkred", size = 1) +
labs(title = "Global Temperature Trend (5-Year Moving Average)",
subtitle = "Smoothing out annual volatility to see the climate signal",
x = "Year",
y = "Anomaly (°C)") +
theme_minimal()
By layering the raw points behind the smoothed line, you keep the honesty of the data (the noise) while highlighting the conclusion (the warming). That's the standard approach in professional climate reporting.
📋 Practical Task
Exercise: Calculating Decadal Warming Accelerations
Using the climate_data data frame created in the lesson, write a script that performs the following:
- Create a new column called
decadethat groups the years into 10-year bins (e.g., 1880-1889, 1890-1899). - Calculate the average
anomalyfor each decade. - Determine the difference in average anomaly between the most recent decade (2014-2023) and the first decade (1880-1889).
- Print the final result as a sentence, such as: "The average anomaly increased by X degrees between the first and last decade."
There are no comments for now.