To prepare a choropleth map showing district-wise average rainfall in Delhi using R programming and the supplied government rainfall dataset.
A choropleth map is a thematic map in which geographical areas are shaded with different colours according to the value of a variable. In this assignment, Delhi districts are shaded according to their average recorded rainfall. Darker shades represent higher average rainfall and lighter shades represent lower average rainfall.
The rainfall data is supplied in the file
rainfall_data.csv. The dataset contains the columns
State, District, Date,
Year, Month, Avg_rainfall, and
Agency_name. The analysis below uses Delhi records for
2025.
# Install packages only once, if they are not already installed
packages <- c("sf", "dplyr", "ggplot2", "readr", "lubridate", "geodata", "stringr")
new_packages <- packages[!(packages %in% installed.packages()[, "Package"])]
if (length(new_packages)) install.packages(new_packages)
# Load the required packages
library(sf)
library(dplyr)
library(ggplot2)
library(readr)
library(lubridate)
library(geodata)
library(stringr)
# Read the supplied CSV file. Keep it in the same folder as this RMD file.
rainfall_raw <- read_csv("rainfall_data.csv", show_col_types = FALSE)
# Display the original column names
names(rainfall_raw)
## [1] "State" "District" "Date" "Year" "Month"
## [6] "Avg_rainfall" "Agency_name"
# Rename the CSV columns (which start with capital letters) to lowercase
rainfall <- rainfall_raw %>%
rename(
state = State,
district = District,
date = Date,
year = Year,
month = Month,
avg_rainfall = Avg_rainfall,
agency_name = Agency_name
)
# Convert columns into suitable data types
rainfall <- rainfall %>%
mutate(
date = as.Date(date),
year = as.integer(year),
avg_rainfall = as.numeric(avg_rainfall),
state = str_to_lower(str_squish(state)),
district = str_to_lower(str_squish(district))
)
# Remove exact duplicate records before calculating district averages.
# The supplied file contains repeated identical rows for district-date records.
rainfall <- rainfall %>% distinct()
# Select Delhi records for 2025 and calculate mean recorded rainfall by district
rainfall_delhi <- rainfall %>%
filter(state == "delhi", year == 2025) %>%
group_by(district) %>%
summarise(
avg_rainfall = mean(avg_rainfall, na.rm = TRUE),
.groups = "drop"
) %>%
filter(is.finite(avg_rainfall))
# Display the district-wise average rainfall table
rainfall_delhi
## # A tibble: 11 × 2
## district avg_rainfall
## <chr> <dbl>
## 1 central 0.763
## 2 east 0.805
## 3 new delhi 1.31
## 4 north 0.863
## 5 north east 0.723
## 6 north west 0.961
## 7 shahdara 0.726
## 8 south 1.68
## 9 south east 1.66
## 10 south west 1.04
## 11 west 0.953
# Download district-level administrative boundaries for India from GADM
india_districts <- geodata::gadm(country = "IND", level = 2, path = tempdir())
# Convert the downloaded boundaries into an sf object
india_districts <- st_as_sf(india_districts)
# Select the districts belonging to Delhi
# Check the NAME_1 values if the filter returns no rows.
sort(unique(india_districts$NAME_1))
## [1] "Andaman and Nicobar" "Andhra Pradesh" "Arunachal Pradesh"
## [4] "Assam" "Bihar" "Chandigarh"
## [7] "Chhattisgarh" "Dadra and Nagar Haveli" "Daman and Diu"
## [10] "Goa" "Gujarat" "Haryana"
## [13] "Himachal Pradesh" "Jammu and Kashmir" "Jharkhand"
## [16] "Karnataka" "Kerala" "Lakshadweep"
## [19] "Madhya Pradesh" "Maharashtra" "Manipur"
## [22] "Meghalaya" "Mizoram" "Nagaland"
## [25] "NCT of Delhi" "Odisha" "Puducherry"
## [28] "Punjab" "Rajasthan" "Sikkim"
## [31] "Tamil Nadu" "Telangana" "Tripura"
## [34] "Uttar Pradesh" "Uttarakhand" "West Bengal"
delhi_map <- india_districts %>%
filter(str_to_lower(str_squish(NAME_1)) %in% c("delhi", "nct of delhi"))
# Display the Delhi district names available in the shapefile
sort(unique(delhi_map$NAME_2))
## [1] "West"
# Standardise district names in the boundary file for joining.
# For example, 'Central Delhi' becomes 'central', while 'New Delhi' stays 'new delhi'.
delhi_map <- delhi_map %>%
mutate(
district = str_to_lower(str_squish(NAME_2)),
district = case_when(
district %in% c("central delhi") ~ "central",
district %in% c("east delhi") ~ "east",
district %in% c("north delhi") ~ "north",
district %in% c("north east delhi", "northeast delhi") ~ "north east",
district %in% c("north west delhi", "northwest delhi") ~ "north west",
district %in% c("south delhi") ~ "south",
district %in% c("south east delhi", "southeast delhi") ~ "south east",
district %in% c("south west delhi", "southwest delhi") ~ "south west",
district %in% c("west delhi") ~ "west",
district %in% c("shahdara delhi") ~ "shahdara",
TRUE ~ district
)
)
# Join the rainfall table to the district boundaries
map_data <- delhi_map %>%
left_join(rainfall_delhi, by = "district")
# Check the values matched to each district
map_data %>%
st_drop_geometry() %>%
select(NAME_2, district, avg_rainfall)
## NAME_2 district avg_rainfall
## 1 West west 0.953232
# Show districts for which no rainfall value was matched
unmatched_districts <- map_data %>%
st_drop_geometry() %>%
filter(is.na(avg_rainfall)) %>%
pull(NAME_2)
unmatched_districts
## character(0)
ggplot(data = map_data) +
geom_sf(aes(fill = avg_rainfall), colour = "white", linewidth = 0.35) +
scale_fill_gradient(
name = "Average rainfall\n(mm)",
low = "#deebf7",
high = "#08519c",
na.value = "grey85"
) +
labs(
title = "District-wise Average Rainfall in Delhi (2025)",
subtitle = "Source: supplied district-wise rainfall CSV",
caption = "Grey areas indicate districts for which no matching value was available."
) +
theme_minimal() +
theme(
plot.title = element_text(face = "bold", size = 15),
legend.position = "right",
axis.title = element_blank(),
axis.text = element_blank(),
panel.grid = element_blank()
)
The map shows the spatial variation in average recorded rainfall among the Delhi districts during the dates available in the 2025 dataset. Districts shown in darker shades have a higher average rainfall value, while districts shown in lighter shades have a lower average rainfall value. Grey areas indicate missing values or district names that could not be matched between the CSV and the boundary file. The highest and lowest district values are identified by the R code below rather than being assumed in advance.
# District with the highest average recorded rainfall
rainfall_delhi %>%
arrange(desc(avg_rainfall)) %>%
slice_head(n = 1)
## # A tibble: 1 × 2
## district avg_rainfall
## <chr> <dbl>
## 1 south 1.68
# District with the lowest average recorded rainfall
rainfall_delhi %>%
arrange(avg_rainfall) %>%
slice_head(n = 1)
## # A tibble: 1 × 2
## district avg_rainfall
## <chr> <dbl>
## 1 north east 0.723
A district-wise choropleth map of average recorded rainfall in Delhi for the available 2025 observations was prepared in R using the supplied CSV dataset and district boundary shapefile.
The choropleth map helps us understand how rainfall values vary from one district to another. It presents numerical information in a simple visual form and makes comparisons between districts easier. This assignment demonstrates how R can be used to import and clean data, work with geographical boundaries, join datasets and create a thematic map.
rainfall_data(1).csv.sf, dplyr,
ggplot2, readr, lubridate,
geodata, stringr, and
viridis.