相当于 geom_segments xend, yend 参数的传单

Leaflet equivalent of geom_segments xend, yend arguments

我想问一下有没有什么方法可以在leaflet`saddPolylines函数中通过geom_segment参数设置xendyend

为了解释我宁愿提供可重现的例子,因为它很容易看到而不是解释:

library(leaflet)
library(spdep)
library(ggplot2)

URL <- "https://biogeo.ucdavis.edu/data/gadm3.6/Rsp/gadm36_DEU_1_sp.rds"
data <- readRDS(url(URL))

cns <- knearneigh(coordinates(data), k = 1, longlat = T)
scnsn <- knn2nb(cns, row.names = NULL, sym = T)
cns
scnsn
cS <- nb2listw(scnsn)

# Plotting results
plot(data)
plot(cS, coordinates(data), add = T)

# Plotting in ggplot 
# Converting to data.frame
data_df <- data.frame(coordinates(data))
colnames(data_df) <- c("long", "lat")

n = length(attributes(cS$neighbours)$region.id)
DA = data.frame(
  from = rep(1:n,sapply(cS$neighbours,length)),
  to = unlist(cS$neighbours),
  weight = unlist(cS$weights)
)
DA = cbind(DA, data_df[DA$from,], data_df[DA$to,])
colnames(DA)[4:7] = c("long","lat","long_to","lat_to")


ggplot(data, aes(x = long, y =lat))+
  geom_polygon(aes(group = group), color = "black", fill = FALSE)+
  geom_point(data = data_df, aes(x= long, y = lat), size = 1)+
  geom_segment(data = DA, aes(xend = long_to, yend = lat_to), size=0.5, color = "red")

# Plotting in leaflet
leaflet() %>% addProviderTiles("CartoDB.Positron") %>% 
  addPolygons(data=data, weight = 0.8, fill = F, color = "red") %>% 
  addPolylines(data=DA, lng = DA$long_to, lat = DA$lat_to, weight = 0.85)

可以看出 leaflet 的结果不对(空间地图不同)基本图和 ggplot 中的图应该是什么样子的,

有没有办法在传单中重现上面的情节?阅读传单文档对我没有帮助

一种可能的解决方法是使用 library(leaflet.minicharts) 中实现的函数 addFlows()

leaflet() %>% addProviderTiles("CartoDB.Positron") %>% 
  addPolygons(data=data, weight = 0.8, fill = F, color = "red") %>% 
  addFlows(lng0 = DA$long, lat0 = DA$lat,lng1 = DA$long_to, lat1 = DA$lat_to, dir = 1, maxThickness= 0.85)