Package 'statebins'

Title: Create United States Uniform Cartogram Heatmaps
Description: The cartogram heatmaps generated by the included methods are an alternative to choropleth maps for the United States and are based on work by the Washington Post graphics department in their report on "The states most threatened by trade" (<http://www.washingtonpost.com/wp-srv/special/business/states-most-threatened-by-trade/>). "State bins" preserve as much of the geographic placement of the states as possible but have the look and feel of a traditional heatmap. Functions are provided that allow for use of a binned, discrete scale, a continuous scale or manually specified colors depending on what is needed for the underlying data.
Authors: Bob Rudis [aut, cre] , Harold Gil [ctb] (fix for show.guide), Brian Adams [ctb] (theme testing & feedback), Thomas Wood [ctb] (Significant suggestions & testing that made new features possible), Mathew Kiang [ctb] (Minor fix for NA handling)
Maintainer: Bob Rudis <[email protected]>
License: MIT + file LICENSE
Version: 1.4.0
Built: 2024-11-13 02:43:27 UTC
Source: https://gitlab.com/hrbrmstr/statebins

Help Index


Create United States Uniform Cartogram Heatmaps

Description

The cartogram heatmaps generated by the included methods are an alternative to choropleth maps for the United States and are based on work by the Washington Post graphics department in their report on "The states most threatened by trade" (http://www.washingtonpost.com/wp-srv/special/business/states-most-threatened-by-trade/). "State bins" preserve as much of the geographic placement of the states as possible but have the look and feel of a traditional heatmap. Functions are provided that allow for use of a binned, discrete scale, a continuous scale or manually specified colors depending on what is needed for the underlying data.

Author(s)

Bob Rudis ([email protected])


A statebins Geom

Description

Pass in a data frame of states and values and let this do the work. It enables easy faceting and makes it simpler to have a uniform legend across all the plots.

There are two special/critical aes() mappings:

  • state (so the geom knows which column to map the state names/abbrevs to)

  • fill (which column you're mapping the filling for the squares with)

Usage

geom_statebins(
  mapping = NULL,
  data = NULL,
  border_col = "white",
  border_size = 2,
  lbl_size = 3,
  dark_lbl = "black",
  light_lbl = "white",
  radius = grid::unit(6, "pt"),
  ...,
  na.rm = FALSE,
  show.legend = NA,
  inherit.aes = TRUE
)

GeomStatebins

Arguments

mapping

Set of aesthetic mappings created by aes() or aes_(). If specified and inherit.aes = TRUE (the default), it is combined with the default mapping at the top level of the plot. You must supply mapping if there is no plot mapping.

data

The data to be displayed in this layer. There are three options:

If NULL, the default, the data is inherited from the plot data as specified in the call to ggplot().

A data.frame, or other object, will override the plot data. All objects will be fortified to produce a data frame. See fortify() for which variables will be created.

A function will be called with a single argument, the plot data. The return value must be a data.frame., and will be used as the layer data.

border_col

border color of the state squares, default "white"

border_size

thickness of the square state borders

lbl_size

font size (relative) of the label text

dark_lbl, light_lbl

colrs to be uses when the label should be dark or light. The function automagically computes when this should be.

radius

the corner radius

...

other arguments passed on to layer(). These are often aesthetics, used to set an aesthetic to a fixed value, like color = "red" or size = 3. They may also be parameters to the paired geom/stat.

na.rm

If FALSE, the default, missing values are removed with a warning. If TRUE, missing values are silently removed.

show.legend

logical. Should this layer be included in the legends? NA, the default, includes if any aesthetics are mapped. FALSE never includes, and TRUE always includes. It can also be a named logical vector to finely select the aesthetics to display.

inherit.aes

If FALSE, overrides the default aesthetics, rather than combining with them. This is most useful for helper functions that define both data and aesthetics and shouldn't inherit behaviour from the default plot specification, e.g. borders().

Format

An object of class GeomStatebins (inherits from Geom, ggproto, gg) of length 7.

Examples

## Not run: 
library(statebins)
library(cdcfluview)
library(hrbrthemes)
library(tidyverse)

flu <- ili_weekly_activity_indicators(2017)

ggplot(flu, aes(state=statename, fill=activity_level)) +
  geom_statebins() +
  coord_equal() +
  viridis::scale_fill_viridis(
    name = "ILI Activity Level  ", limits=c(0,10), breaks=0:10, option = "magma", direction = -1
  ) +
  facet_wrap(~weekend) +
  labs(title="2017-18 Flu Season ILI Activity Level") +
  theme_statebins(base_family = font_ps) +
  theme(plot.title=element_text(size=16, hjust=0)) +
  theme(plot.margin = margin(30,30,30,30))

## End(Not run)

"State" abbreviation to name data frame

Description

"State" abbreviation to name data frame


Create a new ggplot-based "statebin" chart for USA states/territories

Description

Pass in a data frame and get back a square choropleth.

Usage

statebins(
  state_data,
  state_col = "state",
  value_col = "value",
  dark_label = "black",
  light_label = "white",
  na_label = "white",
  font_size = 3,
  state_border_col = "white",
  state_border_size = 2,
  round = FALSE,
  radius = grid::unit(6, "pt"),
  ggplot2_scale_function = ggplot2::scale_fill_distiller,
  ...
)

Arguments

state_data

data frame of states and values to plot

state_col

column name in state_data that has the states. no duplicates and can be names (e.g. "Maine") or abbreviatons (e.g. "ME")

value_col

column name in state_data that holds the values to be plotted

dark_label, light_label, na_label

dark/light/NA label colors. The specified color will be used when the algorithm determines labels should be inverted.

font_size

font size (default = 3)

state_border_col

default "white" - this creates the "spaces" between boxes

state_border_size

border size

round

rounded corners (default: FALSE)

radius

if round is TRUE then use grid::unit to specify the corner radius. Default is grid::unit(6, "pt") if using rounded corners.

ggplot2_scale_function

ggplot2 scale function to use. Defaults to scale_fill_distiller since you're likely passing in continuous data when you shouldn't be :-)

...

additional parameters to the scale function

Details

The state_col and value_col parameters default to state and value. That means if you name the columns you want to plot with those names, you can forego passing them in. Othersise, use "strings".

A handy feature of this function is that you can specify a dark_label color and a light_label color. What does that mean? Well, you also pass in the color scale function you're going to use and statebins will apply it and use that information to determine what the tile color is and — if it's "dark" it will use the light_label and if it's "light" it will use the dark_label color. That means the labels will never blend in to the background (as long as you specify decent label colors).

You can customize the scale function you pass in by using name parameters. All named parameters not used by statebins() itself get passed to the scale function.

Value

ggplot2 object

Examples

data(USArrests)

USArrests$state <- rownames(USArrests)
statebins(USArrests, value_col="Assault", name = "Assault") +
  theme_statebins(legend_position="right")

Base statebins theme

Description

Clears out most of the cruft. Builds off of theme_bw()

Usage

theme_statebins(legend_position = "bottom", base_size = 11, base_family = "")

Arguments

legend_position

fills in legend.position

base_family, base_size

same as theme_bw()