Objectives:

Install and Load Required Packages

# Install the remotes package if not already installed
# install.packages("remotes")

# Install the 'bangladesh' package from GitHub
# remotes::install_github("ovirahman/bangladesh")

# Load the necessary libraries
library(bangladesh)
library(tmap)
## Breaking News: tmap 3.x is retiring. Please test v4, e.g. with
## remotes::install_github('r-tmap/tmap')
library(dplyr)
## 
## Attaching package: 'dplyr'
## The following objects are masked from 'package:stats':
## 
##     filter, lag
## The following objects are masked from 'package:base':
## 
##     intersect, setdiff, setequal, union

Retrieve Map Data

country <- get_map("country")
head(country)
division <- get_map("division")
head(division)
district <- get_map("district")
head(district)
upazila <- get_map("upazila")
head(upazila)
union <- get_map("union")
head(union)

Visualize Simple Maps

# Plot maps for the country and divisions
bd_plot("country")    # Plot the country map
## tmap mode set to plotting

bd_plot("division")   # Plot division boundaries
## tmap mode set to plotting

Merge Population Data with District Map

# Extract district-wise population data
population <- bangladesh::pop_district_2011[, c("district", "population")]

# Retrieve district map
district <- get_map("district")

# Merge population data with the district map
map_data <- dplyr::left_join(district, population, by = c("District" = "district"))

Create a Thematic Map Using tmap

# Create the map
map <- tm_shape(map_data) + 
  tm_polygons("population", id = "District", palette = "Reds", title = "Population") +
  tm_style("cobalt") +
  tm_layout(
    "Bangladesh District Wise Population Map\nSource: BBS",
    title.position = c("left", "bottom"),
    legend.position = c("right", "top")
  )

# Display the map
map

## Interactive tmap

tmap_mode("view")
## tmap mode set to interactive viewing
map <- tm_shape(map_data) + 
  tm_polygons("population",id = "District",palette = "Reds", title = "Population") +
  tm_layout(
    "Bangladesh District Wise Population Map\nSource: BBS",
    title.position = c("left", "bottom"),
    legend.position = c("right", "top")
    )
map + tm_view(set.view = c( 90.3563, 23.6850,7))
## legend.postion is used for plot mode. Use view.legend.position in tm_view to set the legend position in view mode.