我的问题与上一个询问的问题类似(基本上相同):Draw All Lines Between Points
但是我想有一个在R中使用小叶的解决方案。可以使用小叶吗? addPolylines()
函数用作连接数据框中的所有连续点。如下面的示例数据集所示,按顺序连接所有点很容易,但是如何在这五个点之间绘制所有可能的线段呢?
我希望看到带有传单的常规解决方案,可以将其应用于具有更多分数的方案。非常感谢!
locations <- data.frame(Long = c(76,75,73,72,74,76), Lat = c(43,40,40,43,45,43))
leaflet("locations") %>%
addTiles() %>%
addPolylines(lng = locations$Long,
lat = locations$Lat)
答案 0 :(得分:1)
它可以分为2个步骤:
library(leaflet)
library(tidyr)
library(dplyr)
library(purrr)
locations <- data.frame(Long = c(76,75,73,72,74,76), Lat = c(43,40,40,43,45,43))
# get unique coordinates
locations_distinct <- locations %>%
distinct_all()
# get all combinations between any two points
combs <- combn(1:nrow(locations_distinct),2,simplify = FALSE)
# get pairs of coordinates
locations_paris <- map_df(combs,function(x){
df <- bind_cols(
locations_distinct[x[1],],locations_distinct[x[2],]
)
colnames(df) <- c("Long","Lat","Long1","Lat1")
return(df)
})
How Do I connect two coordinates with a line using Leaflet in R
map <- leaflet(locations_paris) %>%
addTiles()
for(i in 1 : nrow(locations_paris)){
map <- map %>%
addPolylines(
lng = c(locations_paris$Long[i],locations_paris$Long1[i]),
lat = c(locations_paris$Lat[i],locations_paris$Lat1[i])
)
}
map