Our youtube channel

Our youtube channel has lots of videos on data visualisation in r.

Download the shape files for the boundaries

We would need the shape files which will be used to draw the boundaries which are needed to make a choropleth maps. The following website has various shape files for India. If you do not like the boundary files, then find any shape files you find for the country and use them.

https://www.diva-gis.org/datadown

Here is a direct link https://biogeo.ucdavis.edu/data/diva/adm/IND_adm.zip

Extract the data and we will be mainly interested in the .shp files

Libraries needed

library(ggplot2)
library(sf)
library(rvest)
library(dplyr)
library(viridis)
library(ggrepel)
library(ggthemes)

Get the shape files loaded.

How do you get the shape files is described above.


shp0 <-  read_sf("E:\\Sidebar Items\\Documents\\r scripts\\India SHP Files\\IND_adm\\IND_adm0.shp")
shp1 <-  read_sf("E:\\Sidebar Items\\Documents\\r scripts\\India SHP Files\\IND_adm\\IND_adm1.shp")
shp2 <-  read_sf("E:\\Sidebar Items\\Documents\\r scripts\\India SHP Files\\IND_adm\\IND_adm2.shp")
shp3 <-  read_sf("E:\\Sidebar Items\\Documents\\r scripts\\India SHP Files\\IND_adm\\IND_adm3.shp")





shp1 <- shp1%>%
  dplyr::mutate(NAME_1 = if_else(NAME_1 == 'Uttaranchal'
                                 , 'Uttarakhand'   
                                 , NAME_1))
 

Country Map

Just the boundary of the whole country without any states or any other details. shp0 file just has the country boundary.

                           

# country level map
pl1 <- ggplot(shp0)
pl1 <- pl1 +  geom_sf(fill="#E8442A", color ="#F53214")
pl1 <- pl1 +  ggthemes::theme_map()
pl1 <- pl1 + labs(title = "India country map")

pl1

# Plot the states of the country shp1 file consists the boundaries of each state.

# Fill each state in different colour ----
pl2 <- ggplot(shp1)
pl2 <- pl2 +   geom_sf(aes(fill  = NAME_1))
pl2 <- pl2 +  ggthemes::theme_map()
pl2 <- pl2 +  theme(legend.position = "none")
pl2 <- pl2 + labs(title = "India States map")

pl2 <- pl2 + scale_fill_viridis_d()
pl2

District boundaries

shp2 file contains the district boundaries.


# Fill each district in different colour ----
pl3 <- ggplot(shp2)
pl3 <- pl3 +   geom_sf(aes(fill  = NAME_2))
pl3 <- pl3 +  ggthemes::theme_map()
pl3 <- pl3 +  theme(legend.position = "none")
pl3 <- pl3 + labs(title = "India Districts map")

pl3 <- pl3 + scale_fill_viridis_d(option="magma")
pl3

Wards boundaries

shp3 file contains these Wards boundaries,


pl4 <- ggplot(shp3)
pl4 <- pl4 +   geom_sf(aes(fill  = NAME_3))
pl4 <- pl4 +  ggthemes::theme_map()
pl4 <- pl4 +  theme(legend.position = "none")
pl4 <- pl4 + labs(title = "India Wards map")
pl4 <- pl4 + scale_fill_viridis_d(option="magma")
pl4

Selective fill and selective text labels.

In this example we just want to fill the Union Territories and display the names as well.


# Show selected areas ----
pl5 <- ggplot(shp1)
pl5 <- pl5 +   geom_sf(aes(fill  = (ENGTYPE_1 %in% 'Union Territory'))
                       ,  size = .2, alpha = 0.5)
pl5 <- pl5 + geom_sf_text(aes(label = if_else(ENGTYPE_1 %in% 'Union Territory',NAME_1,""))
                          , size = 3, colour = "red")
pl5 <- pl5 +  ggthemes::theme_map()
pl5 <- pl5 +  theme(legend.position = "none")
pl5 <- pl5 +  scale_fill_manual(values = c("#B3E82A" ,  "#E8442A"))
pl5 <-  pl5 + labs(title = "India Map highlighting the Union Territories")
pl5

Selective filling of states

We can choose which states to fill and then show the labels for them also.



dstates <- c('Uttar Pradesh'
               ,'Uttarakhand'
               , 'Himachal Pradesh'
               , 'Gujarat'
               ,'Goa'
               ,'Manipur'
               ,'Punjab'
              )

pl6 <- ggplot(shp1)
pl6 <- pl6 +   geom_sf(aes(fill  = NAME_1 %in% dstates)
                           , size = .5
                           , colour = "white")
pl6 <- pl6 + geom_sf_text(data = filter(shp1, NAME_1 %in% dstates)
                          ,aes(label = NAME_1), size = 3)
