Created
November 3, 2020 00:53
-
-
Save mikemahoney218/de1020c5922b71b862e55f98d61c7ed1 to your computer and use it in GitHub Desktop.
R script for map of domestric NYC flights in 2013
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(nycflights13) | |
library(dplyr) | |
library(sf) | |
library(maps) | |
states <- st_as_sf(map("state", plot = FALSE, fill = TRUE)) | |
from <- nycflights13::flights %>% | |
count(origin, dest, carrier) %>% | |
mutate(idx = row_number()) %>% | |
left_join(select(airports, lat, lon, faa), by = c(origin = "faa")) | |
to <- nycflights13::flights %>% | |
count(origin, dest, carrier) %>% | |
mutate(idx = row_number()) %>% | |
left_join(select(airports, lat, lon, faa), by = c(dest = "faa")) | |
flight_groups <- rbind(from, to) %>% | |
arrange(idx) %>% | |
group_by(idx) %>% | |
tidyr::drop_na(lon, lat) | |
resulting <- vector("list", max(flight_groups$idx)) | |
for (i in 1:max(flight_groups$idx)) { | |
resulting[[i]] <- st_as_sf(flight_groups[flight_groups$idx == i, ], | |
coords = c("lon", "lat")) %>% | |
group_by(idx) %>% | |
summarise(across(-matches("geometry"), unique), .groups = "drop") %>% | |
st_cast("LINESTRING") | |
} | |
resulting <- do.call(rbind, resulting) | |
st_crs(resulting) <- st_crs(4326) | |
resulting %>% | |
ggplot() + | |
geom_sf(data = states, aes(geometry = geom), fill = NA, size = 0.05, alpha = 0.2, color = "#a0a0a0") + | |
geom_sf(aes(alpha = n, color = carrier), size = 0.7) + | |
scale_alpha_continuous(range = c(0.15, 0.75)) + | |
scale_color_manual(values = viridis::cividis(16)) + | |
coord_sf(xlim = c(-124.85, -66.9), | |
ylim = c(24.4, 49.4), | |
expand = c(0, 0)) + | |
theme_void() %+replace% | |
theme(legend.position = "none", | |
plot.background = element_rect(fill = "#2c2c2c")) | |
ggsave("12_connections.png", width = 17, height = 11) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Output: