-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathanimated_flights.Rmd
85 lines (67 loc) · 2.24 KB
/
animated_flights.Rmd
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
---
title: "Air Traffic Challenge"
output:
prettydoc::html_pretty:
theme: cayman
highlight: github
toc: true
number_sections: true
df_print: paged
---
## Packages
```{r}
library(tidyverse) # dev ggplot version required: devtools::install_github("hadley/ggplot2")
library(sf)
library(readxl)
library(httr)
library(ggmap)
library(gganimate) # devtools::install_github("dgrtwo/gganimate")
library(hrbrthemes) # devtools::install_github("hrbrmstr/hrbrthemes")
```
# Create Geogif for one day of flights
```{r}
if (!require("pacman")) install.packages("pacman")
pacman::p_load(dplyr,sf,lwgeom,ggplot2,maps,maptools,rgeos)
#read in flight data
flight_sf <- readRDS("flight_sf.RDS")
flight_sf <- readRDS("twist_zrh_cleaned.RDS")
#filter flights for a single date
vizdata <- flight_sf %>% filter(date=="2017-01-01")
#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")
#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_in_secs/60), size=as.numeric(diff_in_secs/60)),show.legend = "line")+
theme_void()+
scale_color_viridis_c(name="Delay (min)")+
guides(alpha=F)+
coord_sf(ndiscr=1000)+
theme_ipsum()+
guides(size=F)+
theme(plot.background = element_rect(fill="#f5f5f2"))+
transition_time(effective_time) +
ease_aes('linear',interval = 0.2)+
labs(title="A day of Flightmovements from / to ZRH",
subtitle = 'Time : {frame_time}')
# ?transition_time
anim_save("plot.gif")
```