Home > Software engineering >  How to prevent ggplot2 (GADM) from plotting districts outside the selected State
How to prevent ggplot2 (GADM) from plotting districts outside the selected State

Time:12-20

I was plotting a map of Maharashtra, India using GADM. I have used a subset of district names to map the areas. But I found that the districts from other states that has the same name are also being drawn. How do I solve this issue. Thanks

I have used the following codes to prepare the plot.

 library(ggplot2)
    library(broom)
    library(scales)
    library(sp)
    library(maptools)

ind2 <-  getData("GADM", country = "IND", level = 2)

ind2_df <- tidy(ind2, region = "NAME_2") 
  
mh <- subset (ind2_df, 
                  id == 'Ahmadnagar' |
                  id == 'Gadchiroli' |
                  id == 'Latur' |
                  id == 'Parbhani' |
                  id == 'Mumbai Suburban' |
                  id == 'Akola' |
                  id == 'Amravati' |
                  id == 'Aurangabad' |
                  id == 'Bid' |
                  id == 'Bhandara' |
                  id == 'Buldana' |
                  id == 'Chandrapur'|
                  id == 'Dhule'|
                  id == 'Gondia'| 
                  id == 'Hingoli'|
                  id == 'Jalgaon'|
                  id == 'Jalna'|
                  id == 'Kolhapur'|
                  id == 'Mumbai City'|
                  id == 'Nagpur'|
                  id == 'Nanded'|
                  id == 'Nandurbar'|
                  id == 'Nashik'|
                  id == 'Osmanabad'|
                  id == 'Palghar'|
                  id == 'Pune'|
                  id == 'Raigarh'|
                  id == 'Ratnagiri'|
                  id == 'Sangli'|
                  id == 'Satara'|
                  id == 'Sindhudurg'|
                  id == 'Solapur'|
                  id == 'Thane'|
                  id == 'Wardha'|
                  id == 'Washim'|
                  id == 'Yavatmal')

                

Centroid data , I have used the same criteria mentioned above for subletting.

centroid <-  as.data.frame(coordinates(ind2))
colnames(centroid) = c("long","lat")
centroid$id <-  ind2@data$NAME_2
centroid <- subset ( SAME AS the ABOVE)

  
plot2 <- ggplot() 
  geom_polygon(data = mh, aes( long, lat, group = group,  fill = id )) 
  geom_text(data = centroid, aes(x = long, y = lat, label = id),
            size = 4,
            check_overlap = T) 
  theme_void() 
  theme(aspect.ratio=1)
                                                                                                                                        

CodePudding user response:

Probably better of using sf simple features approach...

Revised answer following OP comment.

library(ggplot2)
library(dplyr)
library(sp)
library(sf)
library(raster) # getData


ind_sf <- 
  getData("GADM", country = "IND", level = 2) %>% 
  st_as_sf() %>% 
  filter(NAME_1 == "Maharashtra") %>% 
  mutate(lab_x =  )



ggplot(ind_sf) 
  geom_sf(aes(fill = NAME_2)) 
  geom_sf_text(aes(label = NAME_2),
               size = 3,
               fun.geometry = st_centroid) 
  theme_void() 
  theme(aspect.ratio = 1,
        legend.position = "bottom")

Created on 2021-12-19 by the reprex package (v2.0.1)

  • Related