Petr
Petr

Reputation: 1817

Spatial network based on maximum distance in ggplot2

I would like to plot network matrix of regions in ggplot - I know that for ggplot we need data.frame in tidy format in order to plot it.

I am able to plot network based on number of neighbours in ggplot however when I need spatial network based on maximum distance I get an error when creating data frame for ggplot.

I provided example down bellow:

library(ggplot2)
library(sf)
library(spdep)

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

CORD <- rbind(
  coordinates(data)
)
rownames(CORD) <- NULL

# Spatial Network based on number of neighbours
cns <- knearneigh(CORD, k = 5, longlat=T)
scnsn <- knn2nb(cns, row.names = NULL, sym = T)
cS <- nb2listw(scnsn)

data_df <- data.frame(CORD)
colnames(data_df) <- c("long", "lat")

# Creating dataframe  from spatail network (neiresth neighbours) for ggplot plot
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 of spatial network
ggplot(data, aes(x = long, y =lat))+
  geom_polygon(aes(group = group), color = "red", fill = FALSE) +
  geom_segment(data = DA, aes(xend = long_to, yend = lat_to), size=0.5, color = "royalblue") + 
  coord_map()


### Another type of network matrix - Maximum distance

nb200km <- dnearneigh(CORD, d1=0, d2=100, longlat=T)
summary(nb200km)
cS_distance <- nb2listw(nb200km, zero.policy = T)

# I need to recreate this plot in ggplot
plot(data)
plot(W, coordinates(data), add = T)



data_df <- data.frame(CORD)
colnames(data_df) <- c("long", "lat")

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

creating dataframe from cS object works, however creating a dataframe from cS_distance object returns an error.

I would like to ask how to solve the error and plot distance spatial network in ggplot.

Upvotes: 1

Views: 100

Answers (1)

Allan Cameron
Allan Cameron

Reputation: 173858

I'm not sure if this is what you're looking for, but the problem seems to be that you have some regions with no neighbours in cS_distance, so DA$to contains some zero values. This means when you do data_df[DA$from,] it has more rows than data_df[DA$to,], and your code throws an error when you try to cbind them.

If you filter out the rows where DA$to is zero, you get this:

n = length(attributes(cS_distance$neighbours)$region.id)
from <- rep(1:n,sapply(cS_distance$neighbours,length))
to <- unlist(cS_distance$neighbours)[]
weight <- numeric(length(to))
weight[which(to != 0)] <- unlist(cS_distance$weights)
DA = data.frame(from = from, to = to, weight = weight)
DA <- DA[DA$to != 0,]
DA = cbind(DA, data_df[DA$from,], data_df[DA$to,])

colnames(DA)[4:7] = c("long","lat","long_to","lat_to")

# ggplot of spatial network
ggplot(data, aes(x = long, y =lat))+
  geom_polygon(aes(group = group), color = "red", fill = NA) +
  geom_segment(data = DA, aes(xend = long_to, yend = lat_to), size=0.5, color = "royalblue") + 
  coord_map()

enter image description here

Upvotes: 2

Related Questions