Browse Source

rounded rects

master
boB Rudis 5 years ago
parent
commit
50bba28268
No known key found for this signature in database GPG Key ID: 1D7529BE14E2BBA9
  1. 9
      DESCRIPTION
  2. 7
      NAMESPACE
  3. 1
      NEWS.md
  4. 52
      R/geom-rrect.r
  5. 41
      R/geom-rtile.R
  6. 8
      R/gutil.R
  7. 4
      R/statebins-package.R
  8. 17
      R/statebins.R
  9. 17
      README.Rmd
  10. 20
      README.md
  11. BIN
      README_files/figure-gfm/unnamed-chunk-4-1.png
  12. BIN
      README_files/figure-gfm/unnamed-chunk-5-1.png
  13. BIN
      _README_files/figure-markdown_github/unnamed-chunk-31.png
  14. BIN
      _README_files/figure-markdown_github/unnamed-chunk-32.png
  15. BIN
      _README_files/figure-markdown_github/unnamed-chunk-33.png
  16. 54
      inst/extdata/deaths.csv
  17. 52
      inst/extdata/election2012.csv
  18. 1
      inst/extdata/wapostates.csv
  19. 6
      man/statebins.Rd

9
DESCRIPTION

@ -26,3 +26,12 @@ Imports:
ggplot2 (>= 2.2.0),
scales (>= 0.5.0)
RoxygenNote: 6.0.1
Collate:
'aaa.R'
'geom-rrect.r'
'geom-rtile.R'
'gutil.R'
'statebins-package.R'
'statebins.R'
'theme-statebin.R'
'util.R'

7
NAMESPACE

@ -2,10 +2,14 @@
export(statebins)
export(theme_statebins)
importFrom(ggplot2,Geom)
importFrom(ggplot2,GeomRect)
importFrom(ggplot2,Stat)
importFrom(ggplot2,aes)
importFrom(ggplot2,aes_)
importFrom(ggplot2,aes_string)
importFrom(ggplot2,coord_equal)
importFrom(ggplot2,draw_key_polygon)
importFrom(ggplot2,element_blank)
importFrom(ggplot2,element_rect)
importFrom(ggplot2,element_text)
@ -14,15 +18,18 @@ importFrom(ggplot2,geom_text)
importFrom(ggplot2,geom_tile)
importFrom(ggplot2,ggplot)
importFrom(ggplot2,ggplotGrob)
importFrom(ggplot2,ggproto)
importFrom(ggplot2,ggtitle)
importFrom(ggplot2,guides)
importFrom(ggplot2,labs)
importFrom(ggplot2,layer)
importFrom(ggplot2,rel)
importFrom(ggplot2,scale_color_manual)
importFrom(ggplot2,scale_fill_brewer)
importFrom(ggplot2,scale_fill_manual)
importFrom(ggplot2,scale_x_continuous)
importFrom(ggplot2,scale_y_continuous)
importFrom(ggplot2,scale_y_reverse)
importFrom(ggplot2,theme)
importFrom(ggplot2,theme_bw)
importFrom(scales,alpha)

1
NEWS.md

@ -2,6 +2,7 @@
* Added support for `VI`/`Virgin Islands`
* Supports auto-computation of when to use light & dark label colors
* New `theme_statebins()`
* Rounded rectangle support!
* Completely re-designed the interface and will not be providing backwards-compatibility.
# statebins 1.2.2

52
R/geom-rrect.r

@ -0,0 +1,52 @@
geom_rrect <- function(mapping = NULL, data = NULL,
stat = "identity", position = "identity",
...,
na.rm = FALSE,
show.legend = NA,
inherit.aes = TRUE) {
layer(
data = data,
mapping = mapping,
stat = stat,
geom = GeomRect,
position = position,
show.legend = show.legend,
inherit.aes = inherit.aes,
params = list(
na.rm = na.rm,
...
)
)
}
GeomRrect <- ggplot2::ggproto("GeomRrect", ggplot2::Geom,
default_aes = aes(colour = NA, fill = "grey35", size = 0.5, linetype = 1,
alpha = NA),
required_aes = c("xmin", "xmax", "ymin", "ymax"),
draw_panel = function(self, data, panel_params, coord) {
coords <- coord$transform(data, panel_params)
lapply(1:length(coords$xmin), function(i) {
ggname("geom_rrect", grid::roundrectGrob(
coords$xmin[i], coords$ymax[i],
width = (coords$xmax[i] - coords$xmin[i]),
height = (coords$ymax[i] - coords$ymin)[i],
r=unit(0.3, "snpc"),
default.units = "native",
just = c("left", "top"),
gp = grid::gpar(
col = coords$colour[i],
fill = alpha(coords$fill[i], coords$alpha[i]),
lwd = coords$size[i] * .pt,
lty = coords$linetype[i],
lineend = "butt"
)
))
}) -> gl
do.call(grid::gList, gl)
},
draw_key = ggplot2::draw_key_polygon
)

