── 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.4.4 ✔ tibble 3.2.1
✔ lubridate 1.9.3 ✔ tidyr 1.3.1
✔ purrr 1.0.2
── 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(nycflights13)data(flights)
#Heatmap
# Load required librarieslibrary(tidyverse)library(nycflights13)# Loading the flights datasetdata(flights)# Dplyr command to filter and summarize data (Chat GBT helped with some of this, mainly the is.na part, I didn't know how I could have changed that)heatmap_data <- flights %>%filter(month %in%c(7, 8, 9), !is.na(dep_delay)) %>%group_by(day, hour) %>%summarize(avg_delay =mean(dep_delay, na.rm =TRUE))
`summarise()` has grouped output by 'day'. You can override using the `.groups`
argument.
# Creating the heatmap plotheatmap_plot <-ggplot(heatmap_data, aes(x = hour, y = day, fill = avg_delay)) +geom_tile() +scale_fill_gradient(low ="white", high ="blue") +labs(x ="Hour of Day",y ="Day of Month",title ="Average Departure Delay Heatmap",caption ="Data source: nycflights13" ) +theme_minimal() +theme(legend.position ="right")# Displaying itheatmap_plot
#This code filters the flights dataset for the months of July, August, and September, then groups the data by day and hour to calculate the average departure delay. The resulting heatmap represents the average departure delay for each hour of the day and each day of the month. One aspect of the plot that I highlight are the colors in the heatmap that indicate the magnitude of the average delay, with a gradient from white (low delay) to blue (high delay). The x-axis represents the hour of the day, the y-axis represents the day of the month, and the legend provides a reference for the color scale.