[R-sig-Geo] Adding colour to polylines in Leaflet
Kent Johnson
kent3737 @ending from gm@il@com
Sat Sep 1 16:29:43 CEST 2018
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 <dhirajkhanna using gmail.com>
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 <kent3737 using gmail.com> wrote:
>
>> Message: 5
>>> Date: Sat, 1 Sep 2018 08:28:24 +0530
>>> From: Dhiraj Khanna <dhirajkhanna using gmail.com>
>>> To: r-sig-geo using r-project.org
>>> Subject: [R-sig-Geo] Adding colour to polylines in Leaflet
>>> Message-ID:
>>> <CANHhK329-Y7hPJD9gSOs24mSqGkrjC481oQspNA
>>> gVisAw2JBoQ using mail.gmail.com>
>>> 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]]
More information about the R-sig-Geo
mailing list