2

I like Leaflet package it is interactive, with good basemap and high zooming power. Thus, I want to use leaflet package to produce a travel route. I know map and ggmap packages can be used to produce a map such as this:Example of map to produce In ggmap, there is gcintermediate function that connects draws multiple lines simultaneously, but I am not sure if similar function is in leaflet package. I tried using leaflet package, but could only use it to connect two points. So, my question is that can leaflet package be used to visualize multiple routes simultaneously?

Below is my attempt:

library(leaflet)
    p1=leaflet()%>%
      addTiles() %>%

mydf2=data.frame(inter)

#mydf2 is below
          lon         lat
1   -157.8583  21.3069444
2   -158.1350  20.7322775
3   -158.4096  20.1571716
4   -158.6822  19.5816431
5   -158.9528  19.0057081
6   -159.2216  18.4293823
7   -159.4885  17.8526808
8   -159.7538  17.2756187
9   -160.0173  16.6982106
10  -160.2793  16.1204708
11  -160.5398  15.5424133
12  -160.7988  14.9640518
13  -161.0564  14.3853998
14  -161.3127  13.8064705
15  -161.5677  13.2272769
16  -161.8215  12.6478316
17  -162.0742  12.0681472
18  -162.3258  11.4882361
19  -162.5763  10.9081102
20  -162.8259  10.3277815
21  -163.0745   9.7472619
22  -163.3223   9.1665628
23  -163.5692   8.5856958
24  -163.8154   8.0046721
25  -164.0609   7.4235030
26  -164.3058   6.8421994
27  -164.5501   6.2607724
28  -164.7938   5.6792327
29  -165.0370   5.0975911
30  -165.2798   4.5158583
31  -165.5222   3.9340449
32  -165.7643   3.3521614
33  -166.0060   2.7702183
34  -166.2476   2.1882260
35  -166.4889   1.6061948
36  -166.7301   1.0241352
37  -166.9712   0.4420575
38  -167.2123  -0.1400281
39  -167.4534  -0.7221112
40  -167.6946  -1.3041815
41  -167.9359  -1.8862287
42  -168.1773  -2.4682425
43  -168.4189  -3.0502124
44  -168.6608  -3.6321282
45  -168.9030  -4.2139792
46  -169.1456  -4.7957552
47  -169.3886  -5.3774454
48  -169.6321  -5.9590393
49  -169.8761  -6.5405261
50  -170.1206  -7.1218951
51  -170.3658  -7.7031354
52  -170.6116  -8.2842359
53  -170.8582  -8.8651856
54  -171.1055  -9.4459733
55  -171.3537 -10.0265874
56  -171.6028 -10.6070167
57  -171.8528 -11.1872492
58  -172.1038 -11.7672733
59  -172.3559 -12.3470769
60  -172.6091 -12.9266478
61  -172.8635 -13.5059736
62  -173.1191 -14.0850416
63  -173.3760 -14.6638391
64  -173.6343 -15.2423530
65  -173.8940 -15.8205699
66  -174.1552 -16.3984763
67  -174.4179 -16.9760583
68  -174.6823 -17.5533017
69  -174.9484 -18.1301922
70  -175.2162 -18.7067148
71  -175.4858 -19.2828545
72  -175.7574 -19.8585959
73  -176.0309 -20.4339231
74  -176.3065 -21.0088199
75  -176.5842 -21.5832696
76  -176.8641 -22.1572552
77  -177.1464 -22.7307591
78  -177.4310 -23.3037635
79  -177.7180 -23.8762498
80  -178.0076 -24.4481991
81  -178.2999 -25.0195918
82  -178.5948 -25.5904080
83  -178.8927 -26.1606268
84  -179.1934 -26.7302271
85  -179.4971 -27.2991869
86  -179.8040 -27.8674837
87   179.8859 -28.4350940
88   179.5724 -29.0019940
89   179.2555 -29.5681588
90   178.9350 -30.1335627
91   178.6108 -30.6981794
92   178.2828 -31.2619815
93   177.9509 -31.8249406
94   177.6149 -32.3870276
95   177.2747 -32.9482123
96   176.9302 -33.5084632
97   176.5811 -34.0677479
98   176.2275 -34.6260330
99   175.8690 -35.1832834
100  175.5056 -35.7394632
101  175.1371 -36.2945349
102  174.7633 -36.8484597

addPolylines(data = mydf2, lng = ~lon, lat = ~lat)

This is what I got from my attempt.

my attempt

rar
  • 894
  • 1
  • 9
  • 24
William
  • 340
  • 7
  • 17

1 Answers1

2

The example you provided uses Sources and Targets.

You need some sort of 'Source' as lat1 and then 'Target' as lat2. Then the same for longitude 'Source' as lon1 and 'Target' as lon2.

c(lat1, lon1) 
c(lat2, lon2) 

There is an example here: http://kateto.net/network-visualization

The flight path map is the last graph on the website, so scroll to the bottom immediately and then work your way up.

Re-frame your data set into Sources and Targets and try and replot.

Here is a similar question with an answer, which explains how to pass gcintermediate into Leaflet:

Adding Curved Flight path using R's Leaflet Package

Community
  • 1
  • 1
jpf5046
  • 729
  • 7
  • 32