diff --git a/.Rbuildignore b/.Rbuildignore index 71a8201..ea5ba98 100644 --- a/.Rbuildignore +++ b/.Rbuildignore @@ -1,6 +1,7 @@ ^.*\.Rproj$ ^\.Rproj\.user$ ^_README.Rmd$ +^_README.md$ ^NEWS.md$ ^\.travis\.yml$ ^README.md$ diff --git a/DESCRIPTION b/DESCRIPTION index 8398d92..aabb147 100644 --- a/DESCRIPTION +++ b/DESCRIPTION @@ -17,4 +17,5 @@ Depends: ggplot2, grid, scales, - gridExtra + gridExtra, + RColorBrewer diff --git a/NAMESPACE b/NAMESPACE index 55687f0..3485930 100644 --- a/NAMESPACE +++ b/NAMESPACE @@ -2,6 +2,7 @@ export(statebins) export(statebins_continuous) +import(RColorBrewer) import(ggplot2) import(grid) import(gridExtra) diff --git a/R/state_coords.R b/R/state_coords.R new file mode 100644 index 0000000..325b468 --- /dev/null +++ b/R/state_coords.R @@ -0,0 +1,20 @@ +#' @title state_coords +#' @description simple coordinates for states to use in a 'statebin' plot +#' \itemize{ +#' \item \code{abbrev}. state abbreviation, including \code{DC} (chr) +#' \item \code{state}. capitalized state name (chr) +#' \item \code{col}. colum where it fits (12x8 grid) (chr) +#' \item \code{row}. row where it fits (12x8 grid) (chr) +#' } +#' +#' @docType data +#' @keywords datasets +#' @name state_coords +#' @seealso \itemize{ +#' \item WaPo - \url{http://www.washingtonpost.com/wp-srv/special/business/states-most-threatened-by-trade/} +#' } +#' @note Last updated 2014-08-25 +#' @format A data frame with 51 rows and 4 variables +NULL + + diff --git a/R/statebins-package.R b/R/statebins-package.R index 52229fe..4a9424e 100644 --- a/R/statebins-package.R +++ b/R/statebins-package.R @@ -2,5 +2,5 @@ #' @name statebins #' @docType package #' @author Bob Rudis (@@hrbrmstr) -#' @import ggplot2 scales grid gridExtra +#' @import ggplot2 scales grid gridExtra RColorBrewer NULL diff --git a/R/statebins.R b/R/statebins.R index 04702b2..d94b94f 100644 --- a/R/statebins.R +++ b/R/statebins.R @@ -2,19 +2,20 @@ #' 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 font_size -#' @param state_border_col -#' @param breaks -#' @param labels -#' @param legend_title -#' @param legend_position -#' @param brewer_pal -#' @param plot_title -#' @param title_position +#' +#' @param state_data data frame of states and values to plot +#' @param state_col column name in \code{state_data} that has the states. no duplicates and can be names (e.g. "Maine") or abbreviatons (e.g. "ME") +#' @param value_col column name in \code{state_data} that holds the values to be plotted +#' @param text_color default "white" +#' @param font_size font size (default = 2) +#' @param state_border_col default "white" - this creates the "spaces" between boxes +#' @param breaks a single number (greater than or equal to 2) giving the number of intervals into which data values are to be cut. +#' @param labels labels for the levels \code{breaks} +#' @param legend_title title for the legend +#' @param legend_position "none", "top" or "bottom" (defaults to "top") +#' @param brewer_pal which named RColorBrewer palette to use +#' @param plot_title title for the plot +#' @param title_position where to put the title ("bottom" or "top" or " " for none); if "bottom", you get back a grob vs a ggplot object #' @return ggplot2 object or grob #' @export statebins <- function(state_data, state_col="state", value_col="value", @@ -40,9 +41,9 @@ statebins <- function(state_data, state_col="state", value_col="value", 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 <- ggplot(st.dat, aes_string(x="col", y="row", label="abbrev")) + gg <- gg + geom_tile(aes_string(fill="fill_color")) + gg <- gg + geom_tile(color=state_border_col, aes_string(fill="fill_color"), size=3, show_guide=FALSE) gg <- gg + geom_text(color=text_color, size=font_size) gg <- gg + scale_y_reverse() gg <- gg + scale_fill_brewer(palette=brewer_pal, name=legend_title) @@ -76,18 +77,19 @@ statebins <- function(state_data, state_col="state", value_col="value", #' 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 font_size -#' @param state_border_col -#' @param legend_title -#' @param legend_position -#' @param brewer_pal -#' @param plot_title -#' @param title_position +#' This version uses a continuous scale akin to \link{scale_fill_distiller} +#' +#' @param state_data data frame of states and values to plot +#' @param state_col column name in \code{state_data} that has the states. no duplicates and can be names (e.g. "Maine") or abbreviatons (e.g. "ME") +#' @param value_col column name in \code{state_data} that holds the values to be plotted +#' @param text_color default "white" +#' @param font_size font size (default = 2) +#' @param state_border_col default "white" - this creates the "spaces" between boxes +#' @param legend_title title for the legend +#' @param legend_position "none", "top" or "bottom" (defaults to "top") +#' @param brewer_pal which named RColorBrewer palette to use +#' @param plot_title title for the plot +#' @param title_position where to put the title ("bottom" or "top" or " " for none); if "bottom", you get back a grob vs a ggplot object #' @return ggplot2 object or grob #' @export statebins_continuous <- function(state_data, state_col="state", value_col="value", @@ -110,14 +112,14 @@ statebins_continuous <- function(state_data, state_col="state", value_col="value 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 <- ggplot(st.dat, aes_string(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, size=font_size) 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) + gradient_n_pal(brewer_pal("seq", 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() diff --git a/README.md b/README.md index 42544e4..3e69cc4 100644 --- a/README.md +++ b/README.md @@ -1,8 +1,9 @@ -statesquares is ... +statebins is an alternative to choropleth maps for US States The following functions are implemented: - `statebins` - +- `statebins_continuous` - ### News @@ -18,14 +19,7 @@ devtools::install_github("hrbrmstr/statebins") ``` {.r} library(statebins) -``` - - ## Loading required package: ggplot2 - ## Loading required package: grid - ## Loading required package: scales - ## Loading required package: gridExtra -``` {.r} # current verison packageVersion("statebins") ``` @@ -33,11 +27,13 @@ packageVersion("statebins") ## [1] '1.0' ``` {.r} +# the original wapo data + 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", + legend_title="State Groups", font_size=3, brewer_pal="Blues", text_color="black", plot_title="1994-2000", title_position="bottom") @@ -49,14 +45,25 @@ gg ``` {.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") + brewer_pal="OrRd", text_color="black", font_size=3, + plot_title="2001-2007", title_position="bottom") gg2 ``` ![plot of chunk unnamed-chunk-3](./_README_files/figure-markdown_github/unnamed-chunk-32.png) +``` {.r} +gg3 <- statebins_continuous(dat, "state", "avgshare08_12", + legend_title="States", legend_position="none", + brewer_pal="Purples", text_color="black", font_size=3, + plot_title="2008-2012", title_position="bottom") + +gg3 +``` + +![plot of chunk unnamed-chunk-3](./_README_files/figure-markdown_github/unnamed-chunk-33.png) + ### Test Results ``` {.r} @@ -66,7 +73,7 @@ library(testthat) date() ``` - ## [1] "Mon Aug 25 22:25:49 2014" + ## [1] "Mon Aug 25 22:36:21 2014" ``` {.r} test_dir("tests/") diff --git a/_README.Rmd b/_README.Rmd index 4eb2e32..ffe7df5 100644 --- a/_README.Rmd +++ b/_README.Rmd @@ -7,15 +7,16 @@ output: variant: markdown_github --- -statesquares is ... +statebins is an alternative to choropleth maps for US States The following functions are implemented: - `statebins` - +- `statebins_continuous` - ### News -- Version `1.0.0` released +- Version `1.0.0` released ### Installation @@ -29,17 +30,19 @@ options(width=120) ### Usage -```{r} +```{r message=FALSE} library(statebins) # current verison packageVersion("statebins") +# the original wapo data + 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", + legend_title="State Groups", font_size=3, brewer_pal="Blues", text_color="black", plot_title="1994-2000", title_position="bottom") @@ -47,11 +50,18 @@ 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") + brewer_pal="OrRd", text_color="black", font_size=3, + plot_title="2001-2007", title_position="bottom") gg2 +gg3 <- statebins_continuous(dat, "state", "avgshare08_12", + legend_title="States", legend_position="none", + brewer_pal="Purples", text_color="black", font_size=3, + plot_title="2008-2012", title_position="bottom") + +gg3 + ``` ### Test Results diff --git a/_README.md b/_README.md deleted file mode 100644 index 89a7a96..0000000 --- a/_README.md +++ /dev/null @@ -1,75 +0,0 @@ -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:27:38 2014" - -``` {.r} -test_dir("tests/") -``` - - ## basic functionality : diff --git a/_README_files/figure-markdown_github/unnamed-chunk-31.png b/_README_files/figure-markdown_github/unnamed-chunk-31.png index 87aa632..590d4b3 100644 Binary files a/_README_files/figure-markdown_github/unnamed-chunk-31.png 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 index c339fcb..22bc13e 100644 Binary files a/_README_files/figure-markdown_github/unnamed-chunk-32.png and b/_README_files/figure-markdown_github/unnamed-chunk-32.png differ diff --git a/_README_files/figure-markdown_github/unnamed-chunk-33.png b/_README_files/figure-markdown_github/unnamed-chunk-33.png new file mode 100644 index 0000000..2e56de8 Binary files /dev/null and b/_README_files/figure-markdown_github/unnamed-chunk-33.png differ diff --git a/man/state_coords.Rd b/man/state_coords.Rd new file mode 100644 index 0000000..13b418b --- /dev/null +++ b/man/state_coords.Rd @@ -0,0 +1,25 @@ +% Generated by roxygen2 (4.0.1.99): do not edit by hand +\docType{data} +\name{state_coords} +\alias{state_coords} +\title{state_coords} +\format{A data frame with 51 rows and 4 variables} +\description{ +simple coordinates for states to use in a 'statebin' plot +\itemize{ + \item \code{abbrev}. state abbreviation, including \code{DC} (chr) + \item \code{state}. capitalized state name (chr) + \item \code{col}. colum where it fits (12x8 grid) (chr) + \item \code{row}. row where it fits (12x8 grid) (chr) +} +} +\note{ +Last updated 2014-08-25 +} +\seealso{ +\itemize{ + \item WaPo - \url{http://www.washingtonpost.com/wp-srv/special/business/states-most-threatened-by-trade/} +} +} +\keyword{datasets} + diff --git a/man/statebins.Rd b/man/statebins.Rd index 4515080..966fd7a 100644 --- a/man/statebins.Rd +++ b/man/statebins.Rd @@ -3,15 +3,46 @@ \name{statebins} \alias{statebins} \alias{statebins-package} -\title{A package to ...} +\title{statebins is an alternative to choropleth maps for US States} \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") + text_color = "white", font_size = 2, state_border_col = "white", + breaks = 5, labels = 1:4, legend_title = "Legend", + legend_position = "top", brewer_pal = "PuBu", plot_title = "", + title_position = "bottom") +} +\arguments{ +\item{state_data}{data frame of states and values to plot} + +\item{state_col}{column name in \code{state_data} that has the states. no duplicates and can be names (e.g. "Maine") or abbreviatons (e.g. "ME")} + +\item{value_col}{column name in \code{state_data} that holds the values to be plotted} + +\item{text_color}{default "white"} + +\item{font_size}{font size (default = 2)} + +\item{state_border_col}{default "white" - this creates the "spaces" between boxes} + +\item{breaks}{a single number (greater than or equal to 2) giving the number of intervals into which data values are to be cut.} + +\item{labels}{labels for the levels \code{breaks}} + +\item{legend_title}{title for the legend} + +\item{legend_position}{"none", "top" or "bottom" (defaults to "top")} + +\item{brewer_pal}{which named RColorBrewer palette to use} + +\item{plot_title}{title for the plot} + +\item{title_position}{where to put the title ("bottom" or "top" or " " for none); if "bottom", you get back a grob vs a ggplot object} +} +\value{ +ggplot2 object or grob } \description{ -A package to ... +statebins is an alternative to choropleth maps for US States This version uses a discrete scale, binned by the "breaks" parameter } diff --git a/man/statebins_continuous.Rd b/man/statebins_continuous.Rd index e4ed6e6..1a421cb 100644 --- a/man/statebins_continuous.Rd +++ b/man/statebins_continuous.Rd @@ -4,11 +4,37 @@ \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", + text_color = "white", font_size = 2, state_border_col = "white", legend_title = "Legend", legend_position = "top", brewer_pal = "PuBu", plot_title = "", title_position = "bottom") } +\arguments{ +\item{state_data}{data frame of states and values to plot} + +\item{state_col}{column name in \code{state_data} that has the states. no duplicates and can be names (e.g. "Maine") or abbreviatons (e.g. "ME")} + +\item{value_col}{column name in \code{state_data} that holds the values to be plotted} + +\item{text_color}{default "white"} + +\item{font_size}{font size (default = 2)} + +\item{state_border_col}{default "white" - this creates the "spaces" between boxes} + +\item{legend_title}{title for the legend} + +\item{legend_position}{"none", "top" or "bottom" (defaults to "top")} + +\item{brewer_pal}{which named RColorBrewer palette to use} + +\item{plot_title}{title for the plot} + +\item{title_position}{where to put the title ("bottom" or "top" or " " for none); if "bottom", you get back a grob vs a ggplot object} +} +\value{ +ggplot2 object or grob +} \description{ -This version uses a discrete scale, binned by the "breaks" parameter +This version uses a continuous scale akin to \link{scale_fill_distiller} }