Skip to Content
Course content

231: Climate Data Analysis in R

Click on the "Edit" button in the top corner of the screen to edit your slide content.

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 decade that groups the years into 10-year bins (e.g., 1880-1889, 1890-1899).
  • Calculate the average anomaly for 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."
Rating
0 0

There are no comments for now.

to be the first to leave a comment.