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.
boB Rudis 046467b1ec
Merge pull request #20 from delabj/patch-1
4 years ago
.github Create FUNDING.yml 5 years ago
R Remove extra tile_data$size <- 2 4 years ago
inst/extdata lost data files 6 years ago
man README 5 years ago
tests Print NA states 6 years ago
.Rbuildignore Code coverage + appveyor + better theme 6 years ago
.gitignore initial commit 10 years ago
.travis.yml Code coverage + appveyor + better theme 6 years ago
DESCRIPTION README 5 years ago
LICENSE 1.2.2 CRAN 8 years ago
NAMESPACE shld pass CRAN checks on Travis now 6 years ago
NEWS.md geom_statebins() 6 years ago
README.Rmd readme 5 years ago
README.md README 5 years ago
appveyor.yml Code coverage + appveyor + better theme 6 years ago
codecov.yml Code coverage + appveyor + better theme 6 years ago
cran-comments.md CRAN fixes 8 years ago
statebins.Rproj Submitted to CRAN 10 years ago

README.md

Project Status: Active – The project has reached a stable, usablestate and is being activelydeveloped. Signedby Signed commit% Linux buildStatus Windows buildstatus CoverageStatus cranchecks CRANstatus Minimal RVersion License

statebins

Create ‘U.S.’ Uniform Square State Cartogram Heatmaps

Description

Cartogram heatmaps are an alternative to choropleth maps for ‘USA’ 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 has 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.

What’s Inside The Tin

The following functions are implemented:

  • geom_statebins: A statebins Geom
  • theme_statebins: Base statebins theme

Installation

install.packages("statebins", repos = "https://cinc.rud.is")
# or
remotes::install_git("https://git.rud.is/hrbrmstr/statebins.git")
# or
remotes::install_git("https://git.sr.ht/~hrbrmstr/statebins")
# or
remotes::install_gitlab("hrbrmstr/statebins")
# or
remotes::install_bitbucket("hrbrmstr/statebins")
# or
remotes::install_github("hrbrmstr/statebins")

NOTE: To use the ‘remotes’ install options you will need to have the {remotes} package installed.

Usage

All of the following examples use the WaPo data. 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).

library(statebins)
library(cdcfluview)
library(hrbrthemes)
library(tidyverse)

# current verison
packageVersion("statebins")
## [1] '2.0.0'

The original wapo data

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()

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")

Continuous scale, no legend

statebins(adat, value_col = "avgshare08_12", palette = "Purples") +
  labs(x="2008-2010") +
  theme_statebins(legend_position = "none") 

Mortality data (has Puerto Rico)

# 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()

Fertility data

statebins(dat, value_col="fertility_rate", name="Per 100K pop", palette="PuBuGn") +
  labs(title="Fertility Rate (2010)") +
  theme_statebins()

Manual - perhaps good for elections?

election_2012 <- suppressMessages(read_csv(system.file("extdata", "election2012.csv", package="statebins")))

mutate(election_2012, value = ifelse(is.na(Obama), "Romney", "Obama")) %>% 
  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()

Rounded rects!

You can pass in a grid::units() call for the radius parameter.

Slight curves:

data(USArrests)

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

Circles!

statebins(USArrests, value_col="Assault", name = "Assault", round=TRUE, 
          radius=grid::unit(16, "pt"), palette="Reds", direction=1) +
  theme_statebins(legend_position="right")

Geom

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))

All the “states”

statebins now has PR, VI & NYC (by name or abbreviation) so you can use them, too:

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")