繁体   English   中英

在 R 中,如何使用印度 state 中各区的地理空间数据从质心填充相邻区的渐变颜色?

[英]In R, how to fill gradient color for neighboring districts from a centroid, using geospatial data of districts in a state of India?

根据我的具体要求,我只想给邻近的地区上色。 首先,我下载了数据:

library(sp)
library(mapproj)
library(rgeos)
library(raster)
library(GADMTools)
library(precrec)
library(ggplot2)
library(tidyverse)
library(sf)
library(dplyr)
library(plotly)
library(plotrix)

India <- getData("GADM", country = "India", level = 2)
India_Level1 <- raster::getData("GADM", country = "India", level = 1)

# dist level map
dist_level_map_Gujarat <- India %>% st_as_sf() %>% filter(NAME_1 == "Gujarat")

然后我可以 plot 古吉拉特邦 state 内的所有地区:

p_Gujarat <- ggplot(data = dist_level_map_Gujarat) + 
  geom_sf(mapping = aes(fill = NAME_2)) + 
  theme_void() + theme(legend.position = "none")

ggplotly(p_Gujarat)

哪个好。

但是如果我只想使用“Centroid”作为区域及其所有邻居来着色(最好是渐变颜色)。 我正在尝试这样的事情:

# using sf, get the centriods
dist_level_map_Gujarat_Centroids_sf <- st_centroid(dist_level_map_Gujarat)

dist_level_map_Gujarat$Centroid <- dist_level_map_Gujarat_Centroids_sf$geometry

# Extract X and Y values of Centroids
dist_level_map_Gujarat$Centroid_X <- sapply(dist_level_map_Gujarat$Centroid,"[[",1)
dist_level_map_Gujarat$Centroid_Y <- sapply(dist_level_map_Gujarat$Centroid,"[[",2)

我想使用类似于下面的东西(质心周围的圆圈为渐变着色,例如 100 公里的圆圈,为邻近地区着色)

plot(1:5,seq(1,10,length=5),type="n",xlab="",ylab="",main="Test draw.circle", axes=FALSE,ann=FALSE)
draw.circle(3,6,c(1,0.66,0.33),border="purple", col=c("#ff00ff","#ff77ff","#ffccff"),lty=1,lwd=1)

但不确定我该怎么做。 有人可以帮忙吗?

你的问题对我来说不是很清楚:

  • 使用“Centroid”作为区域及其所有邻居是什么意思。 质心是多边形的“中心”。 如果您需要多边形的邻居,有几种方法,如获取接触参考多边形的多边形。

  • 我想使用类似于下面的东西(质心周围的圆圈为渐变着色,例如 100 公里的圆圈,为邻近地区着色) :同样,这只是为了绘图,或者你需要得到一定半径的邻居?

尽管如此,我还是试了一下。 所以技术是:

  1. 您需要将 shapefile“投影”到以米为单位的投影。 数据以经度/纬度坐标下载,并且无法很好地捕获此系统上的距离。
  2. 我在质心周围创建了顺序缓冲区。 这有效地创建了一定半径的圆。 在那之后,梯度部分几乎是微不足道的。

看看这是否能满足您的需求,问候:

library(sf)
library(tidyverse)

India <- raster::getData("GADM", country = "India", level = 2)

# dist level map
dist_level_map_Gujarat <- India %>% st_as_sf() %>% filter(NAME_1 == "Gujarat")

st_crs(dist_level_map_Gujarat)$units
#> NULL

# Project, using: https://epsg.io/7761
dist_level_map_Gujarat <- dist_level_map_Gujarat %>%
  st_transform(7761)

# Now units are meters
st_crs(dist_level_map_Gujarat)$units
#> [1] "m"


# Plot the map
ggplot(data = dist_level_map_Gujarat) + 
  geom_sf(mapping = aes(fill = NAME_2)) + 
  theme_void() + theme(legend.position = "none")

在此处输入图像描述


# Get the centroids of a couple of districts and buffer
centroid <- dist_level_map_Gujarat %>%
  filter(NAME_2 %in% c("Botad", "Dahod")) %>%
  st_centroid()
 

# Create buffers of 33, 66 and 100 km
buffers <- lapply(c(100,66,33), function(x){
  
  # Create the buffer!
  df <- st_buffer(centroid, x*1000)
  df$label <- x
  df
})  %>% bind_rows() 

ggplot() +
geom_sf(data=dist_level_map_Gujarat) +
geom_sf(data=buffers, aes(fill=label), color="purple") +
scale_fill_gradientn(colors=
                       alpha(rev(c("#ff00ff","#ff77ff","#ffccff")),0.7 ),
                       label = scales::label_number(suffix = " km."),
                       guide = guide_colorsteps(title="My buffer")
                       )

在此处输入图像描述

reprex package (v2.0.1) 创建于 2022-06-14

暂无
暂无

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

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