0

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?

LeMarque
  • 733
  • 5
  • 21
  • Please note that the above code does not require any kind of revision / editing... the files are used in the codes in some ways or the other. Thanks. – LeMarque Jun 14 '22 at 07:41

1 Answers1

1

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)

dieghernan
  • 2,690
  • 8
  • 16
  • Sorry for the confusions... but you are almost 95% close to the things I wanted. But I surely got the idea and with some tweaks, I should be able to do the rest. Thanks. – LeMarque Jun 14 '22 at 10:55
  • Just did this, now only districts falling within circles are to be colored and that's it ..... `ggplot() + geom_sf(data=dist_level_map_Gujarat) + geom_sf(data = dist_level_map_Gujarat$Centroid, color = 'black') + geom_sf(data=buffers, aes(fill=label), color="purple") + scale_fill_gradientn(colors = alpha(rev(c("#ff00ff","#ff77ff","#ffccff")),0.3 ), label = scales::label_number(suffix = " km."), guide = guide_colorsteps(title="My buffer")) + theme_void() + theme(legend.position = "none") ` – LeMarque Jun 14 '22 at 11:21