Package 'ggredist'

Title: Scales, Geometries, and Extensions of 'ggplot2' for Election Mapping
Description: Provides 'ggplot2' extensions for political map making. Implements new geometries for groups of simple feature geometries. Adds palettes and scales for red to blue color mapping and for discrete maps. Implements tools for easy label generation and placement, automatic map coloring, and themes.
Authors: Cory McCartan [aut], Christopher T. Kenny [aut, cre]
Maintainer: Christopher T. Kenny <[email protected]>
License: MIT + file LICENSE
Version: 0.0.2
Built: 2024-11-24 04:40:56 UTC
Source: https://github.com/alarm-redist/ggredist

Help Index


U.S. Cities

Description

This data contains the location, name, and 2020 population of U.S. cities and large towns.

Usage

data("cities")

Format

sf object

name

City name.

state

City state.

pop_2020

City population in 2020

GEOID

Census GEOID for the corresponding Census Designated Place.

geometry

The sf geometry column containing the geographic information.

Examples

data(cities)

Aggregate and Plot Map Regions

Description

Aggregates shapefile according to the group aesthetic. If just group is provided, then by default map regions will be colored by group so that adjacent regions do not share a color (set fill to force a particular color, or NA for no fill). If fill is provided, the values in fill will be summed within the groups defined by group. If denom is provided, the values in denom will be summed within the groups defined by group, and then used to divide the summed values of fill. For example, fill and denom can be used together to plot the partisan or demographic characteristics congressional districts (see examples).

Usage

geom_district(
  mapping = NULL,
  data = NULL,
  position = "identity",
  na.rm = FALSE,
  is_coverage = FALSE,
  min_col = FALSE,
  buffer = 0,
  show.legend = NA,
  inherit.aes = TRUE,
  ...
)

stat_district(
  mapping = NULL,
  data = NULL,
  geom = GeomDistrict,
  position = "identity",
  na.rm = FALSE,
  is_coverage = FALSE,
  min_col = FALSE,
  buffer = 0,
  show.legend = NA,
  inherit.aes = TRUE,
  ...
)

Arguments

mapping

Set of aesthetic mappings created by aes()

data

The data to be displayed in this layer

position

Position adjustment

na.rm

if TRUE, will silently remove missing values from calculations

is_coverage

As in sf::st_union(). May speed up plotting for large shapefiles if geos is not installed or the shapefile is not projected.

min_col

If TRUE, try to minimize the number of colors used. May be necessary for short palettes.

buffer

Optionally buffer the merged geometries. Negative values will shrink geometries towards the center and can be used for a "glowing boundary" effect (see examples).

show.legend

Should this layer be included in the legends?

inherit.aes

If FALSE, overrides the default aesthetics, rather than combining with them.

...

Passed onto the underlying geoms.

geom

The geometric object to use display the data

Value

a ggproto object

Examples

library(ggplot2)
data(oregon)

ggplot(oregon, aes(group=county)) +
    geom_district() +
    scale_fill_penn82() +
    theme_map()

ggplot(oregon, aes(group=county, fill=pop)) +
    geom_district() +
    theme_map()

ggplot(oregon, aes(group=cd_2020, fill=ndv, denom=ndv+nrv)) +
    geom_district() +
    scale_fill_party_c(limits=c(0.4, 0.6)) +
    theme_map()

ggplot(oregon, aes(group=county)) +
    geom_district() +
    geom_district(buffer=-5000, fill="#ffffffcc", color=NA) +
    scale_fill_natgeo() +
    theme_map()

Label Map Regions

Description

Aggregates shapefile according to the group aesthetic and positions labels for each region defined by group. By default, labels will be sized in rough proportion to the available area.

Usage

geom_district_text(
  mapping = NULL,
  data = NULL,
  position = "identity",
  na.rm = FALSE,
  adjust = 1,
  check_overlap = FALSE,
  parse = FALSE,
  show.legend = NA,
  inherit.aes = TRUE,
  ...
)

geom_district_label(
  mapping = NULL,
  data = NULL,
  position = "identity",
  na.rm = FALSE,
  label.padding = ggplot2::unit(0.25, "lines"),
  label.r = ggplot2::unit(0.15, "lines"),
  label.size = 0.25,
  check_overlap = FALSE,
  parse = FALSE,
  adjust = 1,
  show.legend = NA,
  inherit.aes = TRUE,
  ...
)

