I am trying to use Leaflet package in R to draw a amp and connect the markers given the latitude and longitude information in the table below.
| Observation | InitialLat | InitialLong | NewLat | NewLong | |-------------|------------|-------------|-----------|-----------| | A | 62.469722 | 6.187194 | 51.4749 | -0.221619 | | B | 48.0975 | 16.3108 | 51.4882 | -0.302621 | | C | 36.84 | -2.435278 | 50.861822 | -0.083278 | | D | 50.834194 | 4.298361 | 54.9756 | -1.62179 | | E | 50.834194 | 4.298361 | 54.9756 | -1.62179 | | F | 50.834194 | 4.298361 | 51.4882 | -0.302621 | | G | 47.460427 | -0.530804 | 51.44 | -2.62021 | | H | 51.5549 | -0.108436 | 53.4281 | -1.36172 | | I | 51.5549 | -0.108436 | 52.9399 | -1.13258 | | J | 51.5549 | -0.108436 | 51.889839 | -0.193608 | | | 51.5549 | -0.108436 | 52.0544 | 1.14554 |
I want to draw lines from an initial point given by the coordinates in the InitialLat
and InitialLong
columns to an end point given by the NewLat
and NewLong
columns.
Here is my current R code which only draws the markers on the map.
library(leaflet) map3 = leaflet(data) %>% addTiles() map3 %>% addMarkers(~InitialLong,~InitialLat, popup=~Observation)
Here is an alternative way using the
leaflet
package. I just took two data points in your data for the purpose of demonstration.I changed the format of
mydf
and create a new data frame for leaflet. You can reshape your data in various ways.I trimmed the interactive map I got. Please see the map below. Although two lines are connected in this image, they are separated. If you run the code and zoom in, you will see that the two lines are separated.
Leaflet can add lines using the
addPolylines
function. The problem with this is it assumes every line is connected - you will get them all linked.The best way to fix this (AFAIK) is to use a loop:
EDIT: There is also an easier way using the points_to_line function by Kyle Walker (see the very bottom for a pasted copy of the code).
First reshape the data, so the starts and ends are in the same columns:
Then call
points_to_line
Now plot:
Source of points_to_line by Kyle Walker:
Depending on what the purpose of the lines is, another great option is gcIntermediate(). It outputs a CURVED SpatialLines object, based on the curvature of the earth. Not great for directions though. SpatialLines class objects work very well with Leaflet. See here for an excellent example. I've posted a modified form, that starts with the data frame from Paul Reiners.
I know this was asked a year ago but I had the same question and figured out how to do it in leaflet.
You are first going to have to adjust your dataframe because addPolyline just connects all the coordinates in a sequence. I will make a dataframe with 4 separate ending locations for the purpose of this demonstration.
Next, I am going to create a data frame with the central location of the same size (4 in this example) of the destination locations. I will explain why I'm doing this soon
The reason why I am doing this is because the addPolylines feature will connect all the coordinates in a sequence. The way to get around this in order to create the image you described is by starting at the starting point, then going to destination point, and then back to the starting point, and then to the next destination point. In order to create the dataframe to do this, we will have to interlace the two dataframes by placing in rows as such:
starting point - destination point 1 - starting point - destination point 2 - and so forth...
The way I will do is create a key for both data frames. For the origin dataframe, I will start at 1, and increment by 2 (e.g., 1 3 5 7). For the destination dataframe, I will start at 2 and increment by 2 (e.g., 2, 4, 6, 8). I will then combine the 2 dataframes using a UNION all. I will then sort by my sequence to make every other row the starting point. I am going to use sqldf for this because that is what I'm comfortable with. There may be a more efficient way.
The new dataframe looks like this Notice how the origin locations are interwoven between the destination
And finally, you can make your map:
And finally it should look like this I hope this helps anyone who is looking to do something like this in the future
Think this one is what you want:
And it shows: Network Connection using Leaflet