简体   繁体   English

使用R的邻近地图

[英]Proximity Maps using R

I'm looking to create some proximity maps using R, which show how far areas are from certain points. 我正在寻找使用R创建一些接近地图,它显示了某些点的区域。 I can't find any examples in R code, but I've found an output which is the sort of thing I want: 我在R代码中找不到任何示例,但我发现了一个输出,这是我想要的东西: 在此输入图像描述

It doesn't necessarily have to have all the labelling/internal boundaries wizardry, but I'd like it to stop at the sea border (thinking of using the rgeos function gintersection - see here ). 它不一定必须具有所有标签/内部边界的魔法,但我希望它停在海边(考虑使用rgeos函数gintersection - 见这里 )。

I've tried doing a density plot as 'heatmaps' (this would be a pretty good solution/alternative) and putting a shapefile over the top (following this suggestion , but they're not lining up and I can't do a gintersection , probably because there's not a coordinate system attached to the density plot. 我已经尝试将密度图作为'热图'(这将是一个非常好的解决方案/替代方案)并将shapefile放在顶部(遵循这个建议 ,但他们没有排队,我不能做一个gintersection ,可能是因为密度图没有附加坐标系。 在此输入图像描述

I used your question to play a little with new libraries... 我用你的问题与新图书馆玩了一点......

Get a UK map and define random points 获取英国地图并定义随机点

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")

数据图

Calculate buffer areas 计算缓冲区

We create a list of multipolygons for each buffer distance. 我们为每个缓冲距离创建一个multipolygons列表。 The point dataset must be in projected coordinates (here mercator) as buffer distance is in the scale of the coordinates system. 由于缓冲距离在坐标系的范围内,因此点数据集必须位于投影坐标(此处为mercator)中。

# 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)

缓冲重叠

Remove overlapping 删除重叠

Buffer areas are overlapping. 缓冲区重叠。 On the figure above, the more intense red color is due to multiple overlapping layers of transparent red. 在上图中,更强烈的红色是由于多个重叠的透明红色层。 Let's remove the overlapping. 让我们删除重叠。 We need to remove from larger areas, the buffer with the lower size. 我们需要从较大的区域移除较小的缓冲区。 I then need to add again the smallest area to the list. 然后我需要再次将最小的区域添加到列表中。

# 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)

缓冲带孔 - 圆环多边形

Remove areas in the sea 移除海域

If you want to find proximity area on terrestrial parts only, we need to remove buffer areas that are in the sea. 如果您只想在地面部件上找到接近区域,我们需要移除海中的缓冲区域。 Intersection is computed between multipolygons with the same projection. 交点之间计算multipolygons具有相同的投影。 I previously realize an union of the UK map. 我以前意识到英国地图的联盟。

# 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)

And here is the final proximity map using sf , ggplot2 and a few other libraries... 这是使用sfggplot2和其他一些库的最终邻近地图......

邻近地图

Based on Sébastien's example, a more old-fashioned approach: 根据塞巴斯蒂安的例子,一种更老式的方法:

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)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM