Newer
Older
install.packages(c("nycflights13", "dplyr", "ggplot2"))
library(nycflights13)
library(dplyr)
library(ggplot2)
visualize_airport_delays <- function() {
# Join flights and airports datasets to include latitude and longitude
airport_delays <- flights %>%
filter(!is.na(dep_delay)) %>% # Exclude rows where departure delay is NA
group_by(origin) %>% # Group by airport
summarize(mean_delay = mean(dep_delay, na.rm = TRUE)) %>%
left_join(airports, by = c("origin" = "faa")) # Join with airports for lat/lon data
# Create the ggplot2 plot
ggplot(airport_delays, aes(x = lon, y = lat)) +
geom_point(aes(size = mean_delay, color = mean_delay), alpha = 0.9) +
scale_color_gradient(low = "blue", high = "red") +
labs(title = "Mean Flight Delay by Airport",
x = "Longitude",
y = "Latitude",
size = "Mean Delay (min)",
color = "Mean Delay (min)") +
theme_minimal()
}
visualize_airport_delays()