2

I'm trying plot flight routes on a worldmap based on a dataset with flight routes (one row for each route, structured as source_airport_longitude, source_airport_latitude, dest_airport_longitude, dest_airport,latitude). However, polylines creates continuous lines, instead of an individual line for each row. Do you have suggestions for how to approach this?enter image description here

  filteredData <- reactive({
      dt.routes.small[source_airport == input$airport]
  })

  output$map <- renderLeaflet({
    leaflet(data = dt.routes.small) %>%
      addTiles() %>%
      addPolylines(lng = c(dt.routes.small$source_airport_longitude, 
                           dt.routes.small$dest_airport_longitude),
                   lat = c(dt.routes.small$source_airport_latitude, 
                           dt.routes.small$dest_airport_latitude)
      )
  })
  observe({
    leafletProxy("map") %>%
      clearShapes() %>%
      addPolylines(lng = c(filteredData()$source_airport_longitude, 
                           filteredData()$dest_airport_longitude),
                   lat = c(filteredData()$source_airport_latitude, 
                           filteredData()$dest_airport_latitude)
      )
  })

Dataset:

> head(dt.routes.small, 10)
   source_airport dest_airport
1             MAG          GKA
2             HGU          GKA
3             LAE          GKA
4             POM          GKA
5             POM          GKA
6             GKA          MAG
7             HGU          MAG
8             LAE          MAG
9             POM          MAG
10            WWK          MAG
                           source_airport_name dest_airport_name
1                               Madang Airport    Goroka Airport
2                 Mount Hagen Kagamuga Airport    Goroka Airport
3                               Nadzab Airport    Goroka Airport
4  Port Moresby Jacksons International Airport    Goroka Airport
5  Port Moresby Jacksons International Airport    Goroka Airport
6                               Goroka Airport    Madang Airport
7                 Mount Hagen Kagamuga Airport    Madang Airport
8                               Nadzab Airport    Madang Airport
9  Port Moresby Jacksons International Airport    Madang Airport
10                 Wewak International Airport    Madang Airport
   source_airport_city dest_airport_city source_airport_country
1               Madang            Goroka       Papua New Guinea
2          Mount Hagen            Goroka       Papua New Guinea
3               Nadzab            Goroka       Papua New Guinea
4         Port Moresby            Goroka       Papua New Guinea
5         Port Moresby            Goroka       Papua New Guinea
6               Goroka            Madang       Papua New Guinea
7          Mount Hagen            Madang       Papua New Guinea
8               Nadzab            Madang       Papua New Guinea
9         Port Moresby            Madang       Papua New Guinea
10               Wewak            Madang       Papua New Guinea
   dest_airport_country source_airport_longitude
1      Papua New Guinea                  145.789
2      Papua New Guinea                  144.296
3      Papua New Guinea                  146.726
4      Papua New Guinea                  147.220
5      Papua New Guinea                  147.220
6      Papua New Guinea                  145.392
7      Papua New Guinea                  144.296
8      Papua New Guinea                  146.726
9      Papua New Guinea                  147.220
10     Papua New Guinea                  143.669
   source_airport_latitude dest_airport_longitude dest_airport_latitude
1                -5.207080                145.392              -6.08169
2                -5.826790                145.392              -6.08169
3                -6.569803                145.392              -6.08169
4                -9.443380                145.392              -6.08169
5                -9.443380                145.392              -6.08169
6                -6.081690                145.789              -5.20708
7                -5.826790                145.789              -5.20708
8                -6.569803                145.789              -5.20708
9                -9.443380                145.789              -5.20708
10               -3.583830                145.789              -5.20708
   n_airlines distance airline_name id
1           1      107 Airlines PNG  1
2           1      125 Airlines PNG  2
3           1      157 Airlines PNG  3
4           2      425  Air Niugini  4
5           2      425 Airlines PNG  5
6           1      107 Airlines PNG  6
7           1      179 Airlines PNG  7
8           1      184 Airlines PNG  8
9           1      497  Air Niugini  9
10          2      297  Air Niugini 10
> 
Sander
  • 67
  • 10
  • Look at [this answer](http://stackoverflow.com/questions/32275213/how-do-i-connect-two-coordinates-with-a-line-using-leaflet-in-r), you can add a group variable in your dataframe and each beginning and end of line needs to be in the same group. – NicE Feb 27 '17 at 15:23
  • Thanks. I just tried adding `group = filteredData()$id` to addPolylines but unfortunately it still connects all the lines :/ – Sander Feb 27 '17 at 16:09
  • 1
    Maybe post a few lines of your `filteredData()` – NicE Feb 27 '17 at 16:13
  • Columns: source_airport dest_airport source_airport_name dest_airport_name source_airport_city dest_airport_city source_airport_country dest_airport_country source_airport_longitude source_airport_latitude dest_airport_longitude dest_airport_latitude n_airlines distance airline_name id Row 1: MAG GKA Madang Airport Goroka Airport Madang Goroka Papua New Guinea Papua New Guinea 145.789001 -5.207080 145.3920 -6.081690 1 107 Airlines PNG 1 – Sander Feb 27 '17 at 16:24
  • It's hard to say what id is with only one line, edit your post and add the first 10 or so with `head(data,10)`, the formatting will also be nicer. – NicE Feb 27 '17 at 18:20
  • Sorry, posted it above! – Sander Feb 27 '17 at 21:43

1 Answers1

3

The easiest here is probably to transform your data into a Lines object from the sp packages and the use addPolylines:

rm(list=ls())
data <- data.frame(source_airport=c("MAG","HGU","CDG"),dest_airport=c("DKA","DKA","JFK"),
                   source_airport_longitude=c(145.789,144.296,2.538),
                   source_airport_latitude=c(-5.207080,-5.826790,49.008),
                   dest_airport_longitude=c(145.392,145.392,-73.49),
                   dest_airport_latitude=c(-6.08169,-6.08169,40.38),
                   id=c(1,2,4),stringsAsFactors = F)



flights_lines <- apply(data,1,function(x){
  points <- data.frame(lng=as.numeric(c(x["source_airport_longitude"], 
                                        x["dest_airport_longitude"])),
                       lat=as.numeric(c(x["source_airport_latitude"], 
                                        x["dest_airport_latitude"])),stringsAsFactors = F)
  coordinates(points) <- c("lng","lat")
  Lines(Line(points),ID=x["id"])
})

row.names(data) <- data$id
flights_lines <- SpatialLinesDataFrame(SpatialLines(flights_lines),data)


leaflet() %>%
  addTiles() %>%
  addPolylines(data=flights_lines,label=~as.character(id))
NicE
  • 21,165
  • 3
  • 51
  • 68
  • Thanks! Works perfectly. Any idea on how to include variables from the original data to add labels with info? – Sander Feb 28 '17 at 13:59
  • 1
    @Sander I've edited the code, you need to use a `SpatialLinesDataFrame` if you want to keep the rest of the info. – NicE Feb 28 '17 at 18:30