0.1 Packages

library(tidyverse) # dev ggplot version required: devtools::install_github("hadley/ggplot2")
## -- Attaching packages ------------------------------------------------------------------------------ tidyverse 1.2.1 --
## <U+221A> ggplot2 3.0.0     <U+221A> purrr   0.2.5
## <U+221A> tibble  1.4.2     <U+221A> dplyr   0.7.6
## <U+221A> tidyr   0.8.1     <U+221A> stringr 1.3.1
## <U+221A> readr   1.1.1     <U+221A> forcats 0.3.0
## -- Conflicts --------------------------------------------------------------------------------- tidyverse_conflicts() --
## x dplyr::filter() masks stats::filter()
## x dplyr::lag()    masks stats::lag()
library(sf)
## Linking to GEOS 3.6.1, GDAL 2.1.3, proj.4 4.9.3
library(readxl)
library(httr)
library(ggmap)
library(gganimate) # devtools::install_github("dgrtwo/gganimate")
library(hrbrthemes) # devtools::install_github("hrbrmstr/hrbrthemes")
## NOTE: Either Arial Narrow or Roboto Condensed fonts are required to use these themes.
##       Please use hrbrthemes::import_roboto_condensed() to install Roboto Condensed and
##       if Arial Narrow is not on your system, please see http://bit.ly/arialnarrow
library(prettydoc)

1 Create Geogif for one day of flights

if (!require("pacman")) install.packages("pacman")
## Loading required package: pacman
pacman::p_load(dplyr,sf,lwgeom,ggplot2,maps,maptools,rgeos)

#read in flight data
flight_sf <- readRDS("twist_zrh-master/flight_sf.RDS")
#filter flights for a single date
vizdata <- flight_sf %>% filter(date=="08.12.2017")

#create a new geometry : lines connecting Zurich Airport and the destination/origin of the flight
vizdata$flightline <- sf::st_union(vizdata$geometry, st_as_sfc("POINT(8 47)",crs=st_crs("+proj=longlat +datum=WGS84 +no_defs"))) %>% 
  st_cast("LINESTRING")
## although coordinates are longitude/latitude, st_union assumes that they are planar
#get worldmap
world1 <- sf::st_as_sf(maps::map('world', plot = FALSE, fill = TRUE))

#plot on top (new ggplot2 2.3.0 version needed!)
ggplot(vizdata)+
  geom_sf(aes(geometry=flightline))+
  geom_sf(data=world1)

#we can now set the "flightline"-geometry as the main geometry in our dataset
st_geometry(vizdata) = "flightline"

#transform worldmap and flights into spheric projection
world2 <- sf::st_transform(
  world1,
  "+proj=laea +y_0=0 +lon_0=8 +lat_0=47 +ellps=WGS84 +no_defs"
)

flights2 <- sf::st_transform(
 vizdata,
 "+proj=laea +y_0=0 +lon_0=8 +lat_0=47 +ellps=WGS84 +no_defs"
)

#Plot!
ggplot() + 
  geom_sf(data=world2, color="white",size=0.2)+
  geom_sf(data=flights2, aes(color=as.numeric(diff), size=as.numeric(diff)), alpha=0.4, show.legend = "line") +
  scale_size(range = c(0.2,5)) +
  theme_void() +
  theme(plot.title = element_text(color="black", size=14, face="bold")) +
  scale_color_continuous(low = "#f4ee42", high = "#091451") +
  guides(alpha=F) +
  coord_sf(ndiscr=1000)+
  theme(plot.background = element_rect(fill="#f5f5f2")) +
  ggtitle("Flight Delays to and from ZRH on December 8th, 2017") +
  guides(size = F) +
  labs(color = "Delay Time [sec]", subtitle = "Time: {frame_time}") +
  transition_time(effective_time) +
  ease_aes('linear', interval = 0.05)

anim_save("pics/plot_2017-12-08.gif")