# library
# Load required libraries
library(ggridges)
library(ggplot2)
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(nycflights13)
# Convert dep_time to hour (dep_time is in HHMM format, e.g., 530 means 5:30 AM)
# Use integer division and modulo to handle dep_time correctly
flights_clean <- flights %>%
filter(!is.na(dep_time)) %>%
mutate(
dep_hour = (dep_time %/% 100) + (dep_time %% 100) / 60 # Extract hours and minutes
)
# Join with airlines dataset to get airline names
flights_clean <- flights_clean %>%
left_join(airlines, by = "carrier")
# Create the ridgeline plot
ggplot(flights_clean, aes(x = dep_hour, y = name, height = ..density.., fill = name)) +
geom_density_ridges(scale = 1.5, alpha = 0.7, size = 0.3) +
labs(
title = "Flight Departure Time Density Throughout the Day by Airline",
x = "Departure Hour",
y = "Airline",
fill = "Airline"
) +
scale_x_continuous(
breaks = c(0, 12), # Only show markers at 0 (midnight) and 12 (noon)
labels = c("Midnight", "Noon") # Custom labels for these markers
) +
theme_ridges() +
theme(legend.position = "none")
## Warning in geom_density_ridges(scale = 1.5, alpha = 0.7, size = 0.3): Ignoring
## unknown parameters: `size`
## Warning: The dot-dot notation (`..density..`) was deprecated in ggplot2 3.4.0.
## ℹ Please use `after_stat(density)` instead.
## This warning is displayed once every 8 hours.
## Call `lifecycle::last_lifecycle_warnings()` to see where this warning was
## generated.
## Picking joint bandwidth of 0.582
