commit adf7588be786ddb22471c9995fd9ae2d50a481c3 Author: Bob Rudis Date: Mon Aug 25 22:20:55 2014 -0400 initial commit diff --git a/.Rbuildignore b/.Rbuildignore new file mode 100644 index 0000000..71a8201 --- /dev/null +++ b/.Rbuildignore @@ -0,0 +1,7 @@ +^.*\.Rproj$ +^\.Rproj\.user$ +^_README.Rmd$ +^NEWS.md$ +^\.travis\.yml$ +^README.md$ +^_README_files/* \ No newline at end of file diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..60332de --- /dev/null +++ b/.gitignore @@ -0,0 +1,7 @@ +.Rproj.user +.Rhistory +.RData +.Rproj +src/*.o +src/*.so +src/*.dll diff --git a/.travis.yml b/.travis.yml new file mode 100644 index 0000000..cf8c99a --- /dev/null +++ b/.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 diff --git a/DESCRIPTION b/DESCRIPTION new file mode 100644 index 0000000..8398d92 --- /dev/null +++ b/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 +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 diff --git a/LICENSE b/LICENSE new file mode 100644 index 0000000..7f50115 --- /dev/null +++ b/LICENSE @@ -0,0 +1,2 @@ +YEAR: 2014 +COPYRIGHT HOLDER: Bob Rudis diff --git a/NAMESPACE b/NAMESPACE new file mode 100644 index 0000000..55687f0 --- /dev/null +++ b/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) diff --git a/R/statebins-package.R b/R/statebins-package.R new file mode 100644 index 0000000..52229fe --- /dev/null +++ b/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 diff --git a/R/statebins.R b/R/statebins.R new file mode 100644 index 0000000..de10a48 --- /dev/null +++ b/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) + +} \ No newline at end of file diff --git a/README.md b/README.md new file mode 100644 index 0000000..19fee03 --- /dev/null +++ b/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 : diff --git a/_README.Rmd b/_README.Rmd new file mode 100644 index 0000000..4eb2e32 --- /dev/null +++ b/_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/") +``` + diff --git a/_README_files/figure-markdown_github/unnamed-chunk-31.png b/_README_files/figure-markdown_github/unnamed-chunk-31.png new file mode 100644 index 0000000..dba2571 Binary files /dev/null and b/_README_files/figure-markdown_github/unnamed-chunk-31.png differ diff --git a/_README_files/figure-markdown_github/unnamed-chunk-32.png b/_README_files/figure-markdown_github/unnamed-chunk-32.png new file mode 100644 index 0000000..f4314d9 Binary files /dev/null and b/_README_files/figure-markdown_github/unnamed-chunk-32.png differ diff --git a/data/states.rda b/data/states.rda new file mode 100644 index 0000000..9bf9366 Binary files /dev/null and b/data/states.rda differ diff --git a/man/statebins.Rd b/man/statebins.Rd new file mode 100644 index 0000000..4515080 --- /dev/null +++ b/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) +} + diff --git a/man/statebins_continuous.Rd b/man/statebins_continuous.Rd new file mode 100644 index 0000000..e4ed6e6 --- /dev/null +++ b/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 +} + diff --git a/statebins.Rproj b/statebins.Rproj new file mode 100644 index 0000000..446d9e1 --- /dev/null +++ b/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 diff --git a/tests/test-all.R b/tests/test-all.R new file mode 100644 index 0000000..5412d76 --- /dev/null +++ b/tests/test-all.R @@ -0,0 +1,2 @@ +library(testthat) +test_check("statebins") diff --git a/tests/testthat/test-statebins.R b/tests/testthat/test-statebins.R new file mode 100644 index 0000000..ab6f62f --- /dev/null +++ b/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")) + +})