The following code can be used to visualise the distances between the first polygon in nc
data (since I don't have the data you mentioned) and all the other polygons in nc
.
# packages
suppressPackageStartupMessages({
library(sf)
library(tmap)
})
# data
nc = st_read(system.file("shape/nc.shp", package="sf"), quiet = TRUE)
# compute distances from first region
nc$dists <- st_distance(nc, nc[1, ])
# plot
tm_shape(nc) +
tm_polygons(col = "dists") +
tm_shape(nc[1, ]) +
tm_polygons(col = "grey85", lwd = 1.75)
Created on 2021-10-27 by the reprex package (v2.0.1)
It's pretty simple, but it may be good enough. The same code also works for larger regions (provided that you are considering just one polygon at a time):
# packages
library(sf)
#> Linking to GEOS 3.9.1, GDAL 3.2.1, PROJ 7.2.1
# data
bounding_box <- st_sfc(st_polygon(list(rbind(c(0, 0), c(1, 0), c(1, 1), c(0, 1), c(0, 0)))))
regions <- st_make_grid(bounding_box, n = c(100, 100))
length(regions)
#> [1] 10000
# compute distances from region 5250 (which is more or less in the middle)
dists <- st_distance(regions, regions[5250])
# plot
cols <- terrain.colors(15)[cut(dists, 15)]
par(mar = rep(0, 4))
plot(regions, col = cols)
Created on 2021-10-27 by the reprex package (v2.0.1)
I don't know how to visualise all the distances at the same time.