Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to plot lines correctly in leaflet R?

Tags:

r

leaflet

I am making a map in R using leaflet package. The map basically plots a line from China to USA. But it is not coming in the way that I expect. Below is the image of map.enter image description here

SO you can see a line between China to USA, but it is crossing the land areas, instead of the direct sea path.

The code for producing the map is 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 correct this?

like image 995
Kumar Manglam Avatar asked Aug 10 '16 13:08

Kumar Manglam


1 Answers

This may be a little unusual, but I added 360 degrees to the 6th point's longitude, so -123.813652038574 + 360 = 236.813652038574, and changed that value in the data frame, and it plotted what I imagine to be what you're looking for - a direct sea route, instead of doubling 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
like image 107
Sam Avatar answered Oct 02 '22 10:10

Sam