Assignment 4

Author

Mars Onyeabo

Loading the libraries

library(tidyverse)
── Attaching core tidyverse packages ──────────────────────── tidyverse 2.0.0 ──
✔ dplyr     1.1.4     ✔ readr     2.1.5
✔ forcats   1.0.0     ✔ stringr   1.5.1
✔ ggplot2   3.5.1     ✔ tibble    3.2.1
✔ lubridate 1.9.4     ✔ tidyr     1.3.1
✔ purrr     1.0.4     
── Conflicts ────────────────────────────────────────── tidyverse_conflicts() ──
✖ dplyr::filter() masks stats::filter()
✖ dplyr::lag()    masks stats::lag()
ℹ Use the conflicted package (<http://conflicted.r-lib.org/>) to force all conflicts to become errors
library(nycflights23)
data(flights)
glimpse(flights)
Rows: 435,352
Columns: 19
$ year           <int> 2023, 2023, 2023, 2023, 2023, 2023, 2023, 2023, 2023, 2…
$ month          <int> 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1…
$ day            <int> 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1…
$ dep_time       <int> 1, 18, 31, 33, 36, 503, 520, 524, 537, 547, 549, 551, 5…
$ sched_dep_time <int> 2038, 2300, 2344, 2140, 2048, 500, 510, 530, 520, 545, …
$ dep_delay      <dbl> 203, 78, 47, 173, 228, 3, 10, -6, 17, 2, -10, -9, -7, -…
$ arr_time       <int> 328, 228, 500, 238, 223, 808, 948, 645, 926, 845, 905, …
$ sched_arr_time <int> 3, 135, 426, 2352, 2252, 815, 949, 710, 818, 852, 901, …
$ arr_delay      <dbl> 205, 53, 34, 166, 211, -7, -1, -25, 68, -7, 4, -13, -14…
$ carrier        <chr> "UA", "DL", "B6", "B6", "UA", "AA", "B6", "AA", "UA", "…
$ flight         <int> 628, 393, 371, 1053, 219, 499, 996, 981, 206, 225, 800,…
$ tailnum        <chr> "N25201", "N830DN", "N807JB", "N265JB", "N17730", "N925…
$ origin         <chr> "EWR", "JFK", "JFK", "JFK", "EWR", "EWR", "JFK", "EWR",…
$ dest           <chr> "SMF", "ATL", "BQN", "CHS", "DTW", "MIA", "BQN", "ORD",…
$ air_time       <dbl> 367, 108, 190, 108, 80, 154, 192, 119, 258, 157, 164, 1…
$ distance       <dbl> 2500, 760, 1576, 636, 488, 1085, 1576, 719, 1400, 1065,…
$ hour           <dbl> 20, 23, 23, 21, 20, 5, 5, 5, 5, 5, 5, 6, 5, 6, 6, 6, 6,…
$ minute         <dbl> 38, 0, 44, 40, 48, 0, 10, 30, 20, 45, 59, 0, 59, 0, 0, …
$ time_hour      <dttm> 2023-01-01 20:00:00, 2023-01-01 23:00:00, 2023-01-01 2…

Sorting the data

flightsmaller <- flights |>
  filter(origin == "JFK" & dest == "IAD") |>
  #change the month from numbers to words
  mutate(month = month.name[month]) |>
  group_by(month) |>
  #sort in chronological order
  mutate(month = factor(month, levels = month.name)) |>
  arrange(month)

Visualization

flightsmaller
# A tibble: 1,029 × 19
# Groups:   month [12]
    year month     day dep_time sched_dep_time dep_delay arr_time sched_arr_time
   <int> <fct>   <int>    <int>          <int>     <dbl>    <int>          <int>
 1  2023 January     1     1450           1455        -5     1557           1633
 2  2023 January     2     1448           1455        -7     1606           1633
 3  2023 January     3     1451           1455        -4     1609           1633
 4  2023 January     4     1455           1455         0     1618           1633
 5  2023 January     5     1454           1455        -1     1611           1633
 6  2023 January     6     1457           1455         2     1634           1633
 7  2023 January     7     1450           1455        -5     1616           1633
 8  2023 January     8      920            929        -9     1100           1053
 9  2023 January     8     1253           1255        -2     1406           1420
10  2023 January     8     1504           1455         9     1640           1633
# ℹ 1,019 more rows
# ℹ 11 more variables: arr_delay <dbl>, carrier <chr>, flight <int>,
#   tailnum <chr>, origin <chr>, dest <chr>, air_time <dbl>, distance <dbl>,
#   hour <dbl>, minute <dbl>, time_hour <dttm>
flightsmaller |>
  ggplot(aes(x = month)) + geom_bar(binwidth = 1, color = "black", fill = "skyblue") +
  labs(y = "Flights",
       title = "Flights from JFK Airport to Dulles Airport by month",
       caption = "Source: Bureau of Transportation Statistics (RITA/BTS), OpenFlights, and the FAA flight registry")
Warning in geom_bar(binwidth = 1, color = "black", fill = "skyblue"): Ignoring
unknown parameters: `binwidth`

I decided to visualize some data from the nycflights23 package through a bar plot. First, I filtered the data down to only include flights that departed from John F. Kennedy International Airport in New York and landed at Dulles International Airport in Virginia. For my visualization, I changed the month variable to be words instead of numbers to make it better visually. Then, I sorted it to display in chronilogical order on the graph. One thing I’d like to point out is March had the most flights out of the year when you would expect it to be December due to the holidays.