Browse Source

statebins_manual

master
hrbrmstr 10 years ago
parent
commit
3cac431a37
No known key found for this signature in database GPG Key ID: 1D7529BE14E2BBA9
  1. 11
      DESCRIPTION
  2. 22
      R/statebins.R
  3. 15
      README.Rmd
  4. 19
      README.md
  5. BIN
      README_files/figure-markdown_github/unnamed-chunk-31.png
  6. BIN
      README_files/figure-markdown_github/unnamed-chunk-32.png
  7. BIN
      README_files/figure-markdown_github/unnamed-chunk-33.png
  8. BIN
      README_files/figure-markdown_github/unnamed-chunk-34.png
  9. 7
      man/statebins_manual.Rd

11
DESCRIPTION

@ -1,8 +1,9 @@
Package: statebins Package: statebins
Type: Package Type: Package
Title: statebins is an alternative to choropleth maps for USA States Title: statebins - U.S. State Cartogram Heatmaps in R; an alternative to
Version: 1.0 choropleth maps for USA States
Date: 2014-08-26 Version: 1.1
Date: 2014-08-29
Author: Bob Rudis (@hrbrmstr) Author: Bob Rudis (@hrbrmstr)
Maintainer: Bob Rudis <bob@rudis.net> Maintainer: Bob Rudis <bob@rudis.net>
Description: statebins is an alternative to choropleth maps for USA States and Description: statebins is an alternative to choropleth maps for USA States and
@ -10,8 +11,8 @@ Description: statebins is an alternative to choropleth maps for USA States and
on "The states most threatened by trade". statebins preserves as much of on "The states most threatened by trade". statebins preserves as much of
the geographic placement of the states as possible but has the look and 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 feel of a traditional heatmap. Functions are provided that allow for use
of both a binned, discrete scale and continuous scale depending on what of a binned, discrete scale, a continuous scale or manually specified
is needed for the underlying data. colors depending on what is needed for the underlying data.
URL: http://github.com/hrbrmstr/statebins URL: http://github.com/hrbrmstr/statebins
BugReports: https://github.com/hrbrmstr/statebins/issues BugReports: https://github.com/hrbrmstr/statebins/issues
License: MIT + file LICENSE License: MIT + file LICENSE

22
R/statebins.R

