library(dplyr)
library(nycflights23)
library(tidyverse)
library(ggplot2)
data(flights)Assignment 4 Part 2
Assignment 4, Part-2
Loading required libraries
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)
)