library(tidyverse)
## -- Attaching core tidyverse packages ------------------------ tidyverse 2.0.0 --
## v dplyr     1.1.4     v readr     2.1.6
## v forcats   1.0.1     v stringr   1.6.0
## v ggplot2   4.0.1     v tibble    3.3.1
## v lubridate 1.9.4     v tidyr     1.3.2
## v purrr     1.2.1     
## -- Conflicts ------------------------------------------ tidyverse_conflicts() --
## x dplyr::filter() masks stats::filter()
## x dplyr::lag()    masks stats::lag()
## i Use the conflicted package (<http://conflicted.r-lib.org/>) to force all conflicts to become errors
library(tidyr)
library(dplyr)
library(ggplot2)

library(openintro)
## 载入需要的程序包:airports
## 载入需要的程序包:cherryblossom
## 载入需要的程序包:usdata
library(nycflights13)
library(maps)
## 
## 载入程序包:'maps'
## 
## The following object is masked from 'package:purrr':
## 
##     map
flights_data <- flights %>%
  mutate(date = as.Date(time_hour)) %>%
  filter(origin %in% c("EWR", "JFK", "LGA")) %>%   
  group_by(dest) %>%
  summarise(flights_per_day = n() / n_distinct(date))


airports %>%
  semi_join(flights_data, by = c("faa" = "dest")) %>%
  left_join(flights_data, by = c("faa" = "dest")) %>%
  ggplot(aes(x = lon, y = lat)) +
    borders("state") +
    geom_point(aes(color = flights_per_day), size = 2) +
    scale_color_gradient(low = "red", high = "black") +
    coord_quickmap() +
    labs(
      title = "Average Daily Flights from NYC to U.S. Airports",
      x = "Longitude",
      y = "Latitude",
      color = "Flights per Day"
    ) 
## Warning: `borders()` was deprecated in ggplot2 4.0.0.
## i Please use `annotation_borders()` instead.
## This warning is displayed once per session.
## Call `lifecycle::last_lifecycle_warnings()` to see where this warning was
## generated.