Skip to content

Instantly share code, notes, and snippets.

@rafapereirabr
Last active April 27, 2022 18:03
Show Gist options
  • Save rafapereirabr/8866d86b4bf27c913212349eb34d1669 to your computer and use it in GitHub Desktop.
Save rafapereirabr/8866d86b4bf27c913212349eb34d1669 to your computer and use it in GitHub Desktop.
cobranca_de_bagagens.

análise rápida dos dados de evolução diária do N. de bagagens pagas e gratuidas X Km

Download data

#' data downloaded from ANAC using flightsbr, https://github.com/ipeaGIT/flightsbr
#' data dictionary at https://www.gov.br/anac/pt-br/assuntos/regulados/empresas-aereas/envio-de-informacoes/descricao-de-variaveis

library(flightsbr)
library(data.table)
library(ggplot2)
library(patchwork)
options(scipen = 999)


# download data 
df2016 <- read_flights(date=2016)
df2017 <- read_flights(date=2017)
df2018 <- read_flights(date=2018)

df <- rbindlist(list(df2015, df2016, df2017, df2018))

clean data and count daily load


# keep national flights  ---------------------------------------------
df <- df[nm_pais_origem == 'BRASIL' & nm_pais_destino == 'BRASIL', ]


# convert columns to numeric
cols <- c('nr_bagagem_paga_km', 'nr_bagagem_gratis_km', 'kg_carga_paga', 'kg_carga_gratis', 'kg_bagagem_excesso', 'kg_peso')
df[, c(cols) := lapply(.SD, function(x){as.numeric(x)}), .SDcols = cols]


# count daily load  ---------------------------------------------

df2 <- df[, .(nr_bagagem_paga_km = sum(nr_bagagem_paga_km, na.rm=TRUE),
               nr_bagagem_gratis_km = sum(nr_bagagem_gratis_km, na.rm=TRUE),
               kg_carga_paga = sum(kg_carga_paga, na.rm=TRUE),
               kg_carga_gratis = sum(kg_carga_gratis, na.rm=TRUE),
               kg_bagagem_excesso = sum(kg_bagagem_excesso, na.rm=TRUE),
               peso_total = sum(kg_peso, na.rm=TRUE)) , by = .(nr_ano_partida_real, dt_partida_real)]



# reformat date   ---------------------------------------------
df2[, date := as.IDate(dt_partida_real)]
df2 <- df2[ between(date, as.IDate('2015-01-01'), as.IDate('2018-12-31')) ]
df2[, date := paste0("2030-", format(date, "%m-%d"))]
df2[, date := as.IDate(date, format="%Y-%m-%d") ]
df2 <- na.omit(df2)

Figure

# plot   ---------------------------------------------
fig_paga <- ggplot() + 
            geom_point( data= df2, aes(x=date, y=nr_bagagem_paga_km , color=nr_ano_partida_real ), alpha=.4, size=1) +
            geom_smooth( data= df2, aes(x=date, y=nr_bagagem_paga_km , color=nr_ano_partida_real, fill=nr_ano_partida_real ), alpha=.4, size=1) +
             scale_y_log10(name="Quantidade de bagagem paga X Km", 
                           labels = unit_format(unit = "")) +
            scale_x_date(date_breaks = "1 months", date_labels = "%b") +
            labs(subtitle ='Bagagem paga', color = "Legend") +
            scale_color_identity(labels = c(2015:2018), name = "", guide = "legend") +
            scale_fill_identity(labels = c(2015:2018), name = "", guide = "legend") +
            geom_vline(xintercept = as.Date('2030-03-14'), color='gray', alpha=.5) +
            theme_classic() +
            theme(panel.grid.minor = element_blank(),
                  axis.text = element_text(size = 7),
                  axis.title=element_text(size=9),
                  plot.background = element_rect(fill='white', colour='white'))

fig_gratis <- ggplot() + 
                geom_point( data= df2, aes(x=date, y=nr_bagagem_gratis_km , color=nr_ano_partida_real ), alpha=.4, size=1) +
                geom_smooth( data= df2, aes(x=date, y=nr_bagagem_gratis_km , color=nr_ano_partida_real, fill=nr_ano_partida_real ), alpha=.4, size=1) +
                scale_y_log10(name="Quantidade de bagagem gratis X Km", 
                              labels = unit_format(unit = "")) +
                scale_x_date(date_breaks = "1 months", date_labels = "%b") +
                labs(subtitle ='Bagagem gratuita', color = "Legend") +
                scale_color_identity(labels = c(2015:2018), name = "", guide = "legend") +
                scale_fill_identity(labels = c(2015:2018), name = "", guide = "legend") +
                geom_vline(xintercept = as.Date('2030-03-14'), color='gray', alpha=.5) +
                theme_classic() +
                theme(panel.grid.minor = element_blank(),
                      axis.text = element_text(size = 7),
                      axis.title=element_text(size=9),
                      plot.background = element_rect(fill='white', colour='white'))


temp <- fig_paga / fig_gratis + plot_layout(guides = 'collect')
ggsave(temp, filename = 'temp.png', dpi=300,
       width = 16, height = 12, units = 'cm')

@rafapereirabr
Copy link
Author

temp

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment