Adding colour to polylines in Leaflet

classic Classic list List threaded Threaded
5 messages Options
Reply | Threaded
Open this post in threaded view
|

Adding colour to polylines in Leaflet

dhirajkhanna
I am working with shipping data where I get the dynamic parameters of a
ship like its position, speed, heading and rate of turn. I am then trying
to plot this on a leaflet map and trying to colour the polylines based on
the speed, but it always shows up in the same colour. Here’s some sample
data:


structure(list(lat = c(51.88783, 51.8878441, 51.887825, 51.88659,
51.8866959, 51.8874931, 51.89359, 51.8941269, 51.8977051, 51.8994331,
51.90773, 51.91324, 51.91604, 51.9216652, 51.93353, 51.9419365 ), lon
= c(4.28763342, 4.287635, 4.28765154, 4.29007339, 4.29562664,
4.29917, 4.30641174, 4.30561829, 4.29263353, 4.284498, 4.261132,
4.24711847, 4.241075, 4.23262, 4.21523666, 4.1927), rateOfTurn = c(0L,
 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L),
sogKts = c(0, 0, 0, 2.1, 3.4, 4.6, 3.5, 3.8, 7.4, 7.9, 8.8,      9.1,
9.2, 9.2, 9.3, 9.3), cog = c(15, 15, 15, 122.2, 70.4,      70, 323.2,
315.3, 289.3, 290.9, 303.8, 303.7, 308.9, 324.5,      304.9, 301.4),
heading = c(163, 162, 163, 106, 71, 71, 303,      298, 289, 294, 303,
303, 310, 324, 304, 302), timestamp = c("2018-07-19T05:27:34",
"2018-07-19T05:39:35", "2018-07-19T05:45:34", "2018-07-19T05:57:37",
   "2018-07-19T06:02:48", "2018-07-19T06:04:49",
"2018-07-19T06:12:51",      "2018-07-19T06:13:32",
"2018-07-19T06:19:08", "2018-07-19T06:21:41",
"2018-07-19T06:28:42", "2018-07-19T06:32:50", "2018-07-19T06:34:37",
   "2018-07-19T06:37:41", "2018-07-19T06:43:49", "2018-07-19T06:50:09"
    ), Color = c("red", "red", "red", "red", "orange", "orange",
"orange", "orange", "orange", "orange", "yellow", "yellow",
"yellow", "yellow", "yellow", "yellow")), row.names = 32:47, class =
"data.frame")

This is the code I have tried which doesn’t work:-

map <-  leaflet(x) map <- addTiles(map) for( Color in
levels(as.factor(x$Color))){   map <- addPolylines(map,
lng=~lon,lat=~lat,data=x[x$Color==Color,], color=~Color) } map

Regards
Dhiraj Khanna
Mob:09873263331

        [[alternative HTML version deleted]]

_______________________________________________
R-sig-Geo mailing list
[hidden email]
https://stat.ethz.ch/mailman/listinfo/r-sig-geo
Reply | Threaded
Open this post in threaded view
|

Re: Adding colour to polylines in Leaflet

Kent Johnson
>
> Message: 5
> Date: Sat, 1 Sep 2018 08:28:24 +0530
> From: Dhiraj Khanna <[hidden email]>
> To: [hidden email]
> Subject: [R-sig-Geo] Adding colour to polylines in Leaflet
> Message-ID:
>         <CANHhK329-Y7hPJD9gSOs24mSqGkrjC481oQspNA
> [hidden email]>
> Content-Type: text/plain; charset="utf-8"
>
> I am working with shipping data where I get the dynamic parameters of a
> ship like its position, speed, heading and rate of turn. I am then trying
> to plot this on a leaflet map and trying to colour the polylines based on
> the speed, but it always shows up in the same colour. Here’s some sample
> data:
>
> <snip>
> This is the code I have tried which doesn’t work:-
>
> map <-  leaflet(x) map <- addTiles(map) for( Color in
> levels(as.factor(x$Color))){   map <- addPolylines(map,
> lng=~lon,lat=~lat,data=x[x$Color==Color,], color=~Color) } map
>
> Regards
> Dhiraj Khanna
> Mob:09873263331


What are you expecting to see? When I run your code I get a map with three
lines, one red, one orange and one yellow.

Kent

        [[alternative HTML version deleted]]

_______________________________________________
R-sig-Geo mailing list
[hidden email]
https://stat.ethz.ch/mailman/listinfo/r-sig-geo
Reply | Threaded
Open this post in threaded view
|

Re: Adding colour to polylines in Leaflet

dhirajkhanna
@Kent they are appearing as three separate lines. I am hoping to see them
joint with no gaps. The transition from row 4 to row 5 is where the speed
has changed from 2.1 knots to 3.4 knots. I am hoping to see another line
from row 4 to row 5 in red colour. Similarly for the other disjoint points.
Regards

Dhiraj Khanna
Mob:09873263331


On Sat, Sep 1, 2018 at 6:17 PM Kent Johnson <[hidden email]> wrote:

> Message: 5
>> Date: Sat, 1 Sep 2018 08:28:24 +0530
>> From: Dhiraj Khanna <[hidden email]>
>> To: [hidden email]
>> Subject: [R-sig-Geo] Adding colour to polylines in Leaflet
>> Message-ID:
>>         <
>> [hidden email]>
>> Content-Type: text/plain; charset="utf-8"
>>
>> I am working with shipping data where I get the dynamic parameters of a
>> ship like its position, speed, heading and rate of turn. I am then trying
>> to plot this on a leaflet map and trying to colour the polylines based on
>> the speed, but it always shows up in the same colour. Here’s some sample
>> data:
>>
>> <snip>
>> This is the code I have tried which doesn’t work:-
>>
>> map <-  leaflet(x) map <- addTiles(map) for( Color in
>> levels(as.factor(x$Color))){   map <- addPolylines(map,
>> lng=~lon,lat=~lat,data=x[x$Color==Color,], color=~Color) } map
>>
>> Regards
>> Dhiraj Khanna
>> Mob:09873263331
>
>
> What are you expecting to see? When I run your code I get a map with three
> lines, one red, one orange and one yellow.
>
> Kent
>
>

        [[alternative HTML version deleted]]

_______________________________________________
R-sig-Geo mailing list
[hidden email]
https://stat.ethz.ch/mailman/listinfo/r-sig-geo
Reply | Threaded
Open this post in threaded view
|

Re: Adding colour to polylines in Leaflet

Kent Johnson
You have to include the points where the colors change in both polylines.
Here is one way:

x$lastColor = dplyr::lag(x$Color)
map <-  leaflet(x)
map <- addTiles(map)
for( Color in
levels(as.factor(x$Color))){
  map <- addPolylines(map,lng=~lon,lat=~lat,data=x[x$Color==Color |
x$lastColor==Color,], color=~Color) }
map

Kent

On Sat, Sep 1, 2018 at 8:56 AM, Dhiraj Khanna <[hidden email]>
wrote:

> @Kent they are appearing as three separate lines. I am hoping to see them
> joint with no gaps. The transition from row 4 to row 5 is where the speed
> has changed from 2.1 knots to 3.4 knots. I am hoping to see another line
> from row 4 to row 5 in red colour. Similarly for the other disjoint points.
> Regards
>
> Dhiraj Khanna
> Mob:09873263331
>
>
> On Sat, Sep 1, 2018 at 6:17 PM Kent Johnson <[hidden email]> wrote:
>
>> Message: 5
>>> Date: Sat, 1 Sep 2018 08:28:24 +0530
>>> From: Dhiraj Khanna <[hidden email]>
>>> To: [hidden email]
>>> Subject: [R-sig-Geo] Adding colour to polylines in Leaflet
>>> Message-ID:
>>>         <CANHhK329-Y7hPJD9gSOs24mSqGkrjC481oQspNA
>>> [hidden email]>
>>> Content-Type: text/plain; charset="utf-8"
>>>
>>> I am working with shipping data where I get the dynamic parameters of a
>>> ship like its position, speed, heading and rate of turn. I am then trying
>>> to plot this on a leaflet map and trying to colour the polylines based on
>>> the speed, but it always shows up in the same colour. Here’s some sample
>>> data:
>>>
>>> <snip>
>>> This is the code I have tried which doesn’t work:-
>>>
>>> map <-  leaflet(x) map <- addTiles(map) for( Color in
>>> levels(as.factor(x$Color))){   map <- addPolylines(map,
>>> lng=~lon,lat=~lat,data=x[x$Color==Color,], color=~Color) } map
>>>
>>> Regards
>>> Dhiraj Khanna
>>> Mob:09873263331
>>
>>
>> What are you expecting to see? When I run your code I get a map with
>> three lines, one red, one orange and one yellow.
>>
>> Kent
>>
>>

        [[alternative HTML version deleted]]

