Title: | Scales, Geometries, and Extensions of 'ggplot2' for Election Mapping |
Version: | 0.0.2 |
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. |
Depends: | R (≥ 2.10) |
Imports: | grDevices, ggplot2, scales |
Suggests: | sf, geos, redist |
License: | MIT + file LICENSE |
Encoding: | UTF-8 |
RoxygenNote: | 7.2.1 |
URL: | https://github.com/alarm-redist/ggredist, https://alarm-redist.org/ggredist/ |
BugReports: | https://github.com/alarm-redist/ggredist/issues |
Config/testthat/edition: | 3 |
LazyData: | true |
NeedsCompilation: | no |
Packaged: | 2022-11-23 01:31:33 UTC; chris |
Author: | Cory McCartan [aut],
Christopher T. Kenny
|
Maintainer: | Christopher T. Kenny <christopherkenny@fas.harvard.edu> |
Repository: | CRAN |
Date/Publication: | 2022-11-23 11:20:02 UTC |
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 |
data |
The data to be displayed in this layer |
position |
Position adjustment |
na.rm |
if |
is_coverage |
As in |
min_col |
If |
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 |
... |
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 |
data |
The data to be displayed in this layer |
position |
Position adjustment |
na.rm |
if |
adjust |
A multiplicative scaling factor for the default label sizes |
check_overlap |
If |
parse |
If |
show.legend |
Should this layer be included in the legends? |
inherit.aes |
If |
... |
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 |
data |
The data to be displayed in this layer |
position |
Position adjustment |
na.rm |
if |
state |
state to use. Guesses based on overlap if not provided. |
show.legend |
Should this layer be included in the legends? |
inherit.aes |
If |
... |
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
Usage
ggredist
Format
list of character vectors of type palette
(which supports a
plot()
generic for visualization)
Examples
plot(ggredist$partisan)
plot(ggredist$dra)
plot(ggredist$penn82)
plot(ggredist$randmcnally)
plot(ggredist$natgeo)
plot(ggredist$coast)
plot(ggredist$larch)
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 |
reverse |
If |
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 |
min_coloring |
if |
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 |
Value
ggplot scale function
Examples
scale_fill_538()
scale_color_538()
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 |
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 |
Value
ggplot scale function
Examples
library(ggplot2)
data(oregon)
ggplot(oregon, aes(group = county, fill=county)) +
geom_district() +
scale_fill_dra() +
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 |
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 |
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()
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 |
midpoint |
Scale midpoint value. Default is |
limits |
Lower and upper limits for scale. Default is |
labels |
Function to adjust scale labels. Default is |
oob |
Function to deal with out of bounds. Default is |
reverse |
Whether to reverse red and blue. |
... |
Additional arguments to |
nice.breaks |
If |
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 |
data |
The data to be displayed in this layer |
geom |
The geometric object to use display the data |
position |
Position adjustment |
na.rm |
if |
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 |
... |
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()