Assignment 4 Part 2

Author

Asim Maharjan

Assignment 4, Part-2

Loading required libraries

library(dplyr)
library(nycflights23)
library(tidyverse)
library(ggplot2)
data(flights)

Creating one visualization using the data-set.

First counting the total flights and months and changing the month’s number to name.

# Counting
flights |>
  count(month)
# A tibble: 12 × 2
   month     n
   <int> <int>
 1     1 36020
 2     2 34761
 3     3 39514
 4     4 37476
 5     5 38710
 6     6 35921
 7     7 36211
 8     8 36765
 9     9 35505
10    10 36586
11    11 34521
12    12 33362
# Changing the numbers to name of the month, arranging it as the sequence of the month and storing in "monthly" variable
monthly <- flights |>
  mutate(month = factor(month.name[month],
                        levels = month.name))
monthly
# A tibble: 435,352 × 19
    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        1           2038       203      328              3
 2  2023 January     1       18           2300        78      228            135
 3  2023 January     1       31           2344        47      500            426
 4  2023 January     1       33           2140       173      238           2352
 5  2023 January     1       36           2048       228      223           2252
 6  2023 January     1      503            500         3      808            815
 7  2023 January     1      520            510        10      948            949
 8  2023 January     1      524            530        -6      645            710
 9  2023 January     1      537            520        17      926            818
10  2023 January     1      547            545         2      845            852
# ℹ 435,342 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>

Here, I am creating a bar graph.

monthly |>
  ggplot(aes(x = month, y = flight, fill = month)) +
  geom_col() +
# The y-axis was showing scientific values, so this code helps show the real value
  scale_y_continuous(labels = scales::comma) +
# Adding labels to the graph
  labs(
    x = "Month",
    y = "Number of flights",
    title =  "Total Numbers of Flights by Months in 2023",
    caption =  "Source: nycflights23, flights data 2023",
  ) +
  theme_minimal() +
  theme(
# Making some adjustment to make the data clear
    axis.text.x = element_text(angle = 30, hjust = 0.5)
  )

In this assignment, I am creating a visualization of how many flights took place in each month in the year 2023. I started with loading required libraries and data for the visualization. In the process of the visualization, I had to count how many flights records were there each month, so I used “count” function. Then, there was two problems with the “month” column, first having numbers (1 to 12) which I changed using “mutate” function. The numbers changed into real names of the month, but there is one more problem left, the names would not be in sequence like; January, February, March, etc. here also using mutate I sequentially arranged the months and stored in new data-set “monthly”.

Now, it was time for creating a bar-chart, which I find easier. I picked up the “monthly” data-set that I stored recently, and started using “ggplot”. After the “geom_col()” code, I added one more line of code to fix the y-axis which was showing scientific notation as the flight column has huge numbers. Then, I mentioned names for the x and y axis, title and caption. At last, I tried to adjust the name of the month making it a little tilt, so it does not jumble up with the other.

The plot that I want to highlight is the color I used different color using “fill = month”, instead of choosing an individual color for 12 months. The various colors make each month easier to notice and compare. This is the result I got which was quite interesting to code.