Home > Net >  In R, how to fill gradient color for neighboring districts from a centroid, using geospatial data of
In R, how to fill gradient color for neighboring districts from a centroid, using geospatial data of

Time:06-14

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 :

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

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?

CodePudding user response:

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 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?

Still, I gave it a try. So the technique is:

  1. You need to "project" the shapefile to a projection in meters. The data is downloaded in longitude/latitude coords, and distances on this system are not captured well.
  2. I created sequentially buffers around the centroids. This effectively create circles of a certain radius. After that, the gradient part is almost trivial.

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

enter image description here


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

enter image description here

Created on 2022-06-14 by the reprex package (v2.0.1)

  • Related