Proximity Maps Using R

I am looking to create some proximity maps using R, which show how far the areas are from certain points. I cannot find examples in the R code, but I found the output I want: enter image description here

It is not necessary to have all the frauds of the marking / internal borders, but I would like him to stop at the sea border (thinking about using the function rgeos gintersection- see here ).

"Heatmaps" ( /) ( , , gintersection, , , . < img src= "https://i.stack.imgur.com/AmEPp.png" alt= " " >

+4
2

, ...

library(raster)
library(sf)
library(ggplot2)
library(dplyr)
library(tidyr)
library(forcats)
library(purrr)

# Get UK map
GBR <- getData(name = "GADM", country = "GBR", level = 1)
GBR_sf <- st_as_sf(GBR)

# Define 3 points on the UK map
pts <- matrix(c(-0.4966766, -2.0772529, -3.8437793, 
                51.91829, 52.86147, 56.73899), ncol = 2)
# Project in mercator to allow buffer with distances
pts_sf <- st_sfc(st_multipoint(pts), crs = 4326) %>% 
  st_sf() %>%
  st_transform(27700)

ggplot() +
  geom_sf(data = GBR_sf) +
  geom_sf(data = pts_sf, colour = "red")

Data card

multipolygons . ( ), .

# Define distances to buffer
dists <- seq(5000, 150000, length.out = 5)
# Create buffer areas with each distances
pts_buf <- purrr::map(dists, ~st_buffer(pts_sf, .)) %>%
  do.call("rbind", .) %>% 
  st_cast() %>%
  mutate(
    distmax = dists,
    dist = glue::glue("<{dists/1000} km"))
# Plot: alpha allows to see overlapping polygons
ggplot() +
  geom_sf(data = GBR_sf) +
  geom_sf(data = pts_buf, fill = "red",
          colour = NA, alpha = 0.1)

Buffer overlay

. . . , . .

# Remove part of polygons overlapping smaller buffer
pts_holes <- purrr::map2(tail(1:nrow(pts_buf),-1),
            head(1:nrow(pts_buf),-1),
            ~st_difference(pts_buf[.x,], pts_buf[.y,])) %>%
  do.call("rbind", .) %>% 
  st_cast() %>%
  select(-distmax.1, -dist.1)
# Add smallest polygon
pts_holes_tot <- pts_holes %>% 
  rbind(filter(pts_buf, distmax == min(dists))) %>%
  arrange(distmax) %>%
  mutate(dist = forcats::fct_reorder(dist, distmax))
# Plot and define color according to dist
ggplot() +
  geom_sf(data = GBR_sf) +
  geom_sf(data = pts_holes_tot,
          aes(fill = dist),
          colour = NA) +
  scale_fill_brewer(direction = 2)

Hole Buffer - Pontoon Polygons

, , . multipolygons . .

# Remove part of polygons in the sea
# Union and projection of UK map
GBR_sf_merc <- st_transform(st_union(GBR_sf), 27700)
pts_holes_uk <- st_intersection(pts_holes_tot, 
                              GBR_sf_merc)

ggplot() +
  geom_sf(data = GBR_sf) +
  geom_sf(data = pts_holes_uk,
          aes(fill = dist),
          colour = NA) +
  scale_fill_brewer(direction = 2)

, sf, ggplot2 ...

Proximity map

+4

, :

library(raster)
GBR <- getData(name = "GADM", country = "GBR", level = 1)
pts <- matrix(c(-0.4966766, -2.0772529, -3.8437793, 51.91829, 52.86147, 56.73899), ncol = 2)

r <- raster(GBR, res=1/12)
d <- distanceFromPoints(r, pts)
m <- mask(d, GBR)

plot(m)
+2

Source: https://habr.com/ru/post/1688620/


All Articles