Skip to content

Plotting part of the nb object

2 messages · Xiang Ye, Grzegorz Sapijaszko

#
Dear community,

I am using the plot() method (actually plot.nb() method) to visually discover the neighborhood network generated by poly2nb() and its sibling functions. When I want to plot only part of the neighborhood network to make a visual comparison, I intuitively tried something like below, but failed:

library(sf)
library(spdep)
library(spDataLarge)

poly2nb(bristol_zones) -> bqueen
plot(bqueen[5], st_geometry(bristol_zones)) # This line will fail

I am thinking of extracting the neighboring regions into a new sf object, then drawing the neighborhood network of the new sf object, but it won't give me what I want: instead of a stellated-shaped ego-network, it still contains the redundant peripheral edges:

bristol_zones[c(5, bqueen[[5]]), ] -> temp
plot(poly2nb(temp), st_geometry(temp)) # This line generates unwanted peripheral edges

Is there any built-in or roundabout method to plot part of the nb object?

Merry Christmas and Happy New Year!
1 day later
#
On 12/25/23 08:19, Xiang Ye via R-sig-Geo wrote:
I do believe the plot is correct. That's because "South Gloucestershire 
005" is a neighbor to "South Gloucestershire 018" and the edges are 
drawn for all neighbors in the (sub)set. Your workaround to create a 
temporary sf object first might be the best solution, as plot.nb() 
requires nb class object.

t <- poly2nb(bristol_zones)
t5 <- bristol_zones[c(5, t[[5]]), ]

t <- poly2nb(t5)

plot(1,
 ? xlim = c(sf::st_bbox(t5)[1], sf::st_bbox(t5)[3]),
 ? ylim = c(sf::st_bbox(t5)[2], sf::st_bbox(t5)[4]),
 ? xlab = "", ylab = ""
)
plot(t5[, c("name", "geometry")], add = TRUE)
plot.nb(t, st_geometry(t5), add = TRUE)

Regards,
Grzegorz