#These are the same libraries I bring in for pretty much every project
#where I might do data visualization.
library(tidyverse); library(dplyr); library(lubridate); library(scales)
library(ggplot2); library(cowplot); library(RColorBrewer); library(ggpubr);
This document shows how to import, process, and visualize the global temperature anomaly data from https://climate.nasa.gov/vital-signs/global-temperature.
#Download the data
theData <- read.table("https://data.giss.nasa.gov/gistemp/graphs/graph_data/Global_Mean_Estimates_based_on_Land_and_Ocean_Data/graph.txt", skip = 5)
#Give the data headings
names(theData) <- c("year","temp_anomaly","smoothed_temp_anomaly")
#Add a new column to break up the data into years since start
theData$years_passed <- theData$year - min(theData$year)
#Add a new column to show which chunk of 50 years each dataset falls into
theData$period <- NA
theData[theData$years_passed >= 0 &
theData$years_passed < 50,]$period = "1880 to 1929"
theData[theData$years_passed >= 50 &
theData$years_passed < 100,]$period = "1930 to 1979"
theData[theData$years_passed >= 100 &
theData$years_passed < 150,]$period = "1980 to 2023"
#Make the period into a factor, rather than a number
theData$period <- as.factor(theData$period)
#Make sure the temperature anomaly is treated as a number
theData$temp_anomaly <- as.numeric(theData$temp_anomaly)
For this example, I’ll take a different approach: I’ll take a look at each set of 50 year data, and plot the global temperature anomaly within that period of time.
This type of plot, called a box plot with jittered points, is good for simultaneously communicating both the MEAN data and the VARIATION within the data. This shows that as time has gone on, global temperatures have risen and also become more variable between 1880 and 2023.
#Make a boxplot showing the distribution of temperature anomaly
#for each 50-year period, with jittered dots for each individual year:
ggplot(data=theData, aes(x=period,y=temp_anomaly)) + theme_bw() +
geom_boxplot(color="black",
outlier.shape=NA) +
geom_point(aes(col=temp_anomaly),
position = position_jitter(width = 0.25),
size=2,
alpha=0.5) +
scale_color_gradient(low = "orange", high = "red3") +
xlab("") + ylab("Temperature Anomaly (C)") +
theme(legend.position = "none")