Last active
August 19, 2018 03:28
-
-
Save Ryo-N7/826fc8c755f9a7c1fab236cd17d8a352 to your computer and use it in GitHub Desktop.
Tokyo's historical summer temperature (heatmap) - original from Toyo Keizai Online
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(ggplot2) | |
library(dplyr) | |
library(tidyr) | |
library(purrr) | |
library(jsonlite) | |
library(lubridate) | |
library(magrittr) | |
library(scales) | |
library(stringr) | |
library(extrafont) | |
# loadfonts() | |
# download the provided .json data from the original Toyo Keizai Online link (https://toyokeizai.net/sp/visual/tko/temperature/) | |
# then read it into R with the jsonlite package. | |
tokyo_his_temp <- jsonlite::read_json("data/temperature.json", simplifyVector = TRUE) | |
# mold it into a dataframe... | |
tokyo_weather_df <- tokyo_his_temp %>% | |
set_names(nm = 1876:2018) %>% | |
map(~as.data.frame(.) %>% | |
modify_if(., is.factor, as.character) %>% | |
modify_if(., is.character, as.numeric)) %>% | |
map2_df(., names(.), ~ mutate(., ID = .y)) %>% | |
rename(avg_temp = ".", | |
year = ID) | |
# separate 2018 as data only up to 7/17 >>> i end up just not showing the 2018 data in the final viz. | |
tokyo_weather_df_2018 <- tokyo_weather_df %>% | |
filter(year == 2018) %>% | |
mutate( | |
date = seq.Date(from = as.Date("2018-06-01"), | |
by = "day", | |
length = 47), | |
date = format(date, "%m/%d") | |
) | |
# rest of the years, then combine back | |
tokyo_weather_df <- tokyo_weather_df %>% | |
filter(year != 2018) %>% | |
group_by(year) %>% | |
mutate( | |
date = seq.Date(from = as.Date("1876-06-01"), | |
by = "day", | |
length = 122), | |
date = format(date, "%m/%d") | |
) %>% | |
ungroup() %>% | |
full_join(tokyo_weather_df_2018) %>% | |
mutate(year = as.numeric(year)) | |
# used diverging 8-class palette from colorbrewer2.org | |
cols <- rev(c('#d53e4f','#f46d43','#fdae61','#fee08b','#e6f598','#abdda4','#66c2a5','#3288bd')) | |
labels <- c("10", "12", "14", "16", "18", "20", "22", "24", "26", "28", "30", "32") | |
breaks <- c(seq(10, 32, by = 2)) | |
y_labs <- seq(1880, 2015, by = 10) | |
# plot! | |
tokyo_weather_df %>% | |
ggplot(aes(x = date, y = year, fill = avg_temp)) + | |
geom_tile() + | |
scale_fill_gradientn( | |
colours = cols, | |
labels = labels, | |
breaks = breaks, | |
limits = c(11, max(tokyo_weather_df$avg_temp))) + | |
guides(fill = guide_colorbar(title = expression("Temperature " ( degree~C)), | |
reverse = FALSE, | |
title.position = "left", | |
label.position = "bottom", | |
nrow = 1)) + | |
scale_y_reverse(limits = c(2017, 1876), expand = c(0, 0), | |
breaks = c(1876, seq(1880, 2015, by = 10), 2017)) + | |
scale_x_discrete(breaks = c("06/01", "07/01", "08/01", "09/01", "09/30"), | |
labels = c("June 1st", "July 1st", "Aug. 1st", | |
"Sept. 1st", "Sept. 30th")) + | |
labs(title = "Summers in Tokyo are Getting Longer and Hotter (1876-2017)", | |
subtitle = glue::glue(" | |
One Row = One Year, From June 1st to September 30th | |
Average Temperature (Celsius) | |
"), | |
caption = "Data from Toyo Keizai News via Japan Meteorological Agency") + | |
theme_minimal() + | |
theme(text = element_text(family = "Roboto Condensed", size = 12), | |
axis.title = element_blank(), | |
panel.grid = element_blank(), | |
legend.position = "bottom", | |
legend.key.width = unit(3, "cm"), | |
plot.margin=unit(c(1,1,1.5,1.2),"cm")) | |
# save it on your computer | |
ggsave("tokyo_summer_temp_eng.png", plot = last_plot()) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment