plot_map: Create a choropleth map from census data.

View source: R/plotting.R

plot_mapR Documentation

Create a choropleth map from census data.

Description

Create a choropleth map from census data.

Usage

plot_map(
  data,
  fill_var,
  title = NULL,
  subtitle = NULL,
  legend_title = NULL,
  palette = "viridis",
  reverse = FALSE,
  direction = NULL,
  na_color = "grey80",
  boundary_year = NULL,
  base_layer = TRUE,
  show_state_boundaries = FALSE,
  state_boundary_color = "black",
  state_boundary_width = 0.3,
  trans = "identity",
  midpoint = NULL,
  oob = NULL,
  limits = NULL,
  breaks = NULL,
  labels = NULL,
  font_family = NULL,
  legend_position = "right"
)

Arguments

data

An sf object or data frame with geometry attached via attach_geometry

fill_var

The variable to map to fill color (unquoted or string)

title

Optional title for the map

subtitle

Optional subtitle for the map

legend_title

Title for the legend

palette

Color palette. Options include:

  • "viridis" - Perceptually uniform, colorblind-friendly (default)

  • "red_blue" - Diverging red to blue (red = high)

  • "blue_red" - Diverging blue to red (blue = high)

  • "greens" - Sequential green palette

  • "oranges" - Sequential orange palette

  • "reds" - Sequential red palette

  • "blues" - Sequential blue palette

  • "purple_green" - Diverging purple to green

reverse

Logical. Reverse the color palette direction? Default FALSE.

direction

Numeric. Alternative to reverse: use -1 to reverse, 1 for default. If both reverse and direction are specified, direction takes precedence.

na_color

Color for missing values. Default is "grey80"

boundary_year

Census year whose state boundaries to draw beneath the data, keeping India's full claimed extent on the map even where the census enumerates no districts. Defaults to the year column if present.

base_layer

Logical. Draw India's full state outline beneath the data? Default TRUE

show_state_boundaries

Logical. Overlay state boundaries?

state_boundary_color

Color for state boundaries. Default is "black"

state_boundary_width

Width of state boundary lines. Default is 0.3

trans

Transformation for the color scale (e.g., "log10", "sqrt"). Default is "identity" (no transformation)

midpoint

Value to centre a diverging palette on, e.g. 0 for growth or 1000 for a sex ratio. Without it the palette centres on the middle of limits, which is rarely the meaningful value.

oob

How to treat values outside limits. The ggplot2 default censors them to na_color, making them indistinguishable from genuinely missing data; pass scales::squish to clamp them to the nearest end instead.

limits

Optional limits for the color scale as c(min, max)

breaks

Optional breaks for the legend

labels

Optional labels for the legend breaks

font_family

Font family for all map text. Default NULL uses the system default; e.g. "Arial".

legend_position

One of "right" (default), "left", "bottom", "top", or "none". "bottom"/"top" also turns the colorbar horizontal.

Value

A ggplot2 object

Examples


# Calculate ST percentage and attach geometry
st_data <- census_2011_pca |>
  dplyr::mutate(st_pct = 100 * st_population / population_total) |>
  attach_geometry(2011)

# Create a basic map
plot_map(st_data, st_pct, title = "ST Population %")

# With red-blue palette and state boundaries
plot_map(st_data, st_pct,
  title = "ST Population % (2011)",
  palette = "red_blue",
  show_state_boundaries = TRUE
)


censusindia documentation built on Oct. 15, 2026, 5:08 p.m.