等效于geom_segments xend的小叶,¥ d参数

问题描述 投票:0回答:1

我想问一种方法,如何从xend yend函数中的geom_segment参数设置leaflet`saddPolylines

要解释,我宁愿提供可复制的示例,因为它很容易看到而不是解释:

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中的图看起来应该是什么样,

有没有一种方法可以复制以上传单中的地块?阅读传单文档对我没有帮助

r ggplot2 leaflet maps r-leaflet
1个回答
0
投票

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

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)
© www.soinside.com 2019 - 2024. All rights reserved.