Edit this page

< back to recipes

Warming stripes visualise a location’s average annual temperature over a hundred or more years. The dark blue to dark red palette is used to show the spread of cooler and warmer years. Warming stripes were originally conceived by Ed Hawkins, a climate scientist based at the University of Reading. They can easily be created in R using historic weather data from the Met Office. In this recipe we’ll create a warming stripes for Sheffield.

Ingredients

Instructions

  1. Load the necessary R packages.
library(tidyverse) ; library(stringr) ; library(lubridate) ; library(RColorBrewer)
  1. Visit the Met Office’s historic station data page. Select one of the locations from the dropdown or the map. Follow the link to the data and copy the URL.

  2. Supply the file argument of the read_table function with the URL that you’ve copied. Skip the first few rows that contain metadata and specify the variable data types. We’ll also remove the second header row.

raw <- read_table("https://www.metoffice.gov.uk/pub/data/weather/uk/climate/stationdata/sheffielddata.txt", col_names = TRUE, skip = 5, col_types = cols(
  yyyy = col_factor(NULL), mm = col_integer(), tmax = col_double(), 
  tmin = col_double(), af = col_integer(), rain = col_double(), sun = col_double())) %>% 
  slice(-1)
  1. We need to do some further cleaning. We’ll replace “—” with NA, format the yyyy variable as a date, and ignore 2019 because it’s not a complete year.
df <- raw %>%
  na_if("---") %>%
  mutate(yyyy = ymd(str_c(yyyy, "01-01", sep = "-"))) %>%
  filter(yyyy != "2019-01-01")
  1. Next we need to calculate the average annual temperature.
temperature <- df %>% 
  group_by(yyyy) %>% 
  summarise(average = (mean(tmax, na.rm = TRUE) + mean(tmin, na.rm = TRUE)) / 2)
  1. Now we are ready to create our first warming stripes visualisation.
ggplot(temperature, aes(x = yyyy, y = 1, fill = average)) +
  geom_tile() +
  scale_x_date(expand = c(0, 0), date_breaks = "10 years", date_labels = "%Y") +
  scale_y_continuous(expand = c(0, 0)) +
  scale_fill_gradientn(colours = rev(brewer.pal(11, "RdBu")),
                       guide = guide_colorbar(
                         barheight = unit(3, units = "mm"),
                         barwidth = unit(50, units = "mm"),
                         title.vjust = 1)) +
  labs(title = "Average annual temperature in Sheffield",
       subtitle = paste0(year(min(df$yyyy)), "-", year(max(df$yyyy))),
       caption = "Source: Met Office",
       fill = "\u00B0C") +
  theme_minimal() +
  theme(plot.title = element_text(size = 14, face = "bold"),
        axis.title = element_blank(),
        axis.text.x = element_text(vjust = 3),
        axis.text.y = element_blank(),
        legend.position = "bottom")
  1. If you want a warming stripes visualisation without any chart paraphernalia use this code.
ggplot(temperature, aes(x = yyyy, y = 1, fill = average))+
  geom_tile() +
  scale_x_date(expand = c(0, 0), date_breaks = "10 years", date_labels = "%Y") +
  scale_y_discrete(expand = c(0, 0)) +
  scale_fill_gradientn(colours = rev(brewer.pal(11, "RdBu"))) +
  theme_void() +
  theme(legend.position = "none")
  1. Output the map as a PNG file.
ggsave("warming_stripes.png", dpi = 300)


Notes

Further information about warming stripes visualisations can be found at the Climate Lab Book blog and charts for individual countries can be downloaded from ShowYourStripes.info.

Credits

This recipe has been adapted from How to create ‘Warming Stripes’ in R by Dominic Royé.