full usa map in R

I am trying to plot the dataset nycflight 2013. However I can't show the full usa map in r. Airports that are located in Hawaii and Alaska are not showing.

this is my code:

library(tidyverse)
library(plotly)

#reading
flight<-read.csv('flight.csv')
airports<-read.csv('airports.csv')
flighsort<-flight[order(flight$airline),]


#airports locations
airports<-read.csv('airports.csv')


#clearning data for map
#converting lon_Dest_airport from factor to character to map it
AirportsMap <- flight %>% 
  mutate(lon_Dest_airport = parse_number(as.character(lon_Dest_airport)))

#cleaning the original file to get the number of flights between each two airports
AirportsMap<-AirportsMap %>% group_by(origin_airport,dest_airport,lon_origin_airport,lat_origin_airport,
                                 lon_Dest_airport,lat_Dest_airport) %>% tally()
#drawing the map
geo <- list(
  scope = 'usa',
  projection = list(type = 'world'),
  showland = TRUE,
  landcolor = toRGB("gray95"),
  countrycolor = toRGB("gray80")
)

#adding marker for the three origin airports 
plot_geo(locationmode = 'USA-states') %>% 
  add_markers(
    data=airports, x = ~lon, y = ~lat, text=~airport,size = 0.1,
    hoverinfo = "text",alpha = 0.5) %>%
#adding flights routes
  add_segments(
    data = AirportsMap,
    x = ~lon_origin_airport, xend = ~lon_Dest_airport,
    y = ~lat_origin_airport, yend = ~lat_Dest_airport,
    alpha = 0.3
  ) %>%
#adding a title
  layout(
    title = 'NYC Flights 2013<br>(Hover for airport names)',
    geo = geo, showlegend = FALSE
  )

any help is appreciated. thanks

In order to get AK & HI to display, they both had to be elided and, at least for Alaska, scaled. So neither Anchorage or Honolulu have their long/lat in the same frame of reference as the rest of the US.

What you'd have to do is to adjust their locations to their new homes. If you are only mapping from NYC, that should be too much bother. If you want all origin/pairs you need to find out the projection, hopefully in meters and apply a function to do the offsets.

thanks but I don't understand your answer. do you think I should zoom out the map and that should solve the problem?

Yes, if you restored AK & HI to their actual positions. But there are complications. A small part of AK is actually EAST of the prime meridian. Your map view port would also be so large that you would lose most of the detail.

Right now your long/lat pairs from any city to AK/HI are x,y, but because AK/HI have been relocated, their coordinates are long^{+adj_long),lat(-adj_lat)} in the case of AK and long^{+adj_long),lat(+adj_lat)} for HI. My original point was that you'd have to make the same adjustments to AK/HI from every origin.

I wish I could understand your answer. Thanks technocrat.

I'm sorry to be obscure.

  1. AK & HI are not where they should be, right?
  2. Origin airports are looking for them in their correct positions, far north and west in the case of AK and far west in the case of HI.
  3. If you restore AK and HI to their actual positions, the origin airport will point to them correctly.
  4. The problem is that the connections to those airports will be much, much longer.
  5. As a result, your map is 'zoomed out' and while routes to/from AK/HI are now correct and visible and lower 48 routes are correct, the lower 48 routes will look bunched up.
  6. To avoid both problems, you have to pretend that AK/HI are really where you want to display them.
  7. This means you have to tell the origin airports to change the actual coordinates of AK/HI to the pretend coordinates.
  8. How much trouble this entails depends on the number of origin airports. There are not that many. SEA, SFO, LAX account for many of them; other origins tend to connect through these gateways.
1 Like

thank you. Now it's fixed :slight_smile:

This topic was automatically closed 7 days after the last reply. New replies are no longer allowed.