Leaflet

Author

Hypatia X. Opcional

Hello World for Maps

Code
m <- leaflet() |>
    addTiles() |>
    addMarkers(lng=-77.0171, lat=38.9041, popup= "Marks the Spot")

m
Code
airport_locations<-airports |>
  select(faa, lon, lat) 

airport_long_lat<- airports |> select(lon, lat)
airport_long_lat
# A tibble: 1,458 × 2
      lon   lat
    <dbl> <dbl>
 1  -80.6  41.1
 2  -85.7  32.5
 3  -88.1  42.0
 4  -74.4  41.4
 5  -81.4  31.1
 6  -82.2  36.4
 7  -84.5  41.5
 8  -76.8  42.9
 9  -76.6  39.8
10 -123.   48.1
# ℹ 1,448 more rows

It is unlikely that flights from NYC will go to every airport in the FAA data base. So we need to do a little data wrangling. What we want, is a list of the airports which are destinations from NYC.

Code
dests <- flights |> select(dest) |> group_by(dest) |> summarize(n())
dselect <- select(dests, dest) |> mutate(faa = dest)|> select(faa)
dselect
# A tibble: 105 × 1
   faa  
   <chr>
 1 ABQ  
 2 ACK  
 3 ALB  
 4 ANC  
 5 ATL  
 6 AUS  
 7 AVL  
 8 BDL  
 9 BGR  
10 BHM  
# ℹ 95 more rows
Code
airport_locations
# A tibble: 1,458 × 3
   faa      lon   lat
   <chr>  <dbl> <dbl>
 1 04G    -80.6  41.1
 2 06A    -85.7  32.5
 3 06C    -88.1  42.0
 4 06N    -74.4  41.4
 5 09J    -81.4  31.1
 6 0A9    -82.2  36.4
 7 0G6    -84.5  41.5
 8 0G7    -76.8  42.9
 9 0P2    -76.6  39.8
10 0S9   -123.   48.1
# ℹ 1,448 more rows
Code
locs <- left_join(dselect,airport_locations)
Joining with `by = join_by(faa)`
Code
locs
# A tibble: 105 × 3
   faa      lon   lat
   <chr>  <dbl> <dbl>
 1 ABQ   -107.   35.0
 2 ACK    -70.1  41.3
 3 ALB    -73.8  42.7
 4 ANC   -150.   61.2
 5 ATL    -84.4  33.6
 6 AUS    -97.7  30.2
 7 AVL    -82.5  35.4
 8 BDL    -72.7  41.9
 9 BGR    -68.8  44.8
10 BHM    -86.8  33.6
# ℹ 95 more rows
Code
m <- leaflet(locs) |> addTiles() |> 
    addCircleMarkers(lng = ~lon, lat = ~lat, popup = ~faa)
Warning in validateCoords(lng, lat, funcName): Data contains 4 rows with either
missing or invalid lat/lon values and will be ignored
Code
m

Modify the above code so that the pop up gives the proper name of the airport in addition to the faa code.