Browse Source

initial commit

master
Bob Rudis 8 years ago
commit
c547664014
No known key found for this signature in database GPG Key ID: 1D7529BE14E2BBA9
  1. 7
      .Rbuildignore
  2. 7
      .gitignore
  3. 23
      .travis.yml
  4. 20
      DESCRIPTION
  5. 2
      LICENSE
  6. 8
      NAMESPACE
  7. 6
      R/statebins-package.R
  8. 143
      R/statebins.R
  9. 75
      README.md
  10. 67
      _README.Rmd
  11. BIN
      _README_files/figure-markdown_github/unnamed-chunk-31.png
  12. BIN
      _README_files/figure-markdown_github/unnamed-chunk-32.png
  13. BIN
      data/states.rda
  14. 21
      man/statebins.Rd
  15. 14
      man/statebins_continuous.Rd
  16. 21
      statebins.Rproj
  17. 2
      tests/test-all.R
  18. 6
      tests/testthat/test-statebins.R

7
.Rbuildignore

@ -0,0 +1,7 @@
^.*\.Rproj$
^\.Rproj\.user$
^_README.Rmd$
^NEWS.md$
^\.travis\.yml$
^README.md$
^_README_files/*

7
.gitignore

@ -0,0 +1,7 @@
.Rproj.user
.Rhistory
.RData
.Rproj
src/*.o
src/*.so
src/*.dll

23
.travis.yml

@ -0,0 +1,23 @@
language: c
before_install:
- curl -OL http://raw.github.com/craigcitro/r-travis/master/scripts/travis-tool.sh
- chmod 755 ./travis-tool.sh
- ./travis-tool.sh bootstrap
install:
- ./travis-tool.sh install_github plyr
- ./travis-tool.sh install_deps
script: ./travis-tool.sh run_tests
on_failure:
- ./travis-tool.sh dump_logs
branches:
except:
- /-expt$/
notifications:
email:
on_success: change
on_failure: change

20
DESCRIPTION

@ -0,0 +1,20 @@
Package: statebins
Type: Package
Title: statebins is an alternative to choropleth maps for US States
Version: 1.0
Date: 2014-08-25
Author: Bob Rudis (@hrbrmstr)
Maintainer: Bob Rudis <bob@rudis.net>
Description: statebins is an alternative to choropleth maps for US States
URL: http://github.com/hrbrmstr/statebins
BugReports: https://github.com/hrbrmstr/statebins/issues
License: MIT + file LICENSE
LazyData: yes
Suggests:
testthat
Depends:
R (>= 3.0.0),
ggplot2,
grid,
scales,
gridExtra

2
LICENSE

@ -0,0 +1,2 @@
YEAR: 2014
COPYRIGHT HOLDER: Bob Rudis

8
NAMESPACE

@ -0,0 +1,8 @@
# Generated by roxygen2 (4.0.1.99): do not edit by hand
export(statebins)
export(statebins_continuous)
import(ggplot2)
import(grid)
import(gridExtra)
import(scales)

6
R/statebins-package.R

@ -0,0 +1,6 @@
#' statebins is an alternative to choropleth maps for US States
#' @name statebins
#' @docType package
#' @author Bob Rudis (@@hrbrmstr)
#' @import ggplot2 scales grid gridExtra
NULL

143
R/statebins.R

@ -0,0 +1,143 @@
#' Create ggplot-based "statebin" charts in the style of \url{http://www.washingtonpost.com/wp-srv/special/business/states-most-threatened-by-trade/}
#'
#' This version uses a discrete scale, binned by the "breaks" parameter
#' @param state_data
#' @param state_col
#' @param value_col
#' @param text_color
#' @param state_border_col
#' @param breaks
#' @param labels
#' @param legend_title
#' @param legend_position
#' @param brewer_pal
#' @param plot_title
#' @param title_position
#' @return ggplot2 object or grob
#' @export
statebins <- function(state_data, state_col="state", value_col="value",
text_color="white",
state_border_col="white", breaks=5, labels=1:4,
legend_title="Legend", legend_position="top",
brewer_pal="PuBu", plot_title="", title_position="bottom") {
stopifnot(breaks > 0 && breaks < 10)
stopifnot(title_position %in% c("", "top", "bottom"))
stopifnot(legend_position %in% c("", "none", "top", "bottom"))
if (max(nchar(state_data[,state_col])) == 2) {
merge.x <- "abbrev"
} else {
merge.x <- "state"
}
stopifnot(state_data[,state_col] %in% state_coords[,merge.x])
stopifnot(!any(duplicated(state_data[,state_col])))
st.dat <- merge(state_coords, state_data, by.x=merge.x, by.y=state_col)
st.dat$fill_color <- cut(st.dat[, value_col], breaks=breaks, labels=labels)
gg <- ggplot(st.dat, aes(x=col, y=row, label=abbrev))
gg <- gg + geom_tile(aes(fill=fill_color))
gg <- gg + geom_tile(color=state_border_col, aes(fill=fill_color), size=3, show_guide=FALSE)
gg <- gg + geom_text(color=text_color)
gg <- gg + scale_y_reverse()
gg <- gg + scale_fill_brewer(palette=brewer_pal, name=legend_title)
gg <- gg + coord_equal()
gg <- gg + labs(x="", y="", title="")
gg <- gg + theme_bw()
gg <- gg + theme(legend.position=legend_position)
gg <- gg + theme(plot.margin=unit(c(0,0,0,0), "lines"))
gg <- gg + theme(panel.margin=unit(0, "lines"))
gg <- gg + theme(panel.border=element_blank())
gg <- gg + theme(panel.grid=element_blank())
gg <- gg + theme(panel.background=element_blank())
gg <- gg + theme(axis.ticks=element_blank())
gg <- gg + theme(axis.text=element_blank())
if (plot_title != "") {
if (title_position == "bottom") {
gg <- arrangeGrob(gg, sub=textGrob(plot_title, gp=gpar(cex=1)))
} else {
gg <- gg + ggtitle(plot_title)
}
}
return(gg)
}
#' Create ggplot-based "statebin" charts in the style of \url{http://www.washingtonpost.com/wp-srv/special/business/states-most-threatened-by-trade/}
#'
#' This version uses a discrete scale, binned by the "breaks" parameter
#' @param state_data
#' @param state_col
#' @param value_col
#' @param text_color
#' @param state_border_col
#' @param legend_title
#' @param legend_position
#' @param brewer_pal
#' @param plot_title
#' @param title_position
#' @return ggplot2 object or grob
#' @export
statebins_continuous <- function(state_data, state_col="state", value_col="value",
text_color="white",
state_border_col="white",
legend_title="Legend", legend_position="top",
brewer_pal="PuBu", plot_title="", title_position="bottom") {
stopifnot(title_position %in% c("", "top", "bottom"))
stopifnot(legend_position %in% c("", "none", "top", "bottom"))
if (max(nchar(state_data[,state_col])) == 2) {
merge.x <- "abbrev"
} else {
merge.x <- "state"
}
stopifnot(state_data[,state_col] %in% state_coords[,merge.x])
stopifnot(!any(duplicated(state_data[,state_col])))
st.dat <- merge(state_coords, state_data, by.x=merge.x, by.y=state_col)
gg <- ggplot(st.dat, aes(x=col, y=row, label=abbrev))
gg <- gg + geom_tile(aes_string(fill=value_col))
gg <- gg + geom_tile(color=state_border_col,
aes_string(fill=value_col), size=3, show_guide=FALSE)
gg <- gg + geom_text(color=text_color)
gg <- gg + scale_y_reverse()
gg <- gg + continuous_scale("fill", "distiller",
gradient_n_pal(brewer_pal(type, brewer_pal)(6), NULL, "Lab"), na.value = "grey50", name=legend_title)
gg <- gg + coord_equal()
gg <- gg + labs(x="", y="", title="")
gg <- gg + theme_bw()
gg <- gg + theme(legend.position=legend_position)
gg <- gg + theme(plot.margin=unit(c(0,0,0,0), "lines"))
gg <- gg + theme(panel.margin=unit(0, "lines"))
gg <- gg + theme(panel.border=element_blank())
gg <- gg + theme(panel.grid=element_blank())
gg <- gg + theme(panel.background=element_blank())
gg <- gg + theme(axis.ticks=element_blank())
gg <- gg + theme(axis.text=element_blank())
if (plot_title != "") {
if (title_position == "bottom") {
gg <- arrangeGrob(gg, sub=textGrob(plot_title, gp=gpar(cex=1)))
} else {
gg <- gg + ggtitle(plot_title)
}
}
return(gg)
}

75
README.md

@ -0,0 +1,75 @@
statesquares is ...
The following functions are implemented:
- `statebins` -
### News
- Version `1.0.0` released
### Installation
``` {.r}
devtools::install_github("hrbrmstr/statebins")
```
### Usage
``` {.r}
library(statebins)
```
## Loading required package: ggplot2
## Loading required package: grid
## Loading required package: scales
## Loading required package: gridExtra
``` {.r}
# current verison
packageVersion("statebins")
```
## [1] '1.0'
``` {.r}
dat <- read.csv("http://www.washingtonpost.com/wp-srv/special/business/states-most-threatened-by-trade/states.csv?cache=1", stringsAsFactors=FALSE)
gg <- statebins(dat, "state", "avgshare94_00", breaks=4,
labels=c("0-1", "1-2", "2-3", "3-4"),
legend_title="State Groups",
brewer_pal="Blues", text_color="black",
plot_title="1994-2000", title_position="bottom")
gg
```
![plot of chunk unnamed-chunk-3](./_README_files/figure-markdown_github/unnamed-chunk-31.png)
``` {.r}
gg2 <- statebins_continuous(dat, "state", "avgshare01_07",
legend_title="States", legend_position="none",
brewer_pal="OrRd", text_color="black",
plot_title="1994-2000", title_position="bottom")
gg2
```
![plot of chunk unnamed-chunk-3](./_README_files/figure-markdown_github/unnamed-chunk-32.png)
### Test Results
``` {.r}
library(statebins)
library(testthat)
date()
```
## [1] "Mon Aug 25 22:19:55 2014"
``` {.r}
test_dir("tests/")
```
## basic functionality :

67
_README.Rmd

@ -0,0 +1,67 @@
---
title: "README"
author: "Bob Rudis"
date: August 25, 2014
output:
md_document:
variant: markdown_github
---
statesquares is ...
The following functions are implemented:
- `statebins` -
### News
- Version `1.0.0` released
### Installation
```{r eval=FALSE}
devtools::install_github("hrbrmstr/statebins")
```
```{r echo=FALSE, message=FALSE, warning=FALSE, error=FALSE}
options(width=120)
```
### Usage
```{r}
library(statebins)
# current verison
packageVersion("statebins")
dat <- read.csv("http://www.washingtonpost.com/wp-srv/special/business/states-most-threatened-by-trade/states.csv?cache=1", stringsAsFactors=FALSE)
gg <- statebins(dat, "state", "avgshare94_00", breaks=4,
labels=c("0-1", "1-2", "2-3", "3-4"),
legend_title="State Groups",
brewer_pal="Blues", text_color="black",
plot_title="1994-2000", title_position="bottom")
gg
gg2 <- statebins_continuous(dat, "state", "avgshare01_07",
legend_title="States", legend_position="none",
brewer_pal="OrRd", text_color="black",
plot_title="1994-2000", title_position="bottom")
gg2
```
### Test Results
```{r}
library(statebins)
library(testthat)
date()
test_dir("tests/")
```

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

Binary file not shown.

After

Width:  |  Height:  |  Size: 44 KiB

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

Binary file not shown.

After

Width:  |  Height:  |  Size: 43 KiB

BIN
data/states.rda

Binary file not shown.

21
man/statebins.Rd

@ -0,0 +1,21 @@
% Generated by roxygen2 (4.0.1.99): do not edit by hand
\docType{package}
\name{statebins}
\alias{statebins}
\alias{statebins-package}
\title{A package to ...}
\usage{
statebins(state_data, state_col = "state", value_col = "value",
text_color = "white", state_border_col = "white", breaks = 5,
labels = 1:4, legend_title = "Legend", legend_position = "top",
brewer_pal = "PuBu", plot_title = "", title_position = "bottom")
}
\description{
A package to ...
This version uses a discrete scale, binned by the "breaks" parameter
}
\author{
Bob Rudis (@hrbrmstr)
}

14
man/statebins_continuous.Rd

@ -0,0 +1,14 @@
% Generated by roxygen2 (4.0.1.99): do not edit by hand
\name{statebins_continuous}
\alias{statebins_continuous}
\title{Create ggplot-based "statebin" charts in the style of \url{http://www.washingtonpost.com/wp-srv/special/business/states-most-threatened-by-trade/}}
\usage{
statebins_continuous(state_data, state_col = "state", value_col = "value",
text_color = "white", state_border_col = "white",
legend_title = "Legend", legend_position = "top", brewer_pal = "PuBu",
plot_title = "", title_position = "bottom")
}
\description{
This version uses a discrete scale, binned by the "breaks" parameter
}

21
statebins.Rproj

@ -0,0 +1,21 @@
Version: 1.0
RestoreWorkspace: Default
SaveWorkspace: Default
AlwaysSaveHistory: Default
EnableCodeIndexing: Yes
UseSpacesForTab: Yes
NumSpacesForTab: 2
Encoding: UTF-8
RnwWeave: Sweave
LaTeX: pdfLaTeX
StripTrailingWhitespace: Yes
BuildType: Package
PackageUseDevtools: Yes
PackageInstallArgs: --no-multiarch --with-keep.source
PackageBuildArgs: --resave-data
PackageRoxygenize: rd,collate,namespace

2
tests/test-all.R

@ -0,0 +1,2 @@
library(testthat)
test_check("statebins")

6
tests/testthat/test-statebins.R

@ -0,0 +1,6 @@
context("basic functionality")
test_that("we can do something", {
#expect_that(some_function(), is_a("data.frame"))
})
Loading…
Cancel
Save