ggplot2 Choropleth Map

Author

David Gerbing

Published

Apr 22, 2026, 09:21 am

Access Needed Packages

As always, all contributed packages must first be installed into the R library with install.packages(), also accessed from the RStudio Tools menu. Then, accessed by retrieving them from the library with the library() function.

suppressPackageStartupMessages(library(lessR))
style(suggest=FALSE)
library(sf)
Linking to GEOS 3.13.0, GDAL 3.8.5, PROJ 9.5.1; sf_use_s2() is TRUE
library(rnaturalearth)  # access map data for plotting Italy
library(ggrepel)  # access the function for plotting city names
Loading required package: ggplot2
library(ggplot2)

Choropleth Map

Repeat Work from the mapview Content

First get the data for the states of the USA, already accomplished from mapview, and repeated here without comment.

states_sf <- ne_states(country="United States of America")
states_sf <- subset(states_sf,
                    type %in% c("State", "Federal district"),
                    select = c("name", "postal", "geometry"))
d_gini <- Read("https://dgerbing.github.io/data/Gini2023.xlsx",
               quiet=TRUE)
states_sf = merge(states_sf, d_gini, by="name")
head(states_sf)
Simple feature collection with 6 features and 3 fields
Geometry type: MULTIPOLYGON
Dimension:     XY
Bounding box:  xmin: -179.1435 ymin: 30.22362 xmax: 179.7809 ymax: 71.4125
Geodetic CRS:  WGS 84
        name postal   Gini                       geometry
1    Alabama     AL 0.4783 MULTIPOLYGON (((-87.41958 3...
2     Alaska     AK 0.4333 MULTIPOLYGON (((-141.0056 6...
3    Arizona     AZ 0.4624 MULTIPOLYGON (((-111.0063 3...
4   Arkansas     AR 0.4807 MULTIPOLYGON (((-90.30422 3...
5 California     CA 0.4887 MULTIPOLYGON (((-114.7243 3...
6   Colorado     CO 0.4576 MULTIPOLYGON (((-109.0463 4...

Create the Maps

To show a map at appropriate scale, map only the contiguous states. So, remove Alaska and Hawaii from the sf data frame. Including them often changes the bounding box and can shrink the visible map.

states48_sf <- subset(states_sf, !name %in% c("Alaska", "Hawaii"))

Plot with default blue sequential scale. Set the fill color of each state according to its Gini index, creating a choropleth map. Accomplish the choropleth map with one line of ggplot2 code via the geom_sf() function that accesses our simple features data file: states48_sf.

ggplot() +
  geom_sf(data=states48_sf, aes(fill=Gini))

Plot with a custom sequential scale using the Base R function hcl(). The function has the following parameters.

  • hue=0 on the color wheel (red)
  • chroma=70 (saturation, 0 is gray, 100 is a fully saturated color)
  • luminance varies from 20 to 85 (0 is black, 100 is bright as possible)
ggplot() + geom_sf(data=states48_sf, aes(fill=Gini)) +
  scale_fill_gradient(low=hcl(0, 70, 20), high=hcl(0, 70, 85))

For more enhancement, create a divergent palette to further highlight the differences between the Gini values of the states.

  • hue varies from 0 (red) to 240 (blue)
  • chroma = 70
  • luminance varies from 20 to 85

Also:

  • Clean the map some by removing the axis values and tick marks, as well as the gray background.
  • Use a custom coordinate reference system, 5070 with the coord_sf function, that better reflects the earth’s curvature.
ggplot() + geom_sf(data=states48_sf, aes(fill=Gini)) +
  coord_sf(crs=5070) +
  scale_fill_gradient(low=hcl(0, 70, 20), high=hcl(240, 70, 85)) +
  theme(
    panel.background = element_rect(fill="white"),
    axis.ticks=element_blank(),
    axis.text.x=element_blank(),
    axis.text.y=element_blank()
  )