Last active
March 17, 2022 14:47
-
-
Save rafapereirabr/1c4da1f6c32d6378547557f5a8edecf2 to your computer and use it in GitHub Desktop.
Comparing air passenger demand over time in Brazil
This file contains hidden or 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
#' Comparing air passenger demand over time in Brazil | |
#' Reproducible code to create a figure visualizing how daily air passenger demand in Brazil has changed over time. | |
### Load libraries | |
library(ggplot2) | |
library(flightsbr) | |
library(lubridate) | |
library(data.table) | |
### Download and rbind data | |
d2019 <- read_flights(date = 2019) | |
d2020 <- read_flights(date = 2020) | |
d2021 <- read_flights(date = 2021) | |
d2022 <- read_flights(date = 2022) | |
df <- rbindlist(list(d2019, d2020, d2021, d2022)) | |
### Recode columns and sum daily number of passengers | |
df[, date := lubridate::as_date(dt_chegada_real)] | |
df[, year := lubridate::year(date)] | |
df[, month := lubridate::month(date)] | |
df[, day := lubridate::day(date)] | |
df[, day_week := lubridate::wday(date, label = TRUE)] | |
# sum total of passenger by day and flight type | |
df2 <- df[, .(total_pass = sum(nr_passag_pagos, nr_passag_gratis, na.rm=T)), by=.(date, year)] | |
df2[, xx := paste0("2020-", format(date, "%m-%d"))] | |
df2[, xx := lubridate::as_date(xx, format="%Y-%m-%d") ] | |
df2[, month := lubridate::month(date) ] | |
head(df2) | |
df2 <- subset(df2, year %in% c(2019, 2020, 2021, 2022)) | |
df2 <- subset(df2, date < as.Date('2022-01-31')) | |
### Figure | |
fig <- ggplot() + | |
# geom_smooth( data= df2, aes(x=xx, y=total_pass/1000, color=factor(year)), alpha=.5, size=1) + | |
geom_point( data= df2, aes(x=xx, y=total_pass/1000, color=factor(year)), alpha=.5, size=1) + | |
scale_x_date(date_breaks = "1 months", date_labels = "%b") + | |
labs(y='N. of passengers\n(thousands)', color='Year', x= 'Month', | |
title ="Daily air passenger demand, Brazil", caption = 'figure by @UrbanDemog') + | |
scale_colour_manual(values = c('gray70', 'gray30', 'black', 'red')) + | |
theme_classic() + | |
theme(plot.caption=element_text(colour="gray")) | |
ggsave(fig, filename = 'passenger_demand.png', dpi = 300, | |
width = 18, height = 10, units = 'cm') |
Author
rafapereirabr
commented
Mar 17, 2022
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment