I have a dataset with lat long coordinates. Each coordinate belongs to a group and I want to connect them with polylines using leaflet. I am trying to give different colors to different line segments. I am fairly new to leaflet, so I don't know if I'm approaching this problem the right way.
I have tried the solution provided here, but I only get a single color as a result.
I have simulated a dataset to illustrate my problem:
#Example dataframe
lat <- runif(10,-10.8544921875,2.021484375)
long <- runif(10,49.82380908513249,59.478568831926395)
group <- factor(c(1,1,1,1,2,2,2,1,1,1))
header <- c("lat", "long", "group")
df <- data.frame(lat, long, group)
colnames(df) <- header
I have tried the following:
#Color palette
pal <- colorFactor(palette = c('navy', 'red'),
levels = levels(df$group))
#Graph
leaflet(df) %>%
addTiles() %>%
addPolylines(lng = ~as.numeric(df$long),
lat = ~as.numeric(df$lat), stroke = pal(df$group),
opacity = 0.1)
I want that the polyline between points that belong to group 1 and 2 shows red, and the rest just blue (or any combination of 2 colors, for that matter). However, I get back only one color. Opacity doesn't seem to match either (I have defined an opacity of 0.1 but the value of resulting polyline has certainly a value of 1).
Can anyone give me some pointers as to what I'm doing wrong?
Also, (with the larger dataset) I notice that once I add colors to the polylines of my dataset, the process becomes computationally very intensive. Can anyone give me guidelines to optimize the process?
Any help is very much appreciated. Thank you in advance!!