41
R/geom-rtile.R

@ -0,0 +1,41 @@
geom_rtile <- function(mapping = NULL, data = NULL,
stat = "identity", position = "identity",
...,
na.rm = FALSE,
show.legend = NA,
inherit.aes = TRUE) {
layer(
data = data,
mapping = mapping,
stat = stat,
geom = GeomRtile,
position = position,
show.legend = show.legend,
inherit.aes = inherit.aes,
params = list(
na.rm = na.rm,
...
)
)
}
GeomRtile <- ggplot2::ggproto("GeomRtile", GeomRrect,
extra_params = c("na.rm", "width", "height"),
setup_data = function(data, params) {
data$width <- data$width %||% params$width %||% resolution(data$x, FALSE)
data$height <- data$height %||% params$height %||% resolution(data$y, FALSE)
transform(data,
xmin = x - width / 2, xmax = x + width / 2, width = NULL,
ymin = y - height / 2, ymax = y + height / 2, height = NULL
)
},
default_aes = aes(fill = "grey20", colour = NA, size = 0.1, linetype = 1,
alpha = NA),
required_aes = c("x", "y"),
draw_key = ggplot2::draw_key_polygon
)

8
R/gutil.R

@ -0,0 +1,8 @@
# Name ggplot grid object
# Convenience function to name grid objects
#
# @keyword internal
ggname <- function(prefix, grob) {
grob$name <- grid::grobName(grob, prefix)
grob
}

4
R/statebins-package.R

@ -9,6 +9,6 @@
#' @importFrom ggplot2 geom_point geom_text scale_color_manual guides theme labs
#' @importFrom ggplot2 scale_x_continuous scale_y_continuous coord_equal theme_bw
#' @importFrom ggplot2 aes element_rect element_blank element_text
#' @importFrom ggplot2 aes_string aes_
#' @importFrom ggplot2 scale_fill_brewer ggtitle rel
#' @importFrom ggplot2 aes_string aes_ scale_y_reverse layer GeomRect
#' @importFrom ggplot2 scale_fill_brewer ggtitle rel ggproto draw_key_polygon Geom Stat
NULL

17
R/statebins.R

@ -27,8 +27,10 @@
#' @param font_size font size (default = \code{3})
#' @param state_border_col default "\code{white}" - this creates the "spaces" between boxes
#' @param state_border_size border size
#' @param round rounded corners (default: `FALSE`)
#' @param ggplot2_scale_function ggplot2 scale function to use. Defaults to `scale_fill_distiller`
#' since you're likely passing in continuous data when you shouldn't be :-)
#' @param ... additional parameters to the scale function
#' @return ggplot2 object
#' @export
#' @examples
@ -41,6 +43,7 @@ statebins <- function(state_data,
state_col="state", value_col="value",
dark_label = "black", light_label = "white", font_size=3,
state_border_col="white", state_border_size=2,
round = FALSE,
ggplot2_scale_function=ggplot2::scale_fill_distiller,
...) {
@ -58,9 +61,17 @@ statebins <- function(state_data,
sort=TRUE)
gg <- ggplot()
gg <- gg + geom_tile(data = st.dat,
aes_string(x = "col", y = "row", fill = value_col),
color = state_border_col, size = state_border_size)
if (round) {
gg <- gg + geom_rtile(data = st.dat,
aes_string(x = "col", y = "row", fill = value_col),
color = state_border_col, size = state_border_size)
} else {
gg <- gg + geom_tile(data = st.dat,
aes_string(x = "col", y = "row", fill = value_col),
color = state_border_col, size = state_border_size)
}
gg <- gg + scale_y_reverse()
gg <- gg + ggplot2_scale_function(...)
gg <- gg + coord_equal()

17
README.Rmd

@ -38,7 +38,7 @@ packageVersion("statebins")
# the original wapo data
adat <- suppressMessages(read_csv("http://www.washingtonpost.com/wp-srv/special/business/states-most-threatened-by-trade/states.csv?cache=1"))
adat <- suppressMessages(read_csv(system.file("extdata", "wapostates.csv", package="statebins")))
mutate(
adat,
@ -73,7 +73,7 @@ statebins(adat, value_col = "avgshare08_12", palette = "Purples") +
# mortality data (has Puerto Rico)
# from: http://www.cdc.gov/nchs/fastats/state-and-territorial-data.htm
dat <- suppressMessages(read_csv("http://datadrivensecurity.info/data/deaths.csv"))
dat <- suppressMessages(read_csv(system.file("extdata", "deaths.csv", package="statebins")))
statebins(dat, value_col = "death_rate", name="Per 100K pop") +
labs(title="Mortality Rate (2010)") +
@ -87,7 +87,7 @@ statebins(dat, value_col="fertility_rate", name="Per 100K pop", palette="PuBuGn"
# manual - perhaps good for elections?
election_2012 <- suppressMessages(read_csv("https://raw.githubusercontent.com/hrbrmstr/statebins/master/tmp/election2012.csv"))
election_2012 <- suppressMessages(read_csv(system.file("extdata", "election2012.csv", package="statebins")))
mutate(election_2012, value = ifelse(is.na(Obama), "Romney", "Obama")) %>%
statebins(
@ -99,6 +99,17 @@ mutate(election_2012, value = ifelse(is.na(Obama), "Romney", "Obama")) %>%
theme_statebins()
```
### Rounded rects!
```{r}
data(USArrests)
USArrests$state <- rownames(USArrests)
statebins(USArrests, value_col="Assault", name = "Assault", round=TRUE) +
theme_statebins(legend_position="right")
```
### All the "states"
`statebins` now has PR, VI & NYC (by name or abbreviation) so you can use them, too:

20
README.md

@ -38,7 +38,7 @@ packageVersion("statebins")
``` r
# the original wapo data
adat <- suppressMessages(read_csv("http://www.washingtonpost.com/wp-srv/special/business/states-most-threatened-by-trade/states.csv?cache=1"))
adat <- suppressMessages(read_csv(system.file("extdata", "wapostates.csv", package="statebins")))
mutate(
adat,
@ -83,7 +83,7 @@ statebins(adat, value_col = "avgshare08_12", palette = "Purples") +
# mortality data (has Puerto Rico)
# from: http://www.cdc.gov/nchs/fastats/state-and-territorial-data.htm
dat <- suppressMessages(read_csv("http://datadrivensecurity.info/data/deaths.csv"))
dat <- suppressMessages(read_csv(system.file("extdata", "deaths.csv", package="statebins")))
statebins(dat, value_col = "death_rate", name="Per 100K pop") +
labs(title="Mortality Rate (2010)") +
@ -105,7 +105,7 @@ statebins(dat, value_col="fertility_rate", name="Per 100K pop", palette="PuBuGn"
``` r
# manual - perhaps good for elections?
election_2012 <- suppressMessages(read_csv("https://raw.githubusercontent.com/hrbrmstr/statebins/master/tmp/election2012.csv"))
election_2012 <- suppressMessages(read_csv(system.file("extdata", "election2012.csv", package="statebins")))
mutate(election_2012, value = ifelse(is.na(Obama), "Romney", "Obama")) %>%
statebins(
@ -119,6 +119,18 @@ mutate(election_2012, value = ifelse(is.na(Obama), "Romney", "Obama")) %>%
<img src="README_files/figure-gfm/unnamed-chunk-3-6.png" width="672" />
### Rounded rects\!
``` r
data(USArrests)
USArrests$state <- rownames(USArrests)
statebins(USArrests, value_col="Assault", name = "Assault", round=TRUE) +
theme_statebins(legend_position="right")
```
<img src="README_files/figure-gfm/unnamed-chunk-4-1.png" width="672" />
### All the “states”
`statebins` now has PR, VI & NYC (by name or abbreviation) so you can
@ -150,4 +162,4 @@ statebins(us_arrests, value_col="Assault",
theme_statebins("right")
```
<img src="README_files/figure-gfm/unnamed-chunk-4-1.png" width="672" />
<img src="README_files/figure-gfm/unnamed-chunk-5-1.png" width="672" />

BIN
README_files/figure-gfm/unnamed-chunk-4-1.png

Binary file not shown.

Before

Width:  |  Height:  |  Size: 87 KiB

After

Width:  |  Height:  |  Size: 118 KiB

BIN
README_files/figure-gfm/unnamed-chunk-5-1.png

Binary file not shown.

After

Width:  |  Height:  |  Size: 87 KiB

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

Binary file not shown.

Before

Width:  |  Height:  |  Size: 34 KiB

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

Binary file not shown.

Before

Width:  |  Height:  |  Size: 36 KiB

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

Binary file not shown.

Before

Width:  |  Height:  |  Size: 30 KiB

54
inst/extdata/deaths.csv

@ -0,0 +1,54 @@
state,births,fertility_rate,deaths,death_rate
Alabama,58448,60.9,48681,1013.6
Alaska,11187,75.9,3849,532.6
Arizona,86441,67.5,48381,746.3
Arkansas,38347,67.0,29653,1009.3
California,503755,63.3,239942,636.6
Colorado,65187,62.0,32563,636.4
Connecticut,36359,53.2,29526,824.6
Delaware,11023,61.4,7845,864.8
District of Columbia,9399,55.3,4589,742.6
Florida,213148,59.0,173976,912.9
Georgia,130280,62.4,71248,725.9
Hawaii,18980,71.6,9923,721.8
Idaho,22963,74.1,12027,758.8
Illinois,159160,60.9,101906,791.9
Indiana,83227,64.7,58202,893.1
Iowa,38702,66.8,28184,920.4
Kansas,40341,72.3,25116,874.7
Kentucky,55758,65.4,42626,975.6
Louisiana,62642,67.0,40890,893.8
Maine,12798,53.9,13007,978.9
Maryland,72883,60.8,43745,750.6
Massachusetts,72439,53.5,53710,815.3
Michigan,113091,59.6,89508,906.3
Minnesota,68772,65.7,39820,745.0
Mississippi,38669,64.2,29278,983.0
Missouri,75446,64.3,55848,929.1
Montana,12118,66.6,9115,913.1
Nebraska,25942,72.3,15476,839.9
Nevada,34911,63.0,20343,747.0
New Hampshire,12352,50.3,10823,821.0
New Jersey,104230,60.4,70558,799.9
New Mexico,27068,67.8,16452,790.1
New York,240916,59.7,149174,766.4
North Carolina,119831,61.0,79882,827.2
North Dakota,10106,74.6,5965,872.2
Ohio,138483,62.6,111427,965.2
Oklahoma,52751,70.5,37175,980.5
Oregon,45067,59.0,32788,846.8
Pennsylvania,142514,58.8,128237,1006.3
Rhode Island,10926,51.7,9581,911.3
South Carolina,57155,61.4,42072,899.1
South Dakota,12104,78.1,7314,887.5
Tennessee,80371,62.6,60541,945.5
Texas,382727,69.9,168640,656.8
Utah,51465,83.1,15266,541.9
Vermont,6009,51.6,5433,867.3
Virginia,103013,61.7,60804,751.0
Washington,87463,63.5,49691,727.5
West Virginia,20827,61.3,21867,1178.6
Wisconsin,67295,61.8,48410,847.5
Wyoming,7572,69.5,4387,772.1
Puerto Rico,38900,51.8,29758,802.8
1 state births fertility_rate deaths death_rate
2 Alabama 58448 60.9 48681 1013.6
3 Alaska 11187 75.9 3849 532.6
4 Arizona 86441 67.5 48381 746.3
5 Arkansas 38347 67.0 29653 1009.3
6 California 503755 63.3 239942 636.6
7 Colorado 65187 62.0 32563 636.4
8 Connecticut 36359 53.2 29526 824.6
9 Delaware 11023 61.4 7845 864.8
10 District of Columbia 9399 55.3 4589 742.6
11 Florida 213148 59.0 173976 912.9
12 Georgia 130280 62.4 71248 725.9
13 Hawaii 18980 71.6 9923 721.8
14 Idaho 22963 74.1 12027 758.8
15 Illinois 159160 60.9 101906 791.9
16 Indiana 83227 64.7 58202 893.1
17 Iowa 38702 66.8 28184 920.4
18 Kansas 40341 72.3 25116 874.7
19 Kentucky 55758 65.4 42626 975.6
20 Louisiana 62642 67.0 40890 893.8
21 Maine 12798 53.9 13007 978.9
22 Maryland 72883 60.8 43745 750.6
23 Massachusetts 72439 53.5 53710 815.3
24 Michigan 113091 59.6 89508 906.3
25 Minnesota 68772 65.7 39820 745.0
26 Mississippi 38669 64.2 29278 983.0
27 Missouri 75446 64.3 55848 929.1
28 Montana 12118 66.6 9115 913.1
29 Nebraska 25942 72.3 15476 839.9
30 Nevada 34911 63.0 20343 747.0
31 New Hampshire 12352 50.3 10823 821.0
32 New Jersey 104230 60.4 70558 799.9
33 New Mexico 27068 67.8 16452 790.1
34 New York 240916 59.7 149174 766.4
35 North Carolina 119831 61.0 79882 827.2
36 North Dakota 10106 74.6 5965 872.2
37 Ohio 138483 62.6 111427 965.2
38 Oklahoma 52751 70.5 37175 980.5
39 Oregon 45067 59.0 32788 846.8
40 Pennsylvania 142514 58.8 128237 1006.3
41 Rhode Island 10926 51.7 9581 911.3
42 South Carolina 57155 61.4 42072 899.1
43 South Dakota 12104 78.1 7314 887.5
44 Tennessee 80371 62.6 60541 945.5
45 Texas 382727 69.9 168640 656.8
46 Utah 51465 83.1 15266 541.9
47 Vermont 6009 51.6 5433 867.3
48 Virginia 103013 61.7 60804 751.0
49 Washington 87463 63.5 49691 727.5
50 West Virginia 20827 61.3 21867 1178.6
51 Wisconsin 67295 61.8 48410 847.5
52 Wyoming 7572 69.5 4387 772.1
53 Puerto Rico 38900 51.8 29758 802.8

52
inst/extdata/election2012.csv

@ -0,0 +1,52 @@
state,Obama,Romney
AL,,9
AK,,3
AZ,,11
AR,,6
CA,55,
CO,9,
CT,7,
DE,3,
DC,3,
FL,29,
GA,,16
HI,4,
ID,,4
IL,20,
IN,,11
IA,6,
KS,,6
KY,,8
LA,,8
ME,4,
MD,10,
MA,11,
MI,16,
MN,10,
MS,,6
MO,,10
MT,,3
NE,,5
NV,6,
NH,4,
NJ,14,
NM,5,
NY,29,
NC,,15
ND,,3
OH,18,
OK,,7
OR,7,
PA,20,
RI,4,
SC,,9
SD,,3
TN,,11
TX,,38
UT,,6
VT,3,
VA,13,
WA,12,
WV,,5
WI,10,
WY,,3
1 state Obama Romney
2 AL 9
3 AK 3
4 AZ 11
5 AR 6
6 CA 55
7 CO 9
8 CT 7
9 DE 3
10 DC 3
11 FL 29
12 GA 16
13 HI 4
14 ID 4
15 IL 20
16 IN 11
17 IA 6
18 KS 6
19 KY 8
20 LA 8
21 ME 4
22 MD 10
23 MA 11
24 MI 16
25 MN 10
26 MS 6
27 MO 10
28 MT 3
29 NE 5
30 NV 6
31 NH 4
32 NJ 14
33 NM 5
34 NY 29
35 NC 15
36 ND 3
37 OH 18
38 OK 7
39 OR 7
40 PA 20
41 RI 4
42 SC 9
43 SD 3
44 TN 11
45 TX 38
46 UT 6
47 VT 3
48 VA 13
49 WA 12
50 WV 5
51 WI 10
52 WY 3

1
inst/extdata/wapostates.csv

File diff suppressed because one or more lines are too long

6
man/statebins.Rd

@ -6,7 +6,7 @@
\usage{
statebins(state_data, state_col = "state", value_col = "value",
dark_label = "black", light_label = "white", font_size = 3,
state_border_col = "white", state_border_size = 2,
state_border_col = "white", state_border_size = 2, round = FALSE,
ggplot2_scale_function = ggplot2::scale_fill_distiller, ...)
}
\arguments{
@ -26,8 +26,12 @@ when the algorithm determines labels should be inverted.}
\item{state_border_size}{border size}
\item{round}{rounded corners (default: \code{FALSE})}
\item{ggplot2_scale_function}{ggplot2 scale function to use. Defaults to \code{scale_fill_distiller}
since you're likely passing in continuous data when you shouldn't be :-)}
\item{...}{additional parameters to the scale function}
}
\value{
ggplot2 object

Loading…
Cancel
Save