Created
September 4, 2019 02:08
-
-
Save fanghuiz/4f3ab09e02f3293b14db5ac5667c38b2 to your computer and use it in GitHub Desktop.
Create heatmap to visualize the daily temperature in England over 100 years
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
library(tidyverse) | |
library(lubridate) | |
# Dataset URL | |
url_eng_daily_max <- "https://www.metoffice.gov.uk/hadobs/hadcet/cetmaxdly1878on_urbadj4.dat" | |
# Read in data from URL | |
eng_daily_max <- data.table::fread( | |
url_eng_daily_max, | |
# Encode -999 as NA | |
na.strings = "-999", | |
fill = TRUE, | |
# Column names | |
col.names = c("year", "day", 1:12), | |
data.table = FALSE) | |
# Reshape data to tidy format | |
eng_daily_max_tidy <- as_tibble(eng_daily_max) %>% | |
gather(key = "month", value = "t_max_c", -year, -day) %>% | |
mutate( | |
# Original data was in tenths of a degree C. Transform to degree C | |
t_max_c = t_max_c / 10, | |
# Get date of measurement | |
date = str_c(year, month, day, sep = "-"), | |
date = ymd(date), | |
# Get date as day_in_year (1-366) | |
year_day = yday(date) | |
) %>% | |
# Remove invalid date caused by `gather`, e.g. day 31 in some months | |
filter(!is.na(date)) | |
# Create labels for use in graph | |
labels_x <- c("Jan-01","Feb-01","Mar-01","Apr-01","May-01","Jun-01", | |
"Jul-01","Aug-01","Sep-01","Oct-01","Nov-01","Dec-01") | |
breaks_x <- c(1, 32, 60, 91, 121, 152, 182, 213, 244, 274, 305, 335) | |
celcius <- scales::degree_format(unit = "C") | |
breaks_fill <- c(seq(-5, 35, by = 5)) | |
breaks_fill_label <- celcius(breaks_fill) | |
# Heatmap | |
eng_daily_max_tidy %>% | |
ggplot(aes(x = year_day, y = year, fill = t_max_c)) + | |
geom_tile() + | |
scale_fill_distiller(palette = "RdYlBu", | |
na.value = "transparent", | |
breaks = breaks_fill, | |
label = breaks_fill_label) + | |
guides(fill = guide_colorbar(title = NULL, | |
reverse = FALSE)) + | |
scale_y_reverse(breaks = c(seq(1880, 2010, by = 10), 2019), | |
expand = c(0, 0)) + | |
scale_x_continuous(breaks = breaks_x, | |
labels = labels_x, | |
expand = c(0, 0), | |
limits = c(1, 366)) + | |
labs(title = "Maximum Daily Temperature in Central England", | |
subtitle = "From 1878-01-01 to 2019-08-31", | |
caption = glue::glue(" | |
Source: Hadley Centre Central England Temperature (HadCET) Dataset | |
Dataset used: Daily maximum HadCET, 1878 to date | |
Retrieved on 2019-09-03 | |
"), | |
x = "Day", y = "Year") + | |
hrbrthemes::theme_ipsum_rc() + | |
theme(legend.key.width = unit(10, "pt"), | |
legend.key.height = unit(30, "pt"), | |
legend.title = element_text(size = 10), | |
axis.text.x = element_text(size = 10), | |
axis.text.y = element_text(size = 10)) | |
# ggsave("england_daily_max.png", width = 8, height = 6) |
Author
fanghuiz
commented
Sep 4, 2019
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment