Marco De Virgilis
Marco De Virgilis

Reputation: 1089

Leaflet separate lines

I am trying to plot lines using leaflet, however I am encountering some difficulties in separating the segment. I have an object that looks like this

> head(trips, n=15)
   time.start   time.end long.start long.end lat.start  lat.end  distance time.diff      speed color
1  1476450598 1476450713    9.03913  9.03924  45.61335 45.61362  31.25292       115  0.9783524 green
2  1476450713 1476450727    9.03924  9.03995  45.61362 45.61365  55.38651        14 14.2422459 green
3  1476450727 1476450751    9.03995  9.04005  45.61365 45.61340  28.89870        24  4.3348057 green
4  1476450751 1476450777    9.04005  9.04017  45.61340 45.61406  74.06267        26 10.2548313 green
5  1476450777 1476450873    9.04017  9.03949  45.61406 45.61419  54.89125        96  2.0584219 green
6  1476450873 1476450920    9.03949  9.03496  45.61419 45.61319 369.88687        47 28.3317600 green
7  1476450920 1476450930    9.03496  9.03440  45.61319 45.61295  51.13973        10 18.4103034 green
8  1476450930 1476450932    9.03440  9.03448  45.61295 45.61285  12.75643         2 22.9615714 green
9  1476450932 1476450982    9.03448  9.03495  45.61285 45.61241  61.14351        50  4.4023330 green
10 1476451362 1476451363    9.03553  9.03559  45.61197 45.61188  11.05462         1 39.7966396 green
11 1476451363 1476451373    9.03559  9.03606  45.61188 45.61129  75.18742        10 27.0674701 green
12 1476451373 1476451382    9.03606  9.03712  45.61129 45.61127  82.57276         9 33.0291031 green
13 1476451382 1476451405    9.03712  9.04059  45.61127 45.61095 272.54942        23 42.6599094 green
14 1476451405 1476451412    9.04059  9.04115  45.61095 45.61091  43.83450         7 22.5434586 green
15 1476451412 1476451431    9.04115  9.04440  45.61091 45.61064 254.85994        19 48.2892512 green

This file represents two trips as an example (there are many more but it is just to give an idea), ideally the point no.9 (end of 1st trip) shouldn't be linked to point no.10 (start of 2nd trip). I got this done with the command

   ggmap(mapImageData)+
  geom_segment(data=trips, mapping=aes(y=trips$lat.start, x=trips$long.start, 
                                       yend=trips$lat.end, xend=trips$long.end),color=trips$color,size=1)

in this code mapImageData is the equivalent of the tiles in Leaflet and then I am adding points with the command geom_segment, specifying the initial and final position of each point.

With leaflet I have to use the command addPolylines(data = mydata, lng = ~long, lat = ~lat, weight=1,color="purple"). The difference is that this command wants a column of longitude and a column of latitude, in my example something like this

 head(mydata, n=15)
        lat    long
1  45.61335 9.03913
2  45.61362 9.03924
3  45.61365 9.03995
4  45.61340 9.04005
5  45.61406 9.04017
6  45.61419 9.03949
7  45.61319 9.03496
8  45.61295 9.03440
9  45.61285 9.03448
10 45.61197 9.03553
11 45.61188 9.03559
12 45.61129 9.03606
13 45.61127 9.03712
14 45.61095 9.04059
15 45.61091 9.04115

This, however will connect all the points, how can I tell when not to connect the dots? for example position no. 9 and 10 shouldn't linked to each other. Thanks, Marco

Upvotes: 1

Views: 2027

Answers (1)

GGamba
GGamba

Reputation: 13680

First of all we need a way to distinguish the different trips.
I used

df$group <- c(rep(1, 9), rep(2, 6))

based on what you said, but modify your df as it suits.

After this we are going to add a polyline for each group:

m <- leaflet(df) %>% 
    addTiles() 

for (i in unique(df$group)) {
    m <- m %>% 
        addPolylines(data = df[df$group == i, ], 
                     lng = ~long.start, 
                     lat = ~lat.start)
}

Result:

enter image description here

Despite being a for loop this is quite fast. To speed it up a notch we can use a lapply:

m <- leaflet(df) %>% 
        addTiles()

lapply(unique(df$group), 
       function(x) {
            addPolylines(m, 
                         data = df[df$group == x, ], 
                         lng = ~long.start, 
                         lat = ~ lat.start)
        })
rbenchmark::benchmark(
    apply = {

        m <- leaflet(df) %>% 
            addTiles()

        lapply(unique(df$group), function(x) {
            addPolylines(m, 
                         data = df[df$group == x, ], 
                         lng = ~long.start, 
                         lat = ~ lat.start)
        })
    },
    forcycle = {

        m <- leaflet(df) %>% 
            addTiles() 

        for (i in unique(df$group)) {
            addPolylines(m, 
                         data = df[df$group == i, ], 
                         lng = ~long.start, 
                         lat = ~lat.start)
        }}, 
    replications = 1000)
#>       test replications elapsed relative user.self sys.self user.child
#> 1    apply         1000    2.91    1.000      2.92        0         NA
#> 2 forcycle         1000    3.04    1.045      3.00        0         NA

Upvotes: 4

Related Questions