diff --git a/NAMESPACE b/NAMESPACE index 3485930..091272f 100644 --- a/NAMESPACE +++ b/NAMESPACE @@ -1,4 +1,4 @@ -# Generated by roxygen2 (4.0.1.99): do not edit by hand +# Generated by roxygen2 (4.0.1): do not edit by hand export(statebins) export(statebins_continuous) diff --git a/R/statebins.R b/R/statebins.R index 35b65fe..c87ae09 100644 --- a/R/statebins.R +++ b/R/statebins.R @@ -1,3 +1,23 @@ +invert <- function(hexColor, darkColor="black", lightColor="white") { + + hexColor <- gsub("#", "", hexColor) + + R <- as.integer(paste("0x", substr(hexColor,1,2), sep="")) + G <- as.integer(paste("0x", substr(hexColor,3,4), sep="")) + B <- as.integer(paste("0x", substr(hexColor,5,6), sep="")) + + cat("R ") ; print(R) + cat("G ") ; print(G) + cat("B ") ; print(B) + + YIQ <- ((R*299) + (G*587) + (B*114)) / 1000 + + cat("YIQ ") ; print(YIQ) + + return(ifelse(YIQ >= 128, darkColor, lightColor)) + +} + #' Create a new ggplot-based "statebin" chart #' #' \code{statebins()} creates "statebin" charts in the style of \url{http://bit.ly/statebins} @@ -38,7 +58,7 @@ #' @export statebins <- function(state_data, state_col="state", value_col="value", text_color="white", font_size=2, - state_border_col="white", breaks=5, labels=1:4, + state_border_col="white", breaks=5, labels=1:5, legend_title="Legend", legend_position="top", brewer_pal="PuBu", plot_title="", title_position="bottom") { @@ -61,16 +81,15 @@ statebins <- function(state_data, state_col="state", value_col="value", 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_tile(color=state_border_col, aes_string(fill="fill_color"), size=2, 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) gg <- gg + coord_equal() - gg <- gg + labs(x="", y="", title="") + gg <- gg + labs(x=NULL, y=NULL, title="") gg <- gg + theme_bw() + gg <- gg + theme(plot.margin=unit(c(-0.5,-0.5,-0.5,-0.5), "cm")) 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()) @@ -157,11 +176,10 @@ statebins_continuous <- function(state_data, state_col="state", value_col="value gg <- gg + scale_y_reverse() gg <- gg + scale_fill_gradientn(colours = brewer.pal(6, brewer_pal), name=legend_title) gg <- gg + coord_equal() - gg <- gg + labs(x="", y="", title="") + gg <- gg + labs(x=NULL, y=NULL, 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(plot.margin=unit(c(-0.5,-0.5,-0.5,-0.5), "cm")) gg <- gg + theme(panel.border=element_blank()) gg <- gg + theme(panel.grid=element_blank()) gg <- gg + theme(panel.background=element_blank()) diff --git a/README.md b/README.md index 3e69cc4..bfa65e6 100644 --- a/README.md +++ b/README.md @@ -2,8 +2,8 @@ statebins is an alternative to choropleth maps for US States The following functions are implemented: -- `statebins` - -- `statebins_continuous` - +- `statebins` - creates "statebin" charts in the style of - This version uses discrete `RColorBrewer` scales, binned by the "breaks" parameter. +- `statebins_continuous` - creates "statebin" charts in the style of - This version uses a continuous scale based on `RColorBrewer` scales (passing in a 6 element `RColorBrewer` palette to `scale_fill_gradientn`). ### News @@ -43,10 +43,12 @@ gg ![plot of chunk unnamed-chunk-3](./_README_files/figure-markdown_github/unnamed-chunk-31.png) ``` {.r} +# continuous scale, legend on top + gg2 <- statebins_continuous(dat, "state", "avgshare01_07", - legend_title="States", legend_position="none", - brewer_pal="OrRd", text_color="black", font_size=3, - plot_title="2001-2007", title_position="bottom") + legend_title="State Data", legend_position="top", + brewer_pal="OrRd", text_color="black", font_size=3, + plot_title="2001-2007", title_position="bottom") gg2 ``` @@ -54,16 +56,37 @@ gg2 ![plot of chunk unnamed-chunk-3](./_README_files/figure-markdown_github/unnamed-chunk-32.png) ``` {.r} +# continuous scale, no legend + 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") + 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) +``` {.r} +# 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) { + statebins(dat, "state",col, brewer_pal="Blues", text_color="black", legend_position="none", font_size=3, plot_title=title, breaks=4, labels=1:4) +} + +image(1:4, 1, as.matrix(1:4), col = brewer.pal(4, name="Blues"), xlab = "", ylab = "", xaxt = "n", yaxt = "n", bty = "n") +``` + +![plot of chunk unnamed-chunk-3](./_README_files/figure-markdown_github/unnamed-chunk-34.png) + +``` {.r} +grid.arrange(sb("avgshare94_00", "1994-2000"), sb("avgshare01_07", "2001-2007"), + sb("avgshare08_12", "2008-2012"), ncol=2) +``` + +![plot of chunk unnamed-chunk-3](./_README_files/figure-markdown_github/unnamed-chunk-35.png) + ### Test Results ``` {.r} @@ -73,7 +96,7 @@ library(testthat) date() ``` - ## [1] "Mon Aug 25 22:36:21 2014" + ## [1] "Tue Aug 26 13:18:47 2014" ``` {.r} test_dir("tests/") diff --git a/_README.Rmd b/_README.Rmd index cf590a5..bf1ca5d 100644 --- a/_README.Rmd +++ b/_README.Rmd @@ -1,7 +1,7 @@ --- title: "README" author: "Bob Rudis" -date: August 25, 2014 +date: August 26, 2014 output: md_document: variant: markdown_github @@ -11,8 +11,8 @@ statebins is an alternative to choropleth maps for US States The following functions are implemented: -- `statebins` - -- `statebins_continuous` - +- `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`). ### News @@ -48,6 +48,8 @@ gg <- statebins(dat, "state", "avgshare94_00", breaks=4, gg +# continuous scale, legend on top + gg2 <- statebins_continuous(dat, "state", "avgshare01_07", legend_title="State Data", legend_position="top", brewer_pal="OrRd", text_color="black", font_size=3, @@ -55,6 +57,8 @@ gg2 <- statebins_continuous(dat, "state", "avgshare01_07", gg2 +# continuous scale, no legend + gg3 <- statebins_continuous(dat, "state", "avgshare08_12", legend_title="States", legend_position="none", brewer_pal="Purples", text_color="black", font_size=3, @@ -62,6 +66,17 @@ gg3 <- statebins_continuous(dat, "state", "avgshare08_12", gg3 +# 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) { + statebins(dat, "state",col, brewer_pal="Blues", text_color="black", legend_position="none", font_size=3, plot_title=title, breaks=4, labels=1:4) +} + +image(1:4, 1, as.matrix(1:4), col = brewer.pal(4, name="Blues"), xlab = "", ylab = "", xaxt = "n", yaxt = "n", bty = "n") + +grid.arrange(sb("avgshare94_00", "1994-2000"), sb("avgshare01_07", "2001-2007"), + sb("avgshare08_12", "2008-2012"), ncol=2) + ``` ### Test Results diff --git a/_README_files/figure-markdown_github/unnamed-chunk-31.png b/_README_files/figure-markdown_github/unnamed-chunk-31.png index 590d4b3..7dddac7 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 22bc13e..594e6bc 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 2e56de8..b107362 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/_README_files/figure-markdown_github/unnamed-chunk-34.png b/_README_files/figure-markdown_github/unnamed-chunk-34.png new file mode 100644 index 0000000..9dcadbc Binary files /dev/null and b/_README_files/figure-markdown_github/unnamed-chunk-34.png differ diff --git a/_README_files/figure-markdown_github/unnamed-chunk-35.png b/_README_files/figure-markdown_github/unnamed-chunk-35.png new file mode 100644 index 0000000..88ad554 Binary files /dev/null and b/_README_files/figure-markdown_github/unnamed-chunk-35.png differ diff --git a/man/state_coords.Rd b/man/state_coords.Rd index 13b418b..bcb6244 100644 --- a/man/state_coords.Rd +++ b/man/state_coords.Rd @@ -1,4 +1,4 @@ -% Generated by roxygen2 (4.0.1.99): do not edit by hand +% Generated by roxygen2 (4.0.1): do not edit by hand \docType{data} \name{state_coords} \alias{state_coords} diff --git a/man/statebins-package.Rd b/man/statebins-package.Rd index 2b0675a..6477836 100644 --- a/man/statebins-package.Rd +++ b/man/statebins-package.Rd @@ -1,4 +1,4 @@ -% Generated by roxygen2 (4.0.1.99): do not edit by hand +% Generated by roxygen2 (4.0.1): do not edit by hand \docType{package} \name{statebins-package} \alias{statebins-package} diff --git a/man/statebins.Rd b/man/statebins.Rd index 03c0f0d..0b749cd 100644 --- a/man/statebins.Rd +++ b/man/statebins.Rd @@ -1,4 +1,4 @@ -% Generated by roxygen2 (4.0.1.99): do not edit by hand +% Generated by roxygen2 (4.0.1): do not edit by hand \name{statebins} \alias{statebins} \title{Create a new ggplot-based "statebin" chart} diff --git a/man/statebins_continuous.Rd b/man/statebins_continuous.Rd index cb1b264..f6c92c5 100644 --- a/man/statebins_continuous.Rd +++ b/man/statebins_continuous.Rd @@ -1,4 +1,4 @@ -% Generated by roxygen2 (4.0.1.99): do not edit by hand +% 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}}