library(tidyverse)
## ── 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.5.1 ✔ 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(nycflights23)
## Warning: package 'nycflights23' was built under R version 4.3.3
data(flights)
new_flights <- flights |>
filter(month >= 12)
by_hour <- new_flights |>
group_by(time_hour) |>
summarise(count = n()) |>
arrange(time_hour)
head(by_hour)
## # A tibble: 6 × 2
## time_hour count
## <dttm> <int>
## 1 2023-12-01 05:00:00 9
## 2 2023-12-01 06:00:00 86
## 3 2023-12-01 07:00:00 85
## 4 2023-12-01 08:00:00 92
## 5 2023-12-01 09:00:00 81
## 6 2023-12-01 10:00:00 66
by_hour <- by_hour |>
mutate(
date = as.Date(time_hour),
hour = format(time_hour, "%H")
)
library(viridis)
## Loading required package: viridisLite
ggplot(by_hour, aes(x = date, y = hour, fill = count)) +
geom_tile() +
scale_fill_viridis_c(option = "A") +
labs(title = "Flight Frequency Heatmap in December 2023",
caption = "Source: FAA Aircraft registry",
x = "Date",
y = "Hour of Day",
fill = "Flight Frequency #") +
theme_minimal()
I acknowledge the use of ChatGPT(https://chat.openai.com/) to search for the “scale_fill_viridis_c(option =”A”)” and “format(time_hour,”%H”)” option in order to add better color and solve the issue of adding dates to my heatmap.