Browse Source

docs

master
hrbrmstr 10 years ago
parent
commit
7268c24492
No known key found for this signature in database GPG Key ID: 1D7529BE14E2BBA9
  1. 2
      NAMESPACE
  2. 34
      R/statebins.R
  3. 31
      README.md
  4. 21
      _README.Rmd
  5. BIN
      _README_files/figure-markdown_github/unnamed-chunk-31.png
  6. BIN
      _README_files/figure-markdown_github/unnamed-chunk-32.png
  7. BIN
      _README_files/figure-markdown_github/unnamed-chunk-33.png
  8. BIN
      _README_files/figure-markdown_github/unnamed-chunk-34.png
  9. BIN
      _README_files/figure-markdown_github/unnamed-chunk-35.png
  10. 2
      man/state_coords.Rd
  11. 2
      man/statebins-package.Rd
  12. 2
      man/statebins.Rd
  13. 2
      man/statebins_continuous.Rd

2
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)

34
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())

31
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 <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
@ -43,8 +43,10 @@ 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",
legend_title="State Data", legend_position="top",
brewer_pal="OrRd", text_color="black", font_size=3,
plot_title="2001-2007", title_position="bottom")
@ -54,6 +56,8 @@ 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,
@ -64,6 +68,25 @@ 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/")

21
_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

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

Binary file not shown.

Before

Width:  |  Height:  |  Size: 32 KiB

After

Width:  |  Height:  |  Size: 32 KiB

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

Binary file not shown.

Before

Width:  |  Height:  |  Size: 30 KiB

After

Width:  |  Height:  |  Size: 33 KiB

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

Binary file not shown.

Before

Width:  |  Height:  |  Size: 30 KiB

After

Width:  |  Height:  |  Size: 30 KiB

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

Binary file not shown.

After

Width:  |  Height:  |  Size: 7.7 KiB

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

Binary file not shown.

After

Width:  |  Height:  |  Size: 56 KiB

2
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}

2
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}

2
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}

2
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}}

Loading…
Cancel
Save