[英]In R, how to fill gradient color for neighboring districts from a centroid, using geospatial data of districts in a state of India?
In my specific requirement, I want to color just the neighbouring districts.根据我的具体要求,我只想给邻近的地区上色。 First, I have downloaded the data:
首先,我下载了数据:
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")
And then I can plot all the districts within the state of 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)
Which is good.哪个好。
But then if I only want to color (preferably gradient color) using 'Centroid' as the district, and all the neighbors to it.但是如果我只想使用“Centroid”作为区域及其所有邻居来着色(最好是渐变颜色)。 I am trying something like this:
我正在尝试这样的事情:
# 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)
And I want to use something similar as below (the circle around the centroid to color the gradients, for a circle of, say 100 Kms, to color the neighboring districts)我想使用类似于下面的东西(质心周围的圆圈为渐变着色,例如 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)
But not sure how can I do that.但不确定我该怎么做。 Can someone please help?
有人可以帮忙吗?
Your question is not very clear to me:你的问题对我来说不是很清楚:
What do you mean by using 'Centroid' as the district, and all the neighbors to it.使用“Centroid”作为区域及其所有邻居是什么意思。 ?
? Centroid is the "center" of a polygon.
质心是多边形的“中心”。 If you need the neighbours of a polygon there are several methods, as getting the polygons that touches the polygon of reference.
如果您需要多边形的邻居,有几种方法,如获取接触参考多边形的多边形。
And I want to use something similar as below (the circle around the centroid to color the gradients, for a circle of, say 100 Kms, to color the neighboring districts) : Again, this is just for plotting or do you need to get the neighbours on a certain radius?我想使用类似于下面的东西(质心周围的圆圈为渐变着色,例如 100 公里的圆圈,为邻近地区着色) :同样,这只是为了绘图,或者你需要得到一定半径的邻居?
Still, I gave it a try.尽管如此,我还是试了一下。 So the technique is:
所以技术是:
See if this can fit your needs, regards:看看这是否能满足您的需求,问候:
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")
)
Created on 2022-06-14 by the reprex package (v2.0.1)由reprex package (v2.0.1) 创建于 2022-06-14
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.