Making a choropleth map of Bangladesh using R
Spatial data analysis and visualization play a crucial role in understanding the geographic landscape of a region. In this tutorial, we’ll see how to create detailed maps of Bangladesh at the division and district levels using the sf
(Simple Features) package in R.
The sf
package offers a comprehensive set of tools for working with spatial data, making it an excellent choice. The first is to load the necessary libraries and download the shapefile (I used this website).
library(sf)
library(ggplot2)
# read the shapefile at division level
sf1 <- st_read(dsn="~/Downloads/BGD_adm/BGD_adm1.shp", layer="BGD_adm1")
# read the shapefile at district level
sf2 <- st_read(dsn="~/Downloads/BGD_adm/BGD_adm2.shp", layer="BGD_adm2")
Right now, there is no real-life variable to plot in the data. So, we will make a fictitious population
column to make the plot.
First, we will make the plot at the division level:
sf1$population <- sample(50000:500000, nrow(sf1), replace = T)
ggplot() +
geom_sf(data = sf1, aes(fill = population)) +
scale_fill_gradient(low = "#f8ead6", high = "#98df8a")
And then at the district level:
sf2$population <- sample(50000:500000, nrow(sf2), replace = T)
ggplot() +
geom_sf(data = sf2, aes(fill = population)) +
scale_fill_gradient(low = "#f8ead6", high = "#98df8a")