Closed alexyshr closed 1 year ago
Where on the internet and when? Did you miss spdep::nb2lines(nb, wts, coords, proj4string=NULL, as_sf=FALSE)
? Yes, as_sf=
could be TRUE. See https://github.com/r-spatial/spdep/blob/82514fb81da0c6c5285bf4219d7979dac34acec4/R/nb2lines.R#L4
I modified the function nb_to_df
from here. Jun 15, 2015.
I got the function st_segment
from here. Feb 14, 2019.
Below is the right solution with spdep::nb2lines
.
Thank you for your answer.
library(spdep)
library(ggplot2)
library(sf)
ColData1 <- sf::st_read(system.file("shapes/columbus.shp", package="spData")[1], quiet=TRUE)
coords1 = sf::st_coordinates(sf::st_centroid(ColData1)) # coordinates of the census tract centroids
#> Warning in st_centroid.sf(ColData1): st_centroid assumes attributes are
#> constant over geometries of x
IDs1 = row.names(ColData1)
ColData1$IDs1 = IDs1
col_nb1 = poly2nb(ColData1, queen = TRUE)
summary(col_nb1)
#> Neighbour list object:
#> Number of regions: 49
#> Number of nonzero links: 236
#> Percentage nonzero weights: 9.829238
#> Average number of links: 4.816327
#> Link number distribution:
#>
#> 2 3 4 5 6 7 8 9 10
#> 5 9 12 5 9 3 4 1 1
#> 5 least connected regions:
#> 1 6 42 46 47 with 2 links
#> 1 most connected region:
#> 20 with 10 links
class(col_nb1) # check the class of col_nb
#> [1] "nb"
# convert nb to sf with binary weights
col_nb1_sf = spdep::nb2lines(col_nb1, coords=coords1, proj4string=NULL, as_sf=T) # weights are binary
# plot
ggplot() +
geom_sf(data=ColData1, fill=NA, color="red", lwd=0.5) +
geom_sf(data=sf::st_centroid(ColData1), pch=19, color="blue") +
geom_sf(data=col_nb1_sf, color="blue", lwd=0.3, lty=2) +
geom_sf_label(data=ColData1, aes(label=IDs1), size = 2) +
#geom_sf_text(data=ColData1, aes(label=IDs1), size = 3) +
labs(title="Adjacency Connection", subtitle = "Columbus Shapefile") +
xlab("") +
ylab("")
#> Warning in st_centroid.sf(ColData1): st_centroid assumes attributes are
#> constant over geometries of x
Created on 2023-03-15 with reprex v2.0.2
Thanks, @alexyshr !
Dear Dr. Edzer,
Maybe it is a good idea to enable
sf::st_as_sf
to convert a neighbors list object with classnb
(output ofspdep::poly2nb
) to simple features. Here I am providing two functions (nb_to_df
andst_segment
) to solve the conversion (I got those from the Internet).Sorry, but I need to take apart some time to learn it and do it by myself.
Best,
Created on 2023-03-14 with reprex v2.0.2