#devtools::install_github('hadley/ggplot2')
library(plotly)
library(leaflet)
library(htmltools)
library(tidyverse)
library(xkcd)
library(extrafont)
library(ggrepel)

So Maële Salmon started this trend how to determine your city of choice based on your climate preference, inspired by this xkcd graph.

We already know that the Spanish islands are our all-time favorite (you could have asked the German tourists) that you really should only go to Iceland because of the landscape and that the weather is the same in the Netherlands, no matter where you go.

Different to the above mentioned analysis, I will just take a look at the average temperature to determine the ideal german city. The reason for this is the way the German Meteorological Service provides data. There is certainly a way to get the necessary data to calculate the humidex, but the time I want to put into this project is limited. For this reason, I reduce the analysis to the data set of average temperatures, downloaded from the open data platform of the dwd.

If you just forgot where Germany is located - here is a little help:

# Set coordinates (http://latitude.to/map/de/germany)
lng = 10.4541194
lat = 51.1642292

leaflet() %>% addTiles() %>%
  setView(lng=lng, lat=lat, zoom=4) %>%
  addProviderTiles(providers$Esri.NatGeoWorldMap) %>%
  addMarkers(lng=lng, lat=lat, 
             label = paste0("Coordinates: ",as.character(lat)," ",as.character(lng)))
#saveWidget(m, file="m.html")

Import the Data

We have the seasonal average temperature as well as precipitation of the german federal states, derived from the gridded fields covering Germany for the period 1881 - 2017.

rm(list=ls())
air_temp <- read.csv("data/air_temperature_mean.txt", sep=";")
air_temp %>% gather(key = "location", value = "air_temp", -Jahr, -season) -> air_temp

rain <- read.csv("data/rain.txt", sep=";")
rain %>% gather(key = "location", value = "rain", -Jahr, -season) -> rain

plot like xkcd

I had to manually download the xkcd font and copy it to the font app on my Mac.

We use the fact that we have data from 1881 to the present day and compare 2016 with 1916:

seas <- c("summer", "winter")
delete <- c("Deutschland", "X")
air_temp %>% 
  filter(season %in% seas) %>%
  filter(!location %in% delete) %>%
  #filter(Jahr == 2016) %>%
  spread(season, air_temp) %>%
  filter(!is.na(winter)) %>%
  filter(!is.na(summer)) -> plot.df1
plot.df1 %>%
  filter(Jahr == 1916 | Jahr == 2016) %>%
  ggplot(aes(summer, winter, color=as.factor(Jahr))) +
  geom_point() +
  geom_text_repel(aes(label = location), family = "xkcd", 
                   max.iter = 50000, size = 3) +
  
  ggtitle("Where to live in Germany - 1916 vs. 2016",
          subtitle = "Data from DWD") +
  labs(color = "Year", 
       x= "Summer avg. temperature in Celsius degrees", 
       y = "Winter avg. temperature in Celsius degrees") +
  theme_xkcd() +
  theme(text = element_text(size = 13, family = "xkcd"))

The entire course of time

Let’s see if this average temperature rise can be tracked over time. Hover over the line to see data.

seas <- c("summer", "winter")
delete <- c("Deutschland")

air_temp %>% 
  filter(season %in% seas) %>%
  filter(location %in% delete) -> plot.df2
g <- plot.df2 %>%
  ggplot(aes(Jahr, air_temp, color=season)) +
  geom_line() +
  ggtitle("Average temperature in Germany - 1881-2017",
          subtitle = "Data from DWD") +
  labs(
       x= "", 
       y = "Average temperature in Celsius degrees") +
  theme_xkcd() +
  theme(text = element_text(size = 13, family = "xkcd"))

ggplotly(g, tooltip = c("Jahr","air_temp"))