carrier_labels <- some_flights |>
group_by(carrier) |>
summarize(mean_time = mean(air_time)) |>
mutate(name = c("9E • Endeavor Air", "B6 • JetBlue", "HA • Hawaiian Airlines"))
some_flights |>
ggplot(aes(x = carrier, y = air_time, fill = carrier)) +
geom_jitter(width = .2, shape = 21, color = "black", stroke = .4) +
geom_hline(yintercept = min_time, linetype = "dashed", alpha = .4) +
geom_hline(yintercept = max_time, linetype = "dashed", alpha = .4) +
annotate("text", x = 3.5, y = min_time, hjust = 0,
label = paste0("◀ shortest: ", min_time, " min"),
color = "grey30", size = 4) +
annotate("text", x = 3.5, y = max_time, hjust = 1,
label = paste0("longest: ", max_time, " min ▶"),
color = "grey30", size = 4) +
geom_text(data = carrier_labels,
aes(x = carrier, y = mean_time, label = name),
inherit.aes = FALSE,
hjust = 0.5, nudge_x = -0.35,
fontface = "bold", size = 4.2) +
geom_text(data = carrier_labels,
aes(x = carrier, y = mean_time,
label = paste0("avg: ", round(mean_time), " min")),
inherit.aes = FALSE,
hjust = 0.5, nudge_x = -0.6,
size = 3.5, color = "grey30") +
scale_x_discrete(expand = expansion(add = c(0.8, 1)), labels = NULL) +
scale_y_continuous(labels = scales::label_number(suffix = " min")) +
scale_fill_manual(values = c("9E" = "#15616d", "B6" = "#ff7d00", "HA" = "#78290f")) +
guides(fill = "none") +
labs(x = NULL, y = NULL, title = "Air time by carrier") +
coord_flip(clip = "off") +
theme_minimal()