pl6 <- pl6 +  ggthemes::theme_map()
#pl5 <- pl + theme_bw()
pl6 <- pl6 +  theme(legend.position = "none")
pl6 <- pl6 +  scale_fill_manual(values = c("grey" ,  "#E8442A"))
pl6 <-  pl6 + labs(title = "India Map showing states\nwith elections in 2022")
pl6

Create Choropleth maps

By using some dummy sales data for each of these states we can create a choropleth map.


#Choropleth maps
# fill each state based on the your own data

shp1$sales <- c(7261
          , 1810000
          , 31282
          , 459000
          , 717000
          , 61110
          , 987000
          ,0
          ,0
          ,1430000
          , 162000
          , 820000
          , 766000
          , 199000
          , 307000
          , 343000
          , 2770000
          , 2730000
          , 9209
          , 788000
          , 5910000
          , 59852
          , 41906
          , 15364
          , 23644
          , 852000
          , 113000
          , 588000
          , 950000
          , 18414
          , 2350000
          , 603000
          , 59321
          , 1700000
          , 337000
          , 1460000
)
options(scipen = 999)
pl7 <- ggplot(shp1)
pl7 <- pl7 +   geom_sf(aes(fill  = sales), alpha = 0.7)
pl7 <- pl7 + geom_sf_text(aes(label = NAME_1), size = 2, color = "black")
pl7 <- pl7 +  ggthemes::theme_map()
pl7 <- pl7 +  theme(legend.position = "right")
pl7 <-  pl7 + labs(title = "India Choropleth map\nshowing sales in each state")
pl7 <- pl7 +  scale_fill_viridis(option="magma")
pl7

# show roads on the map We use the shp1 file to show the states and then use the roads file to show the roads on the map.



# Roads ----
roads <- read_sf("E:\\Sidebar Items\\Documents\\r scripts\\India SHP Files\\IND_rds\\IND_roads.shp")

pl8 <- ggplot()
pl8 <- pl8 +   geom_sf(data = shp1,colour = "blue")
pl8 <- pl8 +   geom_sf(data = roads,colour = "red" , alpha = 0.2)
pl8 <- pl8 +  ggthemes::theme_map()
pl8 <- pl8 +  theme(legend.position = "none")
pl8 <-  pl8 + labs(title = "India roads network")
pl8



pl9 <- ggplot()
pl9 <- pl9 +   geom_sf(data = shp1, color = "grey")
pl9 <- pl9 +   geom_sf(data = roads%>%dplyr::filter(RTT_DESCRI =='Primary Route')
                       , colour = "red", alpha = 0.5)
pl9 <- pl9 +  ggthemes::theme_map()
pl9 <- pl9 +  theme(legend.position = "none")
pl9 <-  pl9 + labs(title = "India - Primary routes")

pl9


# Water ----

water <- read_sf("E:\\Sidebar Items\\Documents\\r scripts\\India SHP Files\\IND_wat\\IND_water_lines_dcw.shp")


water1 <- water%>%
          dplyr::filter(NAM  %in% c('INDUS', "NARMADA") )


pl10 <- ggplot()
pl10 <- pl10 + geom_sf(data = shp1,  color = "grey")
pl10 <- pl10 + geom_sf(data = water, color = "blue", alpha = 0.5)
pl10 <- pl10 +  ggthemes::theme_map()
pl10 <- pl10 +  theme(legend.position = "none")
pl10 <-  pl10 + labs(title = "India- water channels, canals, rivers")
pl10


url <- "https://www.latlong.net/category/hospitals-102-26.html"
ds <- read_html(url)

# there is a table in the webpage which displays the data
# As this is one table on the web page so we get the first table which is available on the web page

ds <- html_table(html_nodes(ds, "table")[[1]])
library(ggrepel)

pl11 <- ggplot()
pl11 <- pl11 + geom_sf(data = shp1, alpha = 0.5, color = "orange", fill = "orange")
pl11 <- pl11 +  geom_point(data = ds
                       , aes(x = Longitude
                          , y = Latitude
                          , color  = `Place Name` == 'Fortis Memorial Research Institute, Gurgaon, India'
                          ))
pl11 <- pl11 + ggrepel::geom_text_repel(data = ds, aes(x = Longitude
                                    , y = Latitude
          , label= if_else(`Place Name` == 'Fortis Memorial Research Institute, Gurgaon, India'
                                                    ,  `Place Name`,''))
                      , color  = "blue", size = 2)

#pl <- pl + geom_sf_text(data = water1, aes(label = NAM), size = 2, color = "red")

pl11 <- pl11 +  ggthemes::theme_map()
pl11 <- pl11 +  theme(legend.position = "none")
pl11 <- pl11 + scale_color_manual(values = c("red","blue"))
pl11 <-  pl11 + labs(title = "India - some major hospitals")
pl11

Subscribe to our channel for various data visualisation and statistics videos in R.

https://www.youtube.com/c/TechAnswers88?s_confirmation=1