Introduction
In this page, we’ll analyze international flight arrivals data using R and ggplot2. The dataset comes from Flightradar24.
Setup
I am making a change here.
Let’s start by loading the necessary libraries and source the custom functions that we will use throughout this tutorial.
library (readr)
library (dplyr)
Attaching package: 'dplyr'
The following objects are masked from 'package:stats':
filter, lag
The following objects are masked from 'package:base':
intersect, setdiff, setequal, union
library (ggplot2)
library (here)
here() starts at C:/IMF-R-Book
source (here ("utils/theme_and_colors_IMF.R" ))
source (here ("utils/fonts.R" ))
We read the CSV file containing flight arrivals data. The str()
function is used to display the structure of the dataset.
jam_flight_arrivals <- read.csv (here ("databases/Flightrader24-flight-arrivals.csv" ))
str (jam_flight_arrivals)
'data.frame': 14 obs. of 4 variables:
$ quarter : chr "2019 Q1" "2019 Q2" "2019 Q3" "2019 Q4" ...
$ international_arrrivals: int 6047 5907 6064 6078 6668 523 2381 2897 3400 4051 ...
$ year : int 2019 2019 2019 2019 2020 2020 2020 2020 2021 2021 ...
$ quarter_q : chr "Q1" "Q2" "Q3" "Q4" ...
The current dataset is from 2019Q1 to 2022Q2. What if we want to present selected quarters and years only? We use filter to truncate the data. For example if we want to exclude 2022Q2, we do this:
filtered_data <- jam_flight_arrivals %>%
filter (quarter != "2022 Q2" )
Creating a Grouped Bar Chart
Now, let’s create a grouped bar chart to visualize international flight arrivals over different quarters to compare pre-pandemic and post-pandemic flight activities.
fig_jam_flight_arrivals <- filtered_data %>%
ggplot (aes (fill = quarter_q, x = year, y = international_arrrivals)) +
geom_col (position = position_dodge2 (preserve = "single" , width = 0.7 , padding = 0.2 ), color = "black" , width = 0.75 ) +
scale_x_continuous (limits = c (2018.5 , 2022.5 ), breaks = 2019 : 2022 ) +
scale_y_continuous (limits = c (0 , 8000 ), breaks = seq (0 , 8000 , 2000 ), expand = c (0 , 0 )) +
labs (
title = "International Flight Arrivals" ,
subtitle = "(Number of flights)" ,
caption = "Source: Flightradar24." ,
x = "" ,
y = ""
) +
scale_fill_manual (values = A4_colors_bar) +
theme_imf () +
theme (
legend.title = element_blank (),
legend.position = c (0.93 , 0.8 ),
legend.direction = "vertical" ,
legend.background = element_blank (),
legend.key.height = unit (0.75 , "cm" ),
legend.key.width = unit (0.85 , "cm" ),
legend.spacing.x = unit (0.15 , "cm" ),
legend.spacing.y = unit (0.25 , "cm" )
)
plot (fig_jam_flight_arrivals)
Save the Plot
Finally, let’s save the plot as an image file.
ggsave (
here ("figures/figure-flight_arrivals.png" ),
plot = fig_jam_flight_arrivals,
width = 9.51 ,
height = 6.9 ,
dpi = 600 ,
units = "in"
)
In summary, the grouped bar chart effectively illustrates the trends in international flight arrivals, showcasing fluctuations over various quarters from 2019 to 2022. The visualization provides a clear and concise representation of the data, aiding in the interpretation of key insights.