statebins_manual: Create a new ggplot-based "statebin" chart for USA states...

Description Usage Arguments Details Value Examples

View source: R/statebins.R

Description

statebins() creates "statebin" charts in the style of http://bit.ly/statebins

Usage

1
2
3
4
statebins_manual(state_data, state_col = "state", color_col = "color",
  text_color = "black", font_size = 3, state_border_col = "white",
  labels = NULL, legend_title = "Legend", legend_position = "top",
  plot_title = "", title_position = "bottom")

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

color_col

column name in state_data that holds the colors to be used

text_color

default "black"

font_size

font size (default = 3)

state_border_col

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

labels

labels for the legend (should be the same number as distinct colors in color_col); NULL == no labels/legend

legend_title

title for the legend

legend_position

"none", "top", "left", "right" or "bottom" (defaults to "top")

plot_title

title for the plot

title_position

where to put the title ("bottom" or "top" or "" for none); if "bottom", you get back a grob vs a ggplot object

Details

This version uses manual colors (i.e. pass in a column that defines the color per-state)

The function minimally expects the caller to pass in a data frame that:

Doing so will create a "statebin" chart with the colors specified as a ggplot2 object.

You can use a different column for the state names and colors by changing state_col and color_col accordingly.

To add a title, change plot_title to anything but an empty atomic string vector (i.e. "") and set title_position to "top" or "bottom". Choosing "bottom" will cause statebins to use arrangeGrob to position the title via sub and return a frame grob instead of a ggplot2 object.

Value

ggplot2 object or grob

Examples

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
## Not run: 
library(httr)
library(dplyr)
election_2012 <-
  GET("https://raw.githubusercontent.com/hrbrmstr/statebins/master/tmp/election2012.csv")
results <- read.csv(textConnection(content(election_2012, as="text")),
                    header=TRUE, stringsAsFactors=FALSE)
results <- results %>%
  mutate(color=ifelse(is.na(Obama), "#2166ac", "#b2182b")) %>%
  select(state, color)
results %>%
  statebins_manual(font_size=4,
      text_color = "white", labels=c("Romney", "Obama"),
      legend_position="right", legend_title="Winner")

## End(Not run)

statebins documentation built on May 30, 2017, 2:25 a.m.