Flow map(Travel Path) Using Lat and Long in R

前端 未结 5 1769
臣服心动
臣服心动 2021-01-19 07:55

I am trying to plot flow map (for singapore) . I have Entry(Lat,Long) and Exit (Lat,long). I am trying to map the flow from entry to exit in singapore map.

s         


        
5条回答
  •  温柔的废话
    2021-01-19 08:56

    Alternative answer using leaflet and geosphere

    #get Packages
    require(leaflet)
    require(geosphere)
    
    #format data
    a$Entry_Station_Long = as.numeric(as.character(a$Entry_Station_Long))
    a$Entry_Station_Lat = as.numeric(as.character(a$Entry_Station_Lat))
    a$Exit_Station_Long = as.numeric(as.character(a$Exit_Station_Long))
    a$Exit_Station_Lat = as.numeric(as.character(a$Exit_Station_Lat))
    a$id = as.factor(as.numeric(as.factor(a$token_id)))
    
    #create some colors
    factpal <- colorFactor(heat.colors(30), pathList$id)
    
    #create a list of interpolated paths
    pathList = NULL
    for(i in 1:nrow(a))
    {
    tmp = gcIntermediate(c(a$Entry_Station_Long[i],
                     a$Entry_Station_Lat[i]),
                   c(a$Exit_Station_Long[i],
                     a$Exit_Station_Lat[i]),n = 25,
                   addStartEnd=TRUE)
    tmp = data.frame(tmp)
    tmp$id = a[i,]$id
    tmp$color = factpal(a[i,]$id)
    pathList = c(pathList,list(tmp))
    }
    
    #create empty base leaflet object
    leaflet() %>% addTiles() -> lf
    
    #add each entry of pathlist to the leaflet object
    for (path in pathList)
    {
      lf %>% addPolylines(data = path,
                          lng = ~lon, 
                          lat = ~lat,
                          color = ~color) -> lf
    
    }
    #show output
    lf
    

    Note that as I mentioned before there is no way of geosphering the paths in such a small locality - the great circles are effectively straight lines. If you want the rounded edges for sake of aesthetics you may have to use the geom_curve way described in my other answer.

提交回复
热议问题