How to build lines in leaflet R?

I am making a map in R using the booklet package. The map basically displays a line from China to the USA. But this is not as I expect. The image of the map is shown below. enter image description here

SO, you can see the line between China and the USA, but it crosses land, not a direct sea route.

Code for creating a map below:

library(leaflet) structure(list(lat = c(21.4982662200928, 25.3100662231445, 25.8857326507568, 33.5610008239746, 33.9683494567871, 46.2030830383301), lng = c(121.90234375, 131.111709594727, 133.618789672852, 159.100082397461, 165.190643310547, -123.813652038574), row_rank = structure(1:6, .Label = c("1", "2", "3", "4", "5", "6"), class = "factor")), .Names = c("lat", "lng", "row_rank"), row.names = c(NA, -6L), class = "data.frame") map <- leaflet() %>% addTiles(urlTemplate ="http://server.arcgisonline.com/ArcGIS/rest/services/Specialty/DeLorme_World_Base_Map/MapServer/tile/{z}/{y}/{x}") #"http://{s}.tile.openstreetmap.org/{z}/{x}/{y}.png" map <- map %>% addCircleMarkers(data=df1, radius = 8, color = 'red', fill = TRUE, label = ~as.character(row_rank), labelOptions=c(noHide=TRUE)) %>% addPolylines(data=df1, lng = ~lng, lat = ~lat) map 

How can i fix this?

+7
r leaflet
source share
1 answer

This may be a little unusual, but I added 360 degrees to the 6th point of longitude, so -123.813652038574 + 360 = 236.813652038574 and changed this value in the data frame, and he built what I imagine, looking for a direct sea route, instead having to double back across continents.

 library(leaflet) df1 <- structure(list(lat = c(21.4982662200928, 25.3100662231445, 25.8857326507568, 33.5610008239746, 33.9683494567871, 46.2030830383301), lng = c(121.90234375, 131.111709594727, 133.618789672852, 159.100082397461, 165.190643310547, -123.813652038574 + 360), row_rank = structure(1:6, .Label = c("1", "2", "3", "4", "5", "6"), class = "factor")), .Names = c("lat", "lng", "row_rank"), row.names = c(NA, -6L), class = "data.frame") map <- leaflet() %>% addTiles() #"http://{s}.tile.openstreetmap.org/{z}/{x}/{y}.png" map <- map %>% addCircleMarkers(data=df1, radius = 8, color = 'red', fill = TRUE, label = ~as.character(row_rank), labelOptions=c(noHide=TRUE)) %>% addPolylines(data=df1, lng = ~lng, lat = ~lat) map 
+6
source share

All Articles