stat_district_coordinates(
  mapping = NULL,
  data = NULL,
  geom = "text",
  position = "identity",
  na.rm = FALSE,
  adjust = 1,
  show.legend = NA,
  inherit.aes = TRUE,
  ...
)

Arguments

mapping

Set of aesthetic mappings created by aes()

data

The data to be displayed in this layer

position

Position adjustment

na.rm

if TRUE, will silently remove missing values from calculations

adjust

A multiplicative scaling factor for the default label sizes

check_overlap

If TRUE, text that overlaps previous text in the same layer will not be plotted.

parse

If TRUE, the labels will be parsed into expressions and displayed as described in ?plotmath.

show.legend

Should this layer be included in the legends?

inherit.aes

If FALSE, overrides the default aesthetics, rather than combining with them.

...

Passed onto the underlying geoms.

label.padding

Padding around label

label.r

Radius of rounded corners

label.size

Size of label border (mm)

geom

The geometric object to use display the data

Value

a ggproto object

Examples

library(ggplot2)
data(oregon)

ggplot(oregon, aes(group=county)) +
    geom_district() +
    geom_district_text() +
    scale_fill_randmcnally() +
    theme_map()

ggplot(oregon, aes(group=cd_2020)) +
  geom_district(lwd=0.9, color="#442222") +
  geom_district(aes(group=county), lwd=0.4, lty="dashed", fill=NA) +
  geom_district_text(aes(group=county, label=toupper(county)),
                     size=2.2, check_overlap=TRUE) +
  geom_district_text(adjust=2) +
  scale_fill_penn82() +
  theme_map()

Emphasize Populated Regions of a Map with greyed out Places

Description

Identifies relevant census places and plots them.

Usage

geom_places(
  mapping = NULL,
  data = NULL,
  position = "identity",
  na.rm = FALSE,
  state = NULL,
  show.legend = NA,
  inherit.aes = TRUE,
  ...
)

stat_places(
  mapping = NULL,
  data = NULL,
  geom = ggplot2::GeomSf,
  position = "identity",
  na.rm = FALSE,
  state = NULL,
  show.legend = NA,
  inherit.aes = TRUE,
  ...
)

Arguments

mapping

Set of aesthetic mappings created by aes()

data

The data to be displayed in this layer

position

Position adjustment

na.rm

if TRUE, will silently remove missing values from calculations

state

state to use. Guesses based on overlap if not provided.

show.legend

Should this layer be included in the legends?

inherit.aes

If FALSE, overrides the default aesthetics, rather than combining with them.

...

Passed onto the underlying geoms.

geom

The geometric object to use display the data

Value

a ggproto object

Examples

library(ggplot2)
data(oregon)

ggplot(oregon, aes(group = cd_2020)) +
  geom_district() +
  theme_map()
ggplot(oregon, aes(group = cd_2020)) +
  geom_district() +
  theme_map()

Redistricting Color Palettes

Description

Included palettes:

  • partisan, a perceptually uniform red-white-blue scale

  • dra, the Dave's Redistricting App classic color palette

  • penn82, historic categorical color palette from the 1982 published Pennsylvania congressional district map

  • randmcnally and natgeo, colors taken from Rand-McNally and National Geographic political maps

  • coast and larch, inspired by natural scenery

  • fivethirtyeight, FiveThirtyEight-inspired color palette

  • wiki_dem_pres: Wikipedia Presidential Democratic palette

  • wiki_rep_pres: Wikipedia Presidential Republican palette

  • wiki_dem: Wikipedia downballot Democratic palette

  • wiki_rep: Wikipedia downballot Republican palette

  • wiki_proposal: Wikipedia proposal support palette

  • jacksonville, Jacksonville, FL inspired color palette

  • florida, Florida inspired color palette

  • washington, Washington Redistricting Commission inspired color palette

  • alaska, Alaska inspired color palette

Usage

ggredist

Format

list of character vectors of type palette::palette (which supports a plot(), print(), and pillar generics for visualization)

Details

For details on Wikipedia-based colors, see https://en.wikipedia.org/wiki/Wikipedia:WikiProject_Elections_and_Referendums/USA_legend_colors.

Examples

