如何为每一行创建折线?

时间:2017-02-27 14:03:21

标签: r shiny leaflet

我正在尝试基于具有航线路线的数据集在世界地图上绘制航线路线(每条路线一行,结构为source_airport_longitude,source_airport_latitude,dest_airport_longitude,dest_airport,纬度)。但是,折线会创建连续的线条,而不是每行的单独线条。您对如何处理此问题有什么建议吗?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)
      )
  })

数据集:

> 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
> 

1 个答案:

答案 0 :(得分:2)

这里最简单的可能是将您的数据转换为Lines包中的sp对象并使用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))