Adding great circle routes as polylines in Leaflet
Ben and Kent, thank you so much for your replies. Both work!
@Ben next time will make sure the code is more ?calorie-free? :)
@Kent Johnson <kent3737 at gmail.com> I modified the weight for the lines by
using a scaling factor.
myScale <- function(x){(x-min(x))/(max(x)-min(x))}
ByRoute$AvgTCE <- myScale(ByRoute$AvgTCE)*5 + 1
@Kent Johnson <kent3737 at gmail.com> yes, this is shipping data and you are
right, great circle routes are not the best visualization. The problem I am
facing is that I have oil trade happening over a period of time from
various ports that I need to visualize. Over the selected period of time,
there are hundreds of voyages being undertaken by ships. Plotting them all
as gc routes looks ugly. My approach has been to classify these ports into
regions, which I drew using mapedit and saved them as SF polygons. I then
calculated their centroids and those are the coordinates in the ByRoute
dataframe. Would appreciate your comments on any other visualization which
you think might be appropriate.
Regards
Dhiraj Khanna
Mob:09873263331
On Mon, Sep 3, 2018 at 10:22 PM Kent Johnson <kent3737 at gmail.com> wrote:
From: Dhiraj Khanna <dhirajkhanna at gmail.com>
To: r-sig-geo at r-project.org
Subject: [R-sig-Geo] Adding great circle routes as polylines in
Leaflet
Message-ID:
<CANHhK31knXB+8ev2LEQWOa3y+ZaUgkJ5noAV=
iX7Z14j_foqZw at mail.gmail.com>
Content-Type: text/plain; charset="utf-8"
I am trying to add great circle routes between various regions in R.
I would like to plot great circle routes between CommencingRegion to
LoadingRegion and then from LoadingRegion to DischargeRegion for every
row.
Additionally, every row needs to be in a different color and the thickness
needs to be proportional to AvgTCE. The last 6 variables in the data above
are the coordinates in Lat Long for the commencing, loading and
discharging
regions respectively. I am quite clueless on how to go about achieving
this. This is what I have tried and failed:
leaflet() %>%
addTiles() %>%
for(i in 1:6){
addPolylines(data=gcIntermediate(c(ByRoute$CLon[i],ByRoute$CLat[i]),c(ByRoute$LLon[i],ByRoute$CLat[i]),n=100,addStartEnd
= T,sp=T))
}
Here is a (rather clunky) start:
library(leaflet)
library(geosphere)
library(dplyr)
d = byRoute %>%
rowwise() %>%
do(leg1=gcIntermediate(c(.$CLon, .$CLat), c(.$LLon, .$LLat), n=50,
addStartEnd=TRUE),
leg2=gcIntermediate(c(.$LLon, .$LLat), c(.$DLon, .$DLat), n=50,
addStartEnd=TRUE))
colors=palette()
map = leaflet() %>% addTiles()
for (i in seq_len(nrow(d)))
map = map %>% addPolylines(data=d$leg1[[i]], color=colors[i],
weight=byRoute$AvgTCE[i]/3000-5) %>%
addPolylines(data=d$leg2[[i]], color=colors[i],
weight=byRoute$AvgTCE[i]/3000-5)
map
You will have to split the great circles using something like
the plot_my_connection function here:
https://www.r-graph-gallery.com/how-to-draw-connecting-routes-on-map-with-r-and-great-circles/
Is this shipping data? Maybe great circles are not the correct route...
Kent
Regards Dhiraj Khanna Mob:09873263331