plot(ggredist$partisan)
plot(ggredist$dra)
plot(ggredist$penn82)
plot(ggredist$randmcnally)
plot(ggredist$natgeo)
plot(ggredist$coast)
plot(ggredist$larch)
plot(ggredist$fivethirtyeight)
plot(ggredist$wiki_dem_pres)
plot(ggredist$wiki_rep_pres)
plot(ggredist$wiki_dem)
plot(ggredist$wiki_rep)
plot(ggredist$wiki_proposal)
plot(ggredist$jacksonville)
plot(ggredist$florida)
plot(ggredist$washington)
plot(ggredist$alaska)

Insterstate Shapefile

Description

This data contains geometry information for the U.S. Interstate Highway System. It was processed from the U.S. Census Bureau TIGER/Line Shapefile system.

Usage

data("interstates")

Format

sf object

name

Census Bureau name for the interstate

geometry

The sf geometry column containing the geographic information.

Examples

data(interstates)

Label Partisan Vote Shares

Description

For example, a 45% Democratic vote share becomes "R+10" or "55% R".

Usage

label_party_margin(midpoint = 0.5, reverse = FALSE, accuracy = 1)

label_party_pct(midpoint = 0.5, reverse = FALSE, accuracy = 1)

Arguments

midpoint

Either 0.5, the default, or 0. For label_party_margin(), if zero, scale will not be doubled (0.05 becomes "D+5" with midpoint=0, while 0.55 becomes "D+10" with 'midpoint=0.5)

reverse

If TRUE, reverse "D" and "R".

accuracy

As with scales::number_format

Value

A labeling function

Examples

labeler = label_party_margin(accuracy=0.1)
labeler(c(0.3, 0.5, 0.543))
labeler = label_party_margin(reverse=TRUE)
labeler(c(0.3, 0.5, 0.543))

Produce a Map Coloring

Description

Finds colors for every element of a shapefile so that adjacent elements don't have the same color.

Usage

map_coloring(shp, min_coloring = TRUE)

Arguments

shp

an sf object

min_coloring

if TRUE, try to minimize the number of colors used

Value

an integer vector of the same length as shp, corresponding to the coloring.

Examples

data(oregon)
or_short = oregon[30:50, ]
map_coloring(or_short)

library(ggplot2)
ggplot(or_short, aes(fill = map_coloring(or_short))) +
    geom_sf() +
    theme_map()

Oregon Redistricting File

Description

This data contains geographic, demographic, and political information on the 1,071 census tracts of the state of Oregon.

Usage

data("oregon")

Format

sf object

county

The county the tract belongs to.

cd_2020

The 2210 congressional district assignment for the tract.

pop

The total population of the tract, according to the 2020 Census.

pop_white

The non-Hispanic white population of the precinct.

ndv

Average number of votes for Democratic candidates in recent statewide elections.

nrv

Average number of votes for Republican candidates in recent statewide elections.

geometry

The sf geometry column containing the geographic information.

Examples

data(oregon)
oregon[, 1:6]

FiveThirtyEight scales for ggplot2

Description

FiveThirtyEight scales for ggplot2

Usage

scale_fill_538(...)

scale_color_538(...)

Arguments

...

additional arguments to ⁠ggplot::scale_*⁠ functions

Value

ggplot scale function

Examples

library(ggplot2)
data(oregon)

ggplot(oregon, aes(fill = ndv / (ndv + nrv))) +
    geom_sf(size = 0) +
    scale_fill_538(name = '') +
    theme_map()

Alaska Color Scales for ggplot2

Description

Alaska Color Scales for ggplot2

Usage

scale_fill_alaska(...)

scale_color_alaska(...)

scale_colour_alaska(...)

Arguments

...

Arguments passed on to ggplot2::discrete_scale()

Value

ggplot scale function

Examples

library(ggplot2)
data(oregon)

ggplot(oregon, aes(group = county)) +
    geom_district() +
    scale_fill_alaska() +
    theme_map()

Nature-derived Color Scales for ggplot2

Description

Nature-derived Color Scales for ggplot2

Usage

scale_fill_coast(...)

scale_color_coast(...)

scale_colour_coast(...)

scale_fill_larch(...)

scale_color_larch(...)

scale_colour_larch(...)

Arguments

...

Arguments passed on to ggplot2::discrete_scale()

Value

ggplot scale function

Examples

library(ggplot2)
data(oregon)

ggplot(oregon, aes(group = county)) +
    geom_district() +
    scale_fill_coast() +
    theme_map()

ggplot(oregon, aes(group = county)) +
    geom_district() +
    scale_fill_larch() +
    theme_map()

Dave's Redistricting App classic scale for ggplot2

Description

Dave's Redistricting App classic scale for ggplot2

Usage

scale_fill_dra(...)

scale_color_dra(...)

scale_colour_dra(...)

Arguments

...

Arguments passed on to ggplot2::discrete_scale()

Value

ggplot scale function

Examples

library(ggplot2)
data(oregon)

ggplot(oregon, aes(group = county, fill=county)) +
    geom_district() +
    scale_fill_dra() +
    theme_map()

Jacksonville and Florida Color Scales for ggplot2

Description

Jacksonville and Florida Color Scales for ggplot2

Usage

scale_fill_jacksonville(...)

scale_color_jacksonville(...)

scale_colour_jacksonville(...)

scale_fill_florida(...)

scale_color_florida(...)

scale_colour_florida(...)

Arguments

...

Arguments passed on to ggplot2::discrete_scale()

Value

ggplot scale function

Examples

library(ggplot2)
data(oregon)

ggplot(oregon, aes(group = county)) +
    geom_district() +
    scale_fill_jacksonville() +
    theme_map()

ggplot(oregon, aes(group = county)) +
    geom_district() +
    scale_fill_florida() +
    theme_map()

Historical Pennsylvania Color Scale for ggplot2

Description

Historical Pennsylvania Color Scale for ggplot2

Usage

scale_fill_penn82(...)

scale_color_penn82(...)

scale_colour_penn82(...)

Arguments

...

Arguments passed on to ggplot2::discrete_scale()

Value

ggplot scale function

Examples

library(ggplot2)
data(oregon)

ggplot(oregon, aes(group = county)) +
    geom_district() +
    scale_fill_penn82() +
    theme_map()

Rand-McNally and National Geographic Color Scales for ggplot2

Description

Rand-McNally and National Geographic Color Scales for ggplot2

Usage

scale_fill_randmcnally(...)

scale_color_randmcnally(...)

scale_colour_randmcnally(...)

scale_fill_natgeo(...)

scale_color_natgeo(...)

scale_colour_natgeo(...)

Arguments

...

Arguments passed on to ggplot2::discrete_scale()

Value

ggplot scale function

Examples

library(ggplot2)
data(oregon)

ggplot(oregon, aes(group = county)) +
    geom_district() +
    scale_fill_randmcnally() +
    theme_map()

ggplot(oregon, aes(group = county)) +
    geom_district() +
    scale_fill_natgeo() +
    theme_map()

Washington Redistricting Commission Color Scales for ggplot2

Description

Washington Redistricting Commission Color Scales for ggplot2

Usage

scale_fill_washington(...)

scale_color_washington(...)

scale_colour_washington(...)

Arguments

...

Arguments passed on to ggplot2::discrete_scale()

Value

ggplot scale function

Examples

library(ggplot2)
data(oregon)

ggplot(oregon, aes(group = county)) +
    geom_district() +
    scale_fill_washington() +
    theme_map()

Wikipedia Style Election Map scales for ggplot2

Description

Wikipedia Style Election Map scales for ggplot2

Usage

scale_fill_wiki_rep(...)

scale_color_wiki_rep(...)

scale_fill_wiki_dem(...)

scale_color_wiki_dem(...)

scale_fill_wiki_rep_pres(...)

scale_color_wiki_rep_pres(...)

scale_fill_wiki_dem_pres(...)

scale_color_wiki_dem_pres(...)

Arguments

...

additional arguments to ⁠ggplot::scale_*⁠ functions

Value

ggplot scale function

Examples

library(ggplot2)
data(oregon)

ggplot(oregon, aes(fill = ndv / (ndv + nrv))) +
    geom_sf(size = 0) +
    scale_fill_wiki_dem(name = 'Dem Share') +
    theme_map()

Partisan scales for ggplot2

Description

Partisan scales for ggplot2

Usage

scale_fill_party_c(
  name = "Vote share",
  midpoint = 0.5,
  limits = 0:1,
  labels = label_party_pct(),
  oob = scales::squish,
  reverse = FALSE,
  ...
)

scale_color_party_c(
  name = "Vote share",
  midpoint = 0.5,
  limits = 0:1,
  labels = label_party_pct(),
  oob = scales::squish,
  reverse = FALSE,
  ...
)

scale_fill_party_d(labels = c("Rep.", "Dem."), reverse = FALSE, ...)

scale_color_party_d(labels = c("Rep.", "Dem."), reverse = FALSE, ...)

scale_fill_party_b(
  name = "Vote share",
  midpoint = 0.5,
  limits = 0:1,
  labels = label_party_pct(),
  oob = scales::squish,
  reverse = FALSE,
  nice.breaks = FALSE,
  ...
)

scale_color_party_b(
  name = "Vote share",
  midpoint = 0.5,
  limits = 0:1,
  labels = label_party_pct(),
  oob = scales::squish,
  reverse = FALSE,
  nice.breaks = FALSE,
  ...
)

scale_colour_party_d(labels = c("Rep.", "Dem."), reverse = FALSE, ...)

scale_colour_party_c(
  name = "Vote share",
  midpoint = 0.5,
  limits = 0:1,
  labels = label_party_pct(),
  oob = scales::squish,
  reverse = FALSE,
  ...
)

scale_colour_party_b(
  name = "Vote share",
  midpoint = 0.5,
  limits = 0:1,
  labels = label_party_pct(),
  oob = scales::squish,
  reverse = FALSE,
  nice.breaks = FALSE,
  ...
)

Arguments

name

Name for scale. Default is "Vote share".

midpoint

Scale midpoint value. Default is 0.5.

limits

Lower and upper limits for scale. Default is 0:1.

labels

Function to adjust scale labels. Default is scales::percent.

oob

Function to deal with out of bounds. Default is scales::squish().

reverse

Whether to reverse red and blue.

...

Additional arguments to ⁠ggplot::scale_*⁠ functions

nice.breaks

If TRUE, attempt to place breaks at nice values instead of exactly evenly spaced between the limits.

Value

ggplot scale function

Examples

library(ggplot2)
data(oregon)

ggplot(oregon, aes(fill = ndv / (ndv + nrv))) +
    geom_sf(size = 0) +
    scale_fill_party_c(limits=c(0.3, 0.7)) +
    theme_map()

ggplot(oregon, aes(fill = ndv / (ndv + nrv))) +
    geom_sf(size = 0) +
    scale_fill_party_b() +
    theme_map()

Annotate a Map with Roads and Cities

Description

Clips the interstates and cities datasets to the bounding box of the map and plots them.

Usage

stat_cities(
  mapping = NULL,
  data = NULL,
  geom = ggplot2::GeomSf,
  position = "identity",
  na.rm = FALSE,
  adjust = 1,
  min_pop = 1e+05,
  show.legend = NA,
  inherit.aes = TRUE,
  ...
)

stat_interstates(
  mapping = NULL,
  data = NULL,
  geom = ggplot2::GeomSf,
  position = "identity",
  na.rm = FALSE,
  show.legend = NA,
  inherit.aes = TRUE,
  ...
)

Arguments

mapping

Set of aesthetic mappings created by aes()

data

The data to be displayed in this layer

geom

The geometric object to use display the data

position

Position adjustment

na.rm

if TRUE, will silently remove missing values from calculations

adjust

A multiplicative scaling factor for the default label sizes

min_pop

The minimum population a city must have had in 2006 to be shown.

show.legend

Should this layer be included in the legends?

inherit.aes

If FALSE, overrides the default aesthetics, rather than combining with them.

...

Passed onto the underlying geoms.

Value

a ggproto object

Examples

library(ggplot2)
data(oregon)

ggplot(oregon, aes(group=cd_2020)) +
  geom_district() +
  stat_interstates(size=1.4, color="#0044aa55") +
  stat_cities(geom="text", min_pop=130e3, fontface="bold", adjust=0.8) +
  scale_fill_penn82() +
  theme_map()

Map Theme

Description

Theme for maps which uses the 'Times' family and has a transparent background.

Usage

theme_map(...)

Arguments

...

additional parameters

Value

ggplot2 theme

Examples

library(ggplot2)
data(oregon)

ggplot(oregon, aes(group = county)) +
    geom_district() +
    scale_fill_penn82() +
    theme_map()