_______________________________________________
R-sig-Geo mailing list
[hidden email]
https://stat.ethz.ch/mailman/listinfo/r-sig-geo
Reply | Threaded
Open this post in threaded view
|

Re: Adding colour to polylines in Leaflet

dhirajkhanna
Thank you Kent, that worked like a charm!
Regards

Dhiraj Khanna
Mob:09873263331


On Sat, Sep 1, 2018 at 7:59 PM Kent Johnson <[hidden email]> wrote:

> You have to include the points where the colors change in both polylines.
> Here is one way:
>
> x$lastColor = dplyr::lag(x$Color)
> map <-  leaflet(x)
> map <- addTiles(map)
> for( Color in
> levels(as.factor(x$Color))){
>   map <- addPolylines(map,lng=~lon,lat=~lat,data=x[x$Color==Color |
> x$lastColor==Color,], color=~Color) }
> map
>
> Kent
>
> On Sat, Sep 1, 2018 at 8:56 AM, Dhiraj Khanna <[hidden email]>
> wrote:
>
>> @Kent they are appearing as three separate lines. I am hoping to see them
>> joint with no gaps. The transition from row 4 to row 5 is where the speed
>> has changed from 2.1 knots to 3.4 knots. I am hoping to see another line
>> from row 4 to row 5 in red colour. Similarly for the other disjoint points.
>> Regards
>>
>> Dhiraj Khanna
>> Mob:09873263331
>>
>>
>> On Sat, Sep 1, 2018 at 6:17 PM Kent Johnson <[hidden email]> wrote:
>>
>>> Message: 5
>>>> Date: Sat, 1 Sep 2018 08:28:24 +0530
>>>> From: Dhiraj Khanna <[hidden email]>
>>>> To: [hidden email]
>>>> Subject: [R-sig-Geo] Adding colour to polylines in Leaflet
>>>> Message-ID:
>>>>         <
>>>> [hidden email]>
>>>> Content-Type: text/plain; charset="utf-8"
>>>>
>>>> I am working with shipping data where I get the dynamic parameters of a
>>>> ship like its position, speed, heading and rate of turn. I am then
>>>> trying
>>>> to plot this on a leaflet map and trying to colour the polylines based
>>>> on
>>>> the speed, but it always shows up in the same colour. Here’s some sample
>>>> data:
>>>>
>>>> <snip>
>>>> This is the code I have tried which doesn’t work:-
>>>>
>>>> map <-  leaflet(x) map <- addTiles(map) for( Color in
>>>> levels(as.factor(x$Color))){   map <- addPolylines(map,
>>>> lng=~lon,lat=~lat,data=x[x$Color==Color,], color=~Color) } map
>>>>
>>>> Regards
>>>> Dhiraj Khanna
>>>> Mob:09873263331
>>>
>>>
>>> What are you expecting to see? When I run your code I get a map with
>>> three lines, one red, one orange and one yellow.
>>>
>>> Kent
>>>
>>>
>

        [[alternative HTML version deleted]]

_______________________________________________
R-sig-Geo mailing list
[hidden email]
https://stat.ethz.ch/mailman/listinfo/r-sig-geo