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.

130 lines
3.6 KiB

10 years ago
---
6 years ago
output: rmarkdown::github_document
editor_options:
chunk_output_type: console
10 years ago
---
6 years ago
```{r echo=FALSE, message=FALSE, warning=FALSE, error=FALSE, include=FALSE}
knitr::opts_chunk$set(message=FALSE, warning=FALSE, fig.retina=2)
options(width=120)
```
# statebins
Create 'U.S.' Uniform Square State Cartogram Heatmaps
6 years ago
## What's in the tin?
10 years ago
The following functions are implemented:
- `statebins`: Creates "statebin" charts in the style of http://bit.ly/statebins
- `theme_statebins`: Base statebins theme
6 years ago
## Installation
10 years ago
```{r eval=FALSE}
devtools::install_github("hrbrmstr/statebins")
```
6 years ago
## Usage
10 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).
6 years ago
```{r}
10 years ago
library(statebins)
6 years ago
library(tidyverse)
10 years ago
# current verison
packageVersion("statebins")
10 years ago
# the original wapo data
adat <- suppressMessages(read_csv("http://www.washingtonpost.com/wp-srv/special/business/states-most-threatened-by-trade/states.csv?cache=1"))
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()
10 years ago
10 years ago
# continuous scale, legend on top
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")
10 years ago
10 years ago
# continuous scale, no legend
statebins(adat, value_col = "avgshare08_12", palette = "Purples") +
labs(x="2008-2010") +
theme_statebins(legend_position = "none")
10 years ago
# mortality data (has Puerto Rico)
9 years ago
# from: http://www.cdc.gov/nchs/fastats/state-and-territorial-data.htm
dat <- suppressMessages(read_csv("http://datadrivensecurity.info/data/deaths.csv"))
6 years ago
statebins(dat, value_col = "death_rate", name="Per 100K pop") +
labs(title="Mortality Rate (2010)") +
theme_statebins()
9 years ago
# fertility data
9 years ago
statebins(dat, value_col="fertility_rate", name="Per 100K pop", palette="PuBuGn") +
labs(title="Fertility Rate (2010)") +
theme_statebins()
9 years ago
10 years ago
# manual - perhaps good for elections?
election_2012 <- suppressMessages(read_csv("https://raw.githubusercontent.com/hrbrmstr/statebins/master/tmp/election2012.csv"))
10 years ago
mutate(election_2012, value = ifelse(is.na(Obama), "Romney", "Obama")) %>%
6 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()
```
### All the "states"
`statebins` now has PR, VI & NYC (by name or abbreviation) so you can use them, too:
```{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")
```