diff --git a/DESCRIPTION b/DESCRIPTION index 6b892d8..916bb97 100644 --- a/DESCRIPTION +++ b/DESCRIPTION @@ -1,21 +1,20 @@ Package: statebins Type: Package -Title: statebins is an alternative to choropleth maps for US States +Title: statebins is an alternative to choropleth maps for USA States Version: 1.0 Date: 2014-08-26 Author: Bob Rudis (@hrbrmstr) Maintainer: Bob Rudis -Description: statebins is an alternative to choropleth maps for US States and +Description: statebins is an alternative to choropleth maps for USA States and is based on work by the Washington Post graphics department in their report on "The states most threatened by trade". statebins preserves as much of - the geographic placment 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 of both a binned, discrete scale and continuous scale depending on what is needed for the underlying data. URL: http://github.com/hrbrmstr/statebins BugReports: https://github.com/hrbrmstr/statebins/issues License: MIT + file LICENSE -LazyData: yes Suggests: testthat Depends: diff --git a/R/state_coords.R b/R/state_coords.R deleted file mode 100644 index 325b468..0000000 --- a/R/state_coords.R +++ /dev/null @@ -1,20 +0,0 @@ -#' @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.R b/R/statebins.R index 187bd58..9cef61b 100644 --- a/R/statebins.R +++ b/R/statebins.R @@ -1,3 +1,29 @@ +state_coords <- structure(list(abbrev = c("AL", "AK", "AZ", "AR", "CA", "CO", + "CT", "DC", "DE", "FL", "GA", "HI", "ID", "IL", "IN", "IA", "KS", + "KY", "LA", "ME", "MD", "MA", "MI", "MN", "MS", "MO", "MT", "NE", + "NV", "NH", "NJ", "NM", "NY", "NC", "ND", "OH", "OK", "OR", "PA", + "RI", "SC", "SD", "TN", "TX", "UT", "VT", "VA", "WA", "WV", "WI", "WY"), + state = c("Alabama", "Alaska", "Arizona", "Arkansas", + "California", "Colorado", "Connecticut", "District of Columbia", + "Delaware", "Florida", "Georgia", "Hawaii", "Idaho", "Illinois", + "Indiana", "Iowa", "Kansas", "Kentucky", "Louisiana", "Maine", + "Maryland", "Massachusetts", "Michigan", "Minnesota", "Mississippi", + "Missouri", "Montana", "Nebraska", "Nevada", "New Hampshire", + "New Jersey", "New Mexico", "New York", "North Carolina", "North Dakota", + "Ohio", "Oklahoma", "Oregon", "Pennsylvania", "Rhode Island", + "South Carolina", "South Dakota", "Tennessee", "Texas", "Utah", + "Vermont", "Virginia", "Washington", "West Virginia", "Wisconsin", "Wyoming"), + col = c(8L, 2L, 3L, 6L, 2L, 4L, 11L, 10L, 11L, 10L, + 9L, 1L, 3L, 7L, 7L, 6L, 5L, 7L, 6L, 12L, 10L, 11L, 8L, 6L, 7L, + 6L, 4L, 5L, 3L, 12L, 10L, 4L, 10L, 8L, 5L, 8L, 5L, 2L, 9L, 12L, + 9L, 5L, 7L, 5L, 3L, 11L, 9L, 2L, 8L, 7L, 4L), + row = c(7L, 8L, + 6L, 6L, 5L, 5L, 4L, 6L, 5L, 8L, 7L, 8L, 3L, 3L, 4L, 4L, 6L, 5L, + 7L, 1L, 5L, 3L, 3L, 3L, 7L, 5L, 3L, 5L, 4L, 2L, 4L, 6L, 3L, 6L, + 3L, 4L, 7L, 4L, 4L, 4L, 6L, 4L, 6L, 8L, 5L, 2L, 5L, 3L, 5L, 2L, 4L)), + .Names = c("abbrev", "state", "col", "row"), class = "data.frame", row.names = c(NA, -51L)) + + invert <- function(hexColor, darkColor="black", lightColor="white") { hexColor <- gsub("#", "", hexColor) @@ -18,7 +44,7 @@ invert <- function(hexColor, darkColor="black", lightColor="white") { } -#' Create a new ggplot-based "statebin" chart +#' Create a new ggplot-based "statebin" chart for USA states (discrete scale) #' #' \code{statebins()} creates "statebin" charts in the style of \url{http://bit.ly/statebins} #' @@ -37,7 +63,7 @@ invert <- function(hexColor, darkColor="black", lightColor="white") { #' and \code{value_col} accordingly. #' #' To add a title, change \code{plot_title} to anything but an empty atomic string vector (i.e. \code{""}) -#' and set \code{title_positioin} to "\code{top}" or "\code{bottom}". Choosing "\code{bottom}" +#' and set \code{title_position} to "\code{top}" or "\code{bottom}". Choosing "\code{bottom}" #' will cause \code{statebins} to use \link{arrangeGrob} to position the title via \code{sub} and #' return a frame grob instead of a ggplot2 object. #' @@ -45,7 +71,7 @@ invert <- function(hexColor, darkColor="black", lightColor="white") { #' @param state_col column name in \code{state_data} that has the states. no duplicates and can be names (e.g. "\code{Maine}") or abbreviatons (e.g. "\code{ME}") #' @param value_col column name in \code{state_data} that holds the values to be plotted #' @param text_color default "\code{white}" -#' @param font_size font size (default = \code{2}) +#' @param font_size font size (default = \code{3}) #' @param state_border_col default "\code{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} @@ -56,8 +82,15 @@ invert <- function(hexColor, darkColor="black", lightColor="white") { #' @param title_position where to put the title ("\code{bottom}" or "\code{top}" or "" for none); if "\code{bottom}", you get back a grob vs a ggplot object #' @return ggplot2 object or grob #' @export +#' @examples +#' \dontrun{ +#' data(USArrests) +#' USArrests$state <- rownames(USArrests) +#' statebins(USArrests, value_col="Assault", text_color="black", font_size=3, +#' legend_title = "Assault", legend_position="bottom") +#' } statebins <- function(state_data, state_col="state", value_col="value", - text_color="white", font_size=2, + text_color="white", font_size=3, state_border_col="white", breaks=5, labels=1:5, legend_title="Legend", legend_position="top", brewer_pal="PuBu", plot_title="", title_position="bottom") { @@ -88,8 +121,6 @@ statebins <- function(state_data, state_col="state", value_col="value", gg <- gg + coord_equal() gg <- gg + labs(x=NULL, y=NULL, title=NULL) gg <- gg + theme_bw() - gg <- gg + theme(plot.margin=unit(c(-1, 0, 0, 0), "line")) - gg <- gg + theme(panel.margin=unit(-1, "line")) gg <- gg + theme(legend.position=legend_position) gg <- gg + theme(panel.border=element_blank()) gg <- gg + theme(panel.grid=element_blank()) @@ -112,7 +143,7 @@ statebins <- function(state_data, state_col="state", value_col="value", } -#' Create ggplot-based "statebin" charts in the style of \url{http://bit.ly/statebins} +#' Create a new ggplot-based "statebin" chart for USA states (continuous scale) #' #' \code{statebins()} creates "statebin" charts in the style of \url{http://bit.ly/statebins} #' @@ -132,7 +163,7 @@ statebins <- function(state_data, state_col="state", value_col="value", #' and \code{value_col} accordingly. #' #' To add a title, change \code{plot_title} to anything but an empty atomic string vector (i.e. \code{""}) -#' and set \code{title_positioin} to "\code{top}" or "\code{bottom}". Choosing "\code{bottom}" +#' and set \code{title_position} to "\code{top}" or "\code{bottom}". Choosing "\code{bottom}" #' will cause \code{statebins} to use \link{arrangeGrob} to position the title via \code{sub} and #' return a frame grob instead of a ggplot2 object. #' @@ -140,7 +171,7 @@ statebins <- function(state_data, state_col="state", value_col="value", #' @param state_col column name in \code{state_data} that has the states. no duplicates and can be names (e.g. "\code{Maine}") or abbreviatons (e.g. "\code{ME}") #' @param value_col column name in \code{state_data} that holds the values to be plotted #' @param text_color default "\code{white}" -#' @param font_size font size (default = \code{2}) +#' @param font_size font size (default = \code{3}) #' @param state_border_col default "\code{white}" - this creates the "spaces" between boxes #' @param legend_title title for the legend #' @param legend_position "\code{none}", "\code{top}", "\code{left}", "\code{right}" or "\code{bottom}" (defaults to "\code{top}") @@ -149,8 +180,15 @@ statebins <- function(state_data, state_col="state", value_col="value", #' @param title_position where to put the title ("\code{bottom}" or "\code{top}" or "" for none); if "\code{bottom}", you get back a grob vs a ggplot object #' @return ggplot2 object or grob #' @export +#' @examples +#' \dontrun{ +#' data(USArrests) +#' USArrests$state <- rownames(USArrests) +#' statebins_continuous(USArrests, value_col="Murder", text_color="black", font_size=3, +#' legend_title = "Murder", legend_position="bottom") +#' } statebins_continuous <- function(state_data, state_col="state", value_col="value", - text_color="white", font_size=2, + text_color="white", font_size=3, state_border_col="white", legend_title="Legend", legend_position="top", brewer_pal="PuBu", plot_title="", title_position="bottom") { @@ -179,8 +217,6 @@ statebins_continuous <- function(state_data, state_col="state", value_col="value gg <- gg + coord_equal() gg <- gg + labs(x=NULL, y=NULL, title=NULL) gg <- gg + theme_bw() - gg <- gg + theme(plot.margin=unit(c(-1, 0, 0, 0), "line")) - gg <- gg + theme(panel.margin=unit(-1, "line")) gg <- gg + theme(legend.position=legend_position) gg <- gg + theme(panel.border=element_blank()) gg <- gg + theme(panel.grid=element_blank()) diff --git a/_README.Rmd b/README.Rmd similarity index 95% rename from _README.Rmd rename to README.Rmd index 8479a67..254191c 100644 --- a/_README.Rmd +++ b/README.Rmd @@ -8,7 +8,7 @@ output: --- -statebins is an alternative to choropleth maps for US States +statebins is an alternative to choropleth maps for USA States The following functions are implemented: @@ -36,7 +36,7 @@ options(width=120) ### Usage -All of the following examples use the [WaPo data](http://www.washingtonpost.com/wp-srv/special/business/states-most-threatened-by-trade/states.csv?cache=1). It looks like the colums they use are scaled data and I didn't take the time to figure out what they did, so the final figure just mimics their output (including the non-annotated legend). +All of the following examples use the [WaPo data](http://www.washingtonpost.com/wp-srv/special/business/states-most-threatened-by-trade/states.csv?cache=1). It looks like the columns they use are scaled data and I didn't take the time to figure out what they did, so the final figure just mimics their output (including the non-annotated legend). ```{r message=FALSE} library(statebins) diff --git a/README.md b/README.md index c51b80d..92917e9 100644 --- a/README.md +++ b/README.md @@ -128,9 +128,7 @@ sb("avgshare08_12", "2008-2012") -->
- ![img](./tmp/statebins-composite.png) -
And, we'll throw in a gratuitous animation for good measure: @@ -177,7 +175,7 @@ library(testthat) date() ``` - ## [1] "Tue Aug 26 15:38:13 2014" + ## [1] "Tue Aug 26 17:39:41 2014" ``` {.r} 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..6cb9d36 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..5200b01 Binary files /dev/null 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..55b4246 Binary files /dev/null and b/README_files/figure-markdown_github/unnamed-chunk-33.png differ diff --git a/_README_files/figure-markdown_github/unnamed-chunk-31.png b/_README_files/figure-markdown_github/unnamed-chunk-31.png index 95874e1..6cb9d36 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 b7a1615..5200b01 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 index 932a489..55b4246 100644 Binary files a/_README_files/figure-markdown_github/unnamed-chunk-33.png and b/_README_files/figure-markdown_github/unnamed-chunk-33.png differ diff --git a/data/states.rda b/data/states.rda deleted file mode 100644 index 9bf9366..0000000 Binary files a/data/states.rda and /dev/null differ diff --git a/man/state_coords.Rd b/man/state_coords.Rd deleted file mode 100644 index bcb6244..0000000 --- a/man/state_coords.Rd +++ /dev/null @@ -1,25 +0,0 @@ -% Generated by roxygen2 (4.0.1): 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 aa29d01..2e1b484 100644 --- a/man/statebins.Rd +++ b/man/statebins.Rd @@ -1,10 +1,10 @@ % Generated by roxygen2 (4.0.1): do not edit by hand \name{statebins} \alias{statebins} -\title{Create a new ggplot-based "statebin" chart} +\title{Create a new ggplot-based "statebin" chart for USA states (discrete scale)} \usage{ statebins(state_data, state_col = "state", value_col = "value", - text_color = "white", font_size = 2, state_border_col = "white", + text_color = "white", font_size = 3, state_border_col = "white", breaks = 5, labels = 1:5, legend_title = "Legend", legend_position = "top", brewer_pal = "PuBu", plot_title = "", title_position = "bottom") @@ -18,7 +18,7 @@ statebins(state_data, state_col = "state", value_col = "value", \item{text_color}{default "\code{white}"} -\item{font_size}{font size (default = \code{2})} +\item{font_size}{font size (default = \code{3})} \item{state_border_col}{default "\code{white}" - this creates the "spaces" between boxes} @@ -58,8 +58,16 @@ You can use a different column for the state names and values by changing \code{ and \code{value_col} accordingly. To add a title, change \code{plot_title} to anything but an empty atomic string vector (i.e. \code{""}) -and set \code{title_positioin} to "\code{top}" or "\code{bottom}". Choosing "\code{bottom}" +and set \code{title_position} to "\code{top}" or "\code{bottom}". Choosing "\code{bottom}" will cause \code{statebins} to use \link{arrangeGrob} to position the title via \code{sub} and return a frame grob instead of a ggplot2 object. } +\examples{ +\dontrun{ +data(USArrests) +USArrests$state <- rownames(USArrests) +statebins(USArrests, value_col="Assault", text_color="black", font_size=3, + legend_title = "Assault", legend_position="bottom") +} +} diff --git a/man/statebins_continuous.Rd b/man/statebins_continuous.Rd index f6c92c5..a17435f 100644 --- a/man/statebins_continuous.Rd +++ b/man/statebins_continuous.Rd @@ -1,10 +1,10 @@ % Generated by roxygen2 (4.0.1): do not edit by hand \name{statebins_continuous} \alias{statebins_continuous} -\title{Create ggplot-based "statebin" charts in the style of \url{http://bit.ly/statebins}} +\title{Create a new ggplot-based "statebin" chart for USA states (continuous scale)} \usage{ statebins_continuous(state_data, state_col = "state", value_col = "value", - text_color = "white", font_size = 2, state_border_col = "white", + text_color = "white", font_size = 3, state_border_col = "white", legend_title = "Legend", legend_position = "top", brewer_pal = "PuBu", plot_title = "", title_position = "bottom") } @@ -17,7 +17,7 @@ statebins_continuous(state_data, state_col = "state", value_col = "value", \item{text_color}{default "\code{white}"} -\item{font_size}{font size (default = \code{2})} +\item{font_size}{font size (default = \code{3})} \item{state_border_col}{default "\code{white}" - this creates the "spaces" between boxes} @@ -54,8 +54,16 @@ You can use a different column for the state names and values by changing \code{ and \code{value_col} accordingly. To add a title, change \code{plot_title} to anything but an empty atomic string vector (i.e. \code{""}) -and set \code{title_positioin} to "\code{top}" or "\code{bottom}". Choosing "\code{bottom}" +and set \code{title_position} to "\code{top}" or "\code{bottom}". Choosing "\code{bottom}" will cause \code{statebins} to use \link{arrangeGrob} to position the title via \code{sub} and return a frame grob instead of a ggplot2 object. } +\examples{ +\dontrun{ +data(USArrests) +USArrests$state <- rownames(USArrests) +statebins_continuous(USArrests, value_col="Murder", text_color="black", font_size=3, + legend_title = "Murder", legend_position="bottom") +} +}