rggplot2cartography

How to create State & district level map in using GADM and ggplot?


I am using Covid data & looking to plot State & district level Indian data on map.

I have State, District Name of India along with Cases but do not have needed lat, long for them.

I came across this so post How to map an Indian state with districts in r?

and tried raster::getData("GADM", country = "India", level = 2) %>% as_tibble() but this doesn't work as it doesnt have lat,lon, shapefile etc.

library(raster)
library(rgdal)
library(rgeos)

state_level_map <- raster::getData("GADM", country = "India", level = 1) %>% 
  as_tibble() %>% 
  filter(NAME_1 == "Rajasthan") %>% 
  fortify() 

  ggplot() +
  geom_map(data= state_level_map, map = state_level_map, 
           aes(x = long, y = lat, map_id = id, group = group))

I am new to spatial data / maps and not sure how exactly I can proceed in this situation. Is it possible to get lat, lon, shapefile etc. for State/districts name's info from any r packages or the only way is to manually google them for lat,lon ?

Appreciate any help.


Solution

  • You were almost there. Use sf for that.

    library(raster)
    library(sf)
    library(rgeos)
    library(dplyr)
    
    state_level_map <- raster::getData("GADM", country = "India", level = 1) %>%
      st_as_sf() %>%
      filter(NAME_1 == "Rajasthan")
    
    ggplot() +
      geom_sf(data = state_level_map)
    

    enter image description here

    you can then easily use aes() to change your aesthetics of the ggplot as you normally would using variables.

    sf uses a dataframe-like notation that incorporates both attribute data as well as geometries into a single and easy to use dataframe. just have a look at print(state_level_map). That is, you could join data using district names to augment you attributes and visualize them through aes(color = yourjoinedvar).