user2997345
user2997345

Reputation: 131

How to draw lines in leaflet that point to same location?

Here is my code

library(leaflet)
library(dplyr)
df <- data.frame(lat = c(44.7,44.7,44.7,44.2,44.5,45.0),lon = c(-63.4,-63.4,-63.4,-64,-63.1,-62.8),id = c(0,0,0,1,1,1))
leaflet() %>%
  addTiles() %>%
  addPolylines(data = df,lng = ~lon, lat = ~lat, group = ~id)

What I want to happen is to have 3 lines all pointing at the location (44.7,-63.4), but I cannot figure out how to make it happen.

Upvotes: 1

Views: 119

Answers (1)

HubertL
HubertL

Reputation: 19544

If you create the id using point pairs, you can then use dplyr::arrange:

library(leaflet)
library(dplyr)
df <- data.frame(lat = c(44.7,44.7,44.7,44.2,44.5,45.0), 
                 lon = c(-63.4,-63.4,-63.4,-64,-63.1,-62.8),
                 id = rep(1:3))

df %>% arrange(id) %>% leaflet %>%
  addPolylines(lng = ~lon, lat = ~lat)

Upvotes: 1

Related Questions