You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

258 lines
7.4 KiB

[![Project Status: Active – The project has reached a stable, usable
state and is being actively
4 years ago
developed.](https://www.repostatus.org/badges/latest/active.svg)](https://www.repostatus.org/#active)
[![Signed
by](https://img.shields.io/badge/Keybase-Verified-brightgreen.svg)](https://keybase.io/hrbrmstr)
![Signed commit
%](https://img.shields.io/badge/Signed_Commits-100%25-lightgrey.svg)
4 years ago
[![Linux build
Status](https://travis-ci.org/hrbrmstr/statebins.svg?branch=master)](https://travis-ci.org/hrbrmstr/statebins)
4 years ago
[![Windows build
status](https://ci.appveyor.com/api/projects/status/github/hrbrmstr/statebins?svg=true)](https://ci.appveyor.com/project/hrbrmstr/statebins)
[![Coverage
Status](https://codecov.io/gh/hrbrmstr/statebins/branch/master/graph/badge.svg)](https://codecov.io/gh/hrbrmstr/statebins)
[![cran
checks](https://cranchecks.info/badges/worst/statebins)](https://cranchecks.info/pkgs/statebins)
[![CRAN
status](https://www.r-pkg.org/badges/version/statebins)](https://www.r-pkg.org/pkg/statebins)
![Minimal R
Version](https://img.shields.io/badge/R%3E%3D-3.5.0-blue.svg)
4 years ago
![License](https://img.shields.io/badge/License-MIT-blue.svg)
9 years ago
5 years ago
# statebins
9 years ago
Create United States Uniform Cartogram Heatmaps
9 years ago
4 years ago
## 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/>).
4 years ago
“State bins” preserve as much of the geographic placement of the states
as possible but have the look and feel of a traditional heatmap.
4 years ago
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.
## What’s Inside The Tin
The following functions are implemented/datasets included:
4 years ago
- `geom_statebins`: A statebins Geom
- `state_tbl`: “State” abbreviation to name data frame
- `theme_statebins`: Base statebins theme
9 years ago
- `statebins`: (the original sole function in the package) Create a
new ggplot-based “statebin” chart for USA states/territories
5 years ago
## Installation
7 years ago
5 years ago
``` r
install.packages("statebins) # NOTE: CRAN version is 1.2.2
# or
install.packages("statebins", repos = c("https://cinc.rud.is", "https://cloud.r-project.org/"))
4 years ago
# or
remotes::install_git("https://git.rud.is/hrbrmstr/statebins.git")
# or
4 years ago
remotes::install_git("https://git.sr.ht/~hrbrmstr/statebins")
# or
4 years ago
remotes::install_gitlab("hrbrmstr/statebins")
# or
remotes::install_bitbucket("hrbrmstr/statebins")
# or
remotes::install_github("hrbrmstr/statebins")
9 years ago
```
4 years ago
NOTE: To use the ‘remotes’ install options you will need to have the
[{remotes} package](https://github.com/r-lib/remotes) installed.
5 years ago
5 years ago
## Usage
7 years ago
5 years ago
All of the following examples use the [WaPo
data](http://www.washingtonpost.com/wp-srv/special/business/states-most-threatened-by-trade/states.csv?cache=1).
It looks like the columns they use are scaled data and I didn’t take the
time to figure out what they did, so the final figure just mimics their
output (including the non-annotated legend).
7 years ago
5 years ago
``` r
9 years ago
library(statebins)
5 years ago
library(cdcfluview)
library(hrbrthemes)
5 years ago
library(tidyverse)
9 years ago
# current verison
packageVersion("statebins")
## [1] '1.4.0'
9 years ago
```
5 years ago
### The original wapo data
5 years ago
``` r
adat <- read_csv(system.file("extdata", "wapostates.csv", package="statebins"))
mutate(
adat,
share = cut(avgshare94_00, breaks = 4, labels = c("0-1", "1-2", "2-3", "3-4"))
) %>%
statebins(
value_col = "share",
ggplot2_scale_function = scale_fill_brewer,
name = "Share of workforce with jobs lost or threatened by trade"
) +
labs(title = "1994-2000") +
theme_statebins()
9 years ago
```
4 years ago
<img src="man/figures/README-original-1.png" width="672" />
9 years ago
5 years ago
### Continuous scale, legend on top
9 years ago
5 years ago
``` r
statebins(
adat,
value_col = "avgshare01_07",
name = "Share of workforce with jobs lost or threatened by trade",
palette = "OrRd",
direction = 1
) +
labs(x="2001-2007") +
theme_statebins(legend_position="top")
9 years ago
```
4 years ago
<img src="man/figures/README-continuous-1.png" width="672" />
9 years ago
5 years ago
### Continuous scale, no legend
9 years ago
5 years ago
``` r
statebins(adat, value_col = "avgshare08_12", palette = "Purples") +
labs(x="2008-2010") +
theme_statebins(legend_position = "none")
9 years ago
```
4 years ago
<img src="man/figures/README-continuous_noleg-1.png" width="672" />
5 years ago
### Mortality data (has Puerto Rico)
8 years ago
5 years ago
``` r
8 years ago
# from: http://www.cdc.gov/nchs/fastats/state-and-territorial-data.htm
dat <- read_csv(system.file("extdata", "deaths.csv", package="statebins"))
statebins(dat, value_col = "death_rate", name="Per 100K pop") +
labs(title="Mortality Rate (2010)") +
theme_statebins()
7 years ago
```
4 years ago
<img src="man/figures/README-mort-1.png" width="672" />
8 years ago
5 years ago
### Fertility data
8 years ago
5 years ago
``` r
statebins(dat, value_col="fertility_rate", name="Per 100K pop", palette="PuBuGn") +
labs(title="Fertility Rate (2010)") +
theme_statebins()
8 years ago
```
4 years ago
<img src="man/figures/README-fert-1.png" width="672" />
8 years ago
5 years ago
### Manual - perhaps good for elections?
9 years ago
5 years ago
``` r
5 years ago
election_2012 <- suppressMessages(read_csv(system.file("extdata", "election2012.csv", package="statebins")))
9 years ago
mutate(election_2012, value = ifelse(is.na(Obama), "Romney", "Obama")) %>%
5 years ago
statebins(
font_size=4, dark_label = "white", light_label = "white",
ggplot2_scale_function = scale_fill_manual,
name = "Winner",
values = c(Romney = "#2166ac", Obama = "#b2182b")
) +
theme_statebins()
```
4 years ago
<img src="man/figures/README-manual-1.png" width="672" />
5 years ago
5 years ago
### Rounded rects\!
You can pass in a `grid::units()` call for the `radius` parameter.
Slight curves:
5 years ago
``` r
data(USArrests)
USArrests$state <- rownames(USArrests)
statebins(USArrests, value_col="Assault", name = "Assault", round=TRUE) +
theme_statebins(legend_position="right")
```
4 years ago
<img src="man/figures/README-rounded-1.png" width="672" />
5 years ago
Circles\!
``` r
statebins(USArrests, value_col="Assault", name = "Assault", round=TRUE,
radius=grid::unit(16, "pt"), palette="Reds", direction=1) +
theme_statebins(legend_position="right")
```
4 years ago
<img src="man/figures/README-rounded2-1.png" width="672" />
5 years ago
### Geom
``` r
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))
```
<img src="man/figures/README-sb_facet-1.png" width="1920" />
5 years ago
### All the “states”
5 years ago
`statebins` now has PR, VI & NYC (by name or abbreviation) so you can
use them, too:
7 years ago
5 years ago
``` r
library(statebins)
library(tidyverse)
library(viridis)
data(USArrests)
# make up some data for the example
rownames_to_column(USArrests, "state") %>%
bind_rows(
data_frame(
state = c("Virgin Islands", "Puerto Rico", "New York City"),
Murder = rep(mean(max(USArrests$Murder),3)),
Assault = rep(mean(max(USArrests$Assault),3)),
Rape = rep(mean(max(USArrests$Rape),3)),
UrbanPop = c(93, 95, 100)
)
) -> us_arrests
statebins(us_arrests, value_col="Assault",
ggplot2_scale_function = viridis::scale_fill_viridis) +
labs(title="USArrests + made up data") +
theme_statebins("right")
7 years ago
```
9 years ago
4 years ago
<img src="man/figures/README-all-1.png" width="672" />