@ -271,22 +271,12 @@ statebins_continuous <- function(state_data, state_col="state", value_col="value
#' @export #' @export
#' @examples #' @examples
#' \dontrun{ #' \dontrun{
#' election_2012 <- structure(list(state = c("AL", "AK", "AZ", "AR", "CA", "CO", #' library(httr)
#' "CT", "DE", "DC", "FL", "GA", "HI", "ID", "IL", "IN", "IA", "KS", #' library(dplyr)
#' "KY", "LA", "ME", "MD", "MA", "MI", "MN", "MS", "MO", "MT", "NE", #' election_2012 <- GET("https://raw.githubusercontent.com/hrbrmstr/statebins/master/tmp/election2012.csv")
#' "NV", "NH", "NJ", "NM", "NY", "NC", "ND", "OH", "OK", "OR", "PA", #' results <- read.csv(textConnection(content(election_2012, as="text")), header=TRUE, stringsAsFactors=FALSE)
#' "RI", "SC", "SD", "TN", "TX", "UT", "VT", "VA", "WA", "WV", "WI", #' results <- results %>% mutate(color=ifelse(is.na(Obama), "#2166ac", "#b2182b")) %>% select(state, color)
#' "WY"), color = c("#2166ac", "#2166ac", "#2166ac", "#2166ac", #' results %>% statebins_manual(font_size=4, text_color = "white", labels=c("Romney", "Obama"), legend_position="right", legend_title="Winner")
#' "#b2182b", "#b2182b", "#b2182b", "#b2182b", "#b2182b", "#b2182b",
#' "#2166ac", "#b2182b", "#2166ac", "#b2182b", "#2166ac", "#b2182b",
#' "#2166ac", "#2166ac", "#2166ac", "#b2182b", "#b2182b", "#b2182b",
#' "#b2182b", "#b2182b", "#2166ac", "#2166ac", "#2166ac", "#2166ac",
#' "#b2182b", "#b2182b", "#b2182b", "#b2182b", "#b2182b", "#2166ac",
#' "#2166ac", "#b2182b", "#2166ac", "#b2182b", "#b2182b", "#b2182b",
#' "#2166ac", "#2166ac", "#2166ac", "#2166ac", "#2166ac", "#b2182b",
#' "#b2182b", "#b2182b", "#2166ac", "#b2182b", "#2166ac")), class = "data.frame", row.names = c(NA,
#' -51L), .Names = c("state", "color"))
#' statebins_manual(election_2012, font_size=4, text_color = "white", labels=c("Romney", "Obama", "Hrm?"), legend_position="right", legend_title="Winner")
#' } #' }
statebins_manual <- function(state_data, state_col="state", color_col="color", statebins_manual <- function(state_data, state_col="state", color_col="color",
text_color="black", font_size=3, text_color="black", font_size=3,

15
README.Rmd

@ -1,19 +1,20 @@
--- ---
title: "README" title: "README"
author: "Bob Rudis" author: "Bob Rudis"
date: August 26, 2014 date: August 29, 2014
output: output:
md_document: md_document:
variant: markdown_github variant: markdown_github
--- ---
<!-- output: html_document --> <!-- output: html_document -->
statebins is an alternative to choropleth maps for USA States statebins - U.S. State Cartogram Heatmaps in R; an alternative to choropleth maps for USA States
The following functions are implemented: The following functions are implemented:
- `statebins` - creates "statebin" charts in the style of http://bit.ly/statebins - This version uses discrete `RColorBrewer` scales, binned by the "breaks" parameter. - `statebins` - creates "statebin" charts in the style of http://bit.ly/statebins - This version uses discrete `RColorBrewer` scales, binned by the "breaks" parameter.
- `statebins_continuous` - creates "statebin" charts in the style of http://bit.ly/statebins - This version uses a continuous scale based on `RColorBrewer` scales (passing in a 6 element `RColorBrewer` palette to `scale_fill_gradientn`). - `statebins_continuous` - creates "statebin" charts in the style of http://bit.ly/statebins - This version uses a continuous scale based on `RColorBrewer` scales (passing in a 6 element `RColorBrewer` palette to `scale_fill_gradientn`).
- `statebins_manual` - creates "statebin" charts using manually specified colors in a column
### TODO ### TODO
@ -22,6 +23,7 @@ The following functions are implemented:
### News ### News
- Version `1.1.0` released - `statebins_manual()` for manual placement of colors and moving of AK in support of a [pull request](https://github.com/hrbrmstr/statebins/pull/1) by [hansthompson](https://github.com/hansthompson)
- Version `1.0.0` released - Version `1.0.0` released
### Installation ### Installation
@ -74,6 +76,15 @@ gg3 <- statebins_continuous(dat, "state", "avgshare08_12",
gg3 gg3
# manual - perhaps good for elections?
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")
# or, more like the one in the WaPo article; i might be picking the wrong columns here. it's just for an example # or, more like the one in the WaPo article; i might be picking the wrong columns here. it's just for an example
sb <- function(col, title) { sb <- function(col, title) {

19
README.md

@ -1,11 +1,12 @@
<!-- output: html_document --> <!-- output: html_document -->
statebins is an alternative to choropleth maps for USA States statebins - U.S. State Cartogram Heatmaps in R; an alternative to choropleth maps for USA States
The following functions are implemented: The following functions are implemented:
- `statebins` - creates "statebin" charts in the style of <http://bit.ly/statebins> - This version uses discrete `RColorBrewer` scales, binned by the "breaks" parameter. - `statebins` - creates "statebin" charts in the style of <http://bit.ly/statebins> - This version uses discrete `RColorBrewer` scales, binned by the "breaks" parameter.
- `statebins_continuous` - creates "statebin" charts in the style of <http://bit.ly/statebins> - This version uses a continuous scale based on `RColorBrewer` scales (passing in a 6 element `RColorBrewer` palette to `scale_fill_gradientn`). - `statebins_continuous` - creates "statebin" charts in the style of <http://bit.ly/statebins> - This version uses a continuous scale based on `RColorBrewer` scales (passing in a 6 element `RColorBrewer` palette to `scale_fill_gradientn`).
- `statebins_manual` - creates "statebin" charts using manually specified colors in a column
### TODO ### TODO
@ -14,6 +15,7 @@ The following functions are implemented:
### News ### News
- Version `1.1.0` released - `statebins_manual()` for manual placement of colors and moving of AK in support of a [pull request](https://github.com/hrbrmstr/statebins/pull/1) by [hansthompson](https://github.com/hansthompson)
- Version `1.0.0` released - Version `1.0.0` released
### Installation ### Installation
@ -78,6 +80,19 @@ gg3
![plot of chunk unnamed-chunk-3](./README_files/figure-markdown_github/unnamed-chunk-33.png) ![plot of chunk unnamed-chunk-3](./README_files/figure-markdown_github/unnamed-chunk-33.png)
``` {.r} ``` {.r}
# manual - perhaps good for elections?
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")
```
![plot of chunk unnamed-chunk-3](./README_files/figure-markdown_github/unnamed-chunk-34.png)
``` {.r}
# or, more like the one in the WaPo article; i might be picking the wrong columns here. it's just for an example # or, more like the one in the WaPo article; i might be picking the wrong columns here. it's just for an example
sb <- function(col, title) { sb <- function(col, title) {
@ -175,7 +190,7 @@ library(testthat)
date() date()
``` ```
## [1] "Tue Aug 26 17:53:56 2014" ## [1] "Fri Aug 29 12:38:53 2014"
``` {.r} ``` {.r}
test_dir("tests/") test_dir("tests/")

BIN
README_files/figure-markdown_github/unnamed-chunk-31.png

Binary file not shown.

Before

Width:  |  Height:  |  Size: 34 KiB

After

Width:  |  Height:  |  Size: 34 KiB

BIN
README_files/figure-markdown_github/unnamed-chunk-32.png

Binary file not shown.

Before

Width:  |  Height:  |  Size: 36 KiB

After

Width:  |  Height:  |  Size: 36 KiB

BIN
README_files/figure-markdown_github/unnamed-chunk-33.png

Binary file not shown.

Before

Width:  |  Height:  |  Size: 30 KiB

After

Width:  |  Height:  |  Size: 30 KiB

BIN
README_files/figure-markdown_github/unnamed-chunk-34.png

Binary file not shown.

After

Width:  |  Height:  |  Size: 36 KiB

7
man/statebins_manual.Rd

@ -59,7 +59,12 @@ return a frame grob instead of a ggplot2 object.
} }
\examples{ \examples{
\dontrun{ \dontrun{
statebins_manual(U) 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")
} }
} }

Loading…
Cancel
Save