Browse Source

More geom cleanup and add user-defined radius for rounded rects)

master
boB Rudis 5 years ago
parent
commit
64b703812f
No known key found for this signature in database GPG Key ID: 1D7529BE14E2BBA9
  1. 2
      NEWS.md
  2. 59
      R/geom-rrect.r
  3. 11
      R/geom-rtile.R
  4. 6
      R/statebins.R
  5. 12
      README.Rmd
  6. 16
      README.md
  7. BIN
      README_files/figure-gfm/rounded-1.png
  8. BIN
      README_files/figure-gfm/rounded2-1.png
  9. 4
      man/statebins.Rd

2
NEWS.md

@ -2,7 +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!
* Rounded rectangle support (with user-defined corner radius)!
* Completely re-designed the interface and will not be providing backwards-compatibility.
# statebins 1.2.2

59
R/geom-rrect.r

@ -1,5 +1,6 @@
geom_rrect <- function(mapping = NULL, data = NULL,
stat = "identity", position = "identity",
radius = grid::unit(6, "pt"),
...,
na.rm = FALSE,
show.legend = NA,
@ -13,6 +14,7 @@ geom_rrect <- function(mapping = NULL, data = NULL,
show.legend = show.legend,
inherit.aes = inherit.aes,
params = list(
radius = radius,
na.rm = na.rm,
...
)
@ -20,33 +22,44 @@ geom_rrect <- function(mapping = NULL, data = NULL,
}
GeomRrect <- ggplot2::ggproto("GeomRrect", ggplot2::Geom,
default_aes = aes(colour = NA, fill = "grey35", size = 0.5, linetype = 1,
alpha = NA),
default_aes = ggplot2::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=grid::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_panel = function(self, data, panel_params, coord,
radius = grid::unit(6, "pt")) {
coords <- coord$transform(data, panel_params)
lapply(1:length(coords$xmin), function(i) {
grid::roundrectGrob(
coords$xmin[i], coords$ymax[i],
width = (coords$xmax[i] - coords$xmin[i]),
height = (coords$ymax[i] - coords$ymin)[i],
r = radius,
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
grobs <- do.call(grid::gList, gl)
ggname("geom_rrect", grid::grobTree(children = grobs))
},
draw_key = ggplot2::draw_key_polygon
)

11
R/geom-rtile.R

@ -1,10 +1,11 @@
geom_rtile <- function(mapping = NULL, data = NULL,
stat = "identity", position = "identity",
radius = grid::unit(6, "pt"),
...,
na.rm = FALSE,
show.legend = NA,
inherit.aes = TRUE) {
layer(
ggplot2::layer(
data = data,
mapping = mapping,
stat = stat,
@ -13,6 +14,7 @@ geom_rtile <- function(mapping = NULL, data = NULL,
show.legend = show.legend,
inherit.aes = inherit.aes,
params = list(
radius = radius,
na.rm = na.rm,
...
)
@ -20,6 +22,7 @@ geom_rtile <- function(mapping = NULL, data = NULL,
}
GeomRtile <- ggplot2::ggproto("GeomRtile", GeomRrect,
extra_params = c("na.rm", "width", "height"),
setup_data = function(data, params) {
@ -32,10 +35,12 @@ GeomRtile <- ggplot2::ggproto("GeomRtile", GeomRrect,
)
},
default_aes = aes(fill = "grey20", colour = NA, size = 0.1, linetype = 1,
alpha = NA),
default_aes = ggplot2::aes(
fill = "grey20", colour = NA, size = 0.1, linetype = 1, alpha = NA
),
required_aes = c("x", "y"),
draw_key = ggplot2::draw_key_polygon
)

6
R/statebins.R

@ -28,6 +28,8 @@
#' @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 radius if `round` is `TRUE` then use `grid::unit` to specify the corner radius.
#' Default is `grid::unit(6, "pt")` if using rounded corners.
#' @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
@ -43,7 +45,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,
round = FALSE, radius = grid::unit(6, "pt"),
ggplot2_scale_function=ggplot2::scale_fill_distiller,
...) {
@ -63,7 +65,7 @@ statebins <- function(state_data,
gg <- ggplot()
if (round) {
gg <- gg + geom_rtile(data = st.dat,
gg <- gg + geom_rtile(data = st.dat, radius = radius,
aes_string(x = "col", y = "row", fill = value_col),
color = state_border_col, size = state_border_size)
} else {

12
README.Rmd

@ -116,6 +116,10 @@ mutate(election_2012, value = ifelse(is.na(Obama), "Romney", "Obama")) %>%
### Rounded rects!
You can pass in a `grid::units()` call for the `radius` parameter.
Slight curves:
```{r rounded}
data(USArrests)
@ -124,6 +128,14 @@ statebins(USArrests, value_col="Assault", name = "Assault", round=TRUE) +
theme_statebins(legend_position="right")
```
Circles!
```{r rounded2}
statebins(USArrests, value_col="Assault", name = "Assault", round=TRUE,
radius=grid::unit(16, "pt"), palette="Reds", direction=1) +
theme_statebins(legend_position="right")
```
### All the "states"
`statebins` now has PR, VI & NYC (by name or abbreviation) so you can use them, too:

16
README.md

@ -40,7 +40,7 @@ library(tidyverse)
packageVersion("statebins")
```
## [1] '1.3.0'
## [1] '1.3.1'
### The original wapo data
@ -129,6 +129,10 @@ mutate(election_2012, value = ifelse(is.na(Obama), "Romney", "Obama")) %>%
### Rounded rects\!
You can pass in a `grid::units()` call for the `radius` parameter.
Slight curves:
``` r
data(USArrests)
@ -139,6 +143,16 @@ statebins(USArrests, value_col="Assault", name = "Assault", round=TRUE) +
<img src="README_files/figure-gfm/rounded-1.png" width="672" />
Circles\!
``` r
statebins(USArrests, value_col="Assault", name = "Assault", round=TRUE,
radius=grid::unit(16, "pt"), palette="Reds", direction=1) +
theme_statebins(legend_position="right")
```
<img src="README_files/figure-gfm/rounded2-1.png" width="672" />
### All the “states”
`statebins` now has PR, VI & NYC (by name or abbreviation) so you can

BIN
README_files/figure-gfm/rounded-1.png

Binary file not shown.

Before

Width:  |  Height:  |  Size: 118 KiB

After

Width:  |  Height:  |  Size: 98 KiB

BIN
README_files/figure-gfm/rounded2-1.png

Binary file not shown.

After

Width:  |  Height:  |  Size: 148 KiB

4
man/statebins.Rd

@ -7,6 +7,7 @@
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, round = FALSE,
radius = grid::unit(6, "pt"),
ggplot2_scale_function = ggplot2::scale_fill_distiller, ...)
}
\arguments{
@ -28,6 +29,9 @@ when the algorithm determines labels should be inverted.}
\item{round}{rounded corners (default: \code{FALSE})}
\item{radius}{if \code{round} is \code{TRUE} then use \code{grid::unit} to specify the corner radius.
Default is \code{grid::unit(6, "pt")} if using rounded corners.}
\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 :-)}

Loading…
Cancel
Save