Browse Source

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

master
boB Rudis 6 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` * Added support for `VI`/`Virgin Islands`
* Supports auto-computation of when to use light & dark label colors * Supports auto-computation of when to use light & dark label colors
* New `theme_statebins()` * 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. * Completely re-designed the interface and will not be providing backwards-compatibility.
# statebins 1.2.2 # statebins 1.2.2

59
R/geom-rrect.r

@ -1,5 +1,6 @@
geom_rrect <- function(mapping = NULL, data = NULL, geom_rrect <- function(mapping = NULL, data = NULL,
stat = "identity", position = "identity", stat = "identity", position = "identity",
radius = grid::unit(6, "pt"),
..., ...,
na.rm = FALSE, na.rm = FALSE,
show.legend = NA, show.legend = NA,
@ -13,6 +14,7 @@ geom_rrect <- function(mapping = NULL, data = NULL,
show.legend = show.legend, show.legend = show.legend,
inherit.aes = inherit.aes, inherit.aes = inherit.aes,
params = list( params = list(
radius = radius,
na.rm = na.rm, na.rm = na.rm,
... ...
) )
@ -20,33 +22,44 @@ geom_rrect <- function(mapping = NULL, data = NULL,
} }
GeomRrect <- ggplot2::ggproto("GeomRrect", ggplot2::Geom, 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"), required_aes = c("xmin", "xmax", "ymin", "ymax"),
draw_panel = function(self, data, panel_params, coord) { draw_panel = function(self, data, panel_params, coord,
coords <- coord$transform(data, panel_params) radius = grid::unit(6, "pt")) {
lapply(1:length(coords$xmin), function(i) {
ggname("geom_rrect", grid::roundrectGrob( coords <- coord$transform(data, panel_params)
coords$xmin[i], coords$ymax[i],
width = (coords$xmax[i] - coords$xmin[i]), lapply(1:length(coords$xmin), function(i) {
height = (coords$ymax[i] - coords$ymin)[i],
r=grid::unit(0.3, "snpc"), grid::roundrectGrob(
default.units = "native", coords$xmin[i], coords$ymax[i],
just = c("left", "top"), width = (coords$xmax[i] - coords$xmin[i]),
gp = grid::gpar( height = (coords$ymax[i] - coords$ymin)[i],
col = coords$colour[i], r = radius,
fill = alpha(coords$fill[i], coords$alpha[i]), default.units = "native",
lwd = coords$size[i] * .pt, just = c("left", "top"),
lty = coords$linetype[i], gp = grid::gpar(
lineend = "butt" col = coords$colour[i],
) fill = alpha(coords$fill[i], coords$alpha[i]),
)) lwd = coords$size[i] * .pt,
}) -> gl lty = coords$linetype[i],
lineend = "butt"
do.call(grid::gList, gl) )
)
}) -> gl
grobs <- do.call(grid::gList, gl)
ggname("geom_rrect", grid::grobTree(children = grobs))
}, },
draw_key = ggplot2::draw_key_polygon draw_key = ggplot2::draw_key_polygon
) )

11
R/geom-rtile.R

@ -1,10 +1,11 @@
geom_rtile <- function(mapping = NULL, data = NULL, geom_rtile <- function(mapping = NULL, data = NULL,
stat = "identity", position = "identity", stat = "identity", position = "identity",
radius = grid::unit(6, "pt"),
..., ...,
na.rm = FALSE, na.rm = FALSE,
show.legend = NA, show.legend = NA,
inherit.aes = TRUE) { inherit.aes = TRUE) {
layer( ggplot2::layer(
data = data, data = data,
mapping = mapping, mapping = mapping,
stat = stat, stat = stat,
@ -13,6 +14,7 @@ geom_rtile <- function(mapping = NULL, data = NULL,
show.legend = show.legend, show.legend = show.legend,
inherit.aes = inherit.aes, inherit.aes = inherit.aes,
params = list( params = list(
radius = radius,
na.rm = na.rm, na.rm = na.rm,
... ...
) )
@ -20,6 +22,7 @@ geom_rtile <- function(mapping = NULL, data = NULL,
} }
GeomRtile <- ggplot2::ggproto("GeomRtile", GeomRrect, GeomRtile <- ggplot2::ggproto("GeomRtile", GeomRrect,
extra_params = c("na.rm", "width", "height"), extra_params = c("na.rm", "width", "height"),
setup_data = function(data, params) { 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, default_aes = ggplot2::aes(
alpha = NA), fill = "grey20", colour = NA, size = 0.1, linetype = 1, alpha = NA
),
required_aes = c("x", "y"), required_aes = c("x", "y"),
draw_key = ggplot2::draw_key_polygon 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_col default "\code{white}" - this creates the "spaces" between boxes
#' @param state_border_size border size #' @param state_border_size border size
#' @param round rounded corners (default: `FALSE`) #' @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` #' @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 :-) #' since you're likely passing in continuous data when you shouldn't be :-)
#' @param ... additional parameters to the scale function #' @param ... additional parameters to the scale function
@ -43,7 +45,7 @@ statebins <- function(state_data,
state_col="state", value_col="value", state_col="state", value_col="value",
dark_label = "black", light_label = "white", font_size=3, 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, round = FALSE, radius = grid::unit(6, "pt"),
ggplot2_scale_function=ggplot2::scale_fill_distiller, ggplot2_scale_function=ggplot2::scale_fill_distiller,
...) { ...) {
@ -63,7 +65,7 @@ statebins <- function(state_data,
gg <- ggplot() gg <- ggplot()
if (round) { 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), aes_string(x = "col", y = "row", fill = value_col),
color = state_border_col, size = state_border_size) color = state_border_col, size = state_border_size)
} else { } else {

12
README.Rmd

@ -116,6 +116,10 @@ mutate(election_2012, value = ifelse(is.na(Obama), "Romney", "Obama")) %>%
### Rounded rects! ### Rounded rects!
You can pass in a `grid::units()` call for the `radius` parameter.
Slight curves:
```{r rounded} ```{r rounded}
data(USArrests) data(USArrests)
@ -124,6 +128,14 @@ statebins(USArrests, value_col="Assault", name = "Assault", round=TRUE) +
theme_statebins(legend_position="right") 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" ### All the "states"
`statebins` now has PR, VI & NYC (by name or abbreviation) so you can use them, too: `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") packageVersion("statebins")
``` ```
## [1] '1.3.0' ## [1] '1.3.1'
### The original wapo data ### The original wapo data
@ -129,6 +129,10 @@ mutate(election_2012, value = ifelse(is.na(Obama), "Romney", "Obama")) %>%
### Rounded rects\! ### Rounded rects\!
You can pass in a `grid::units()` call for the `radius` parameter.
Slight curves:
``` r ``` r
data(USArrests) 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" /> <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” ### All the “states”
`statebins` now has PR, VI & NYC (by name or abbreviation) so you can `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", statebins(state_data, state_col = "state", value_col = "value",
dark_label = "black", light_label = "white", font_size = 3, dark_label = "black", light_label = "white", font_size = 3,
state_border_col = "white", state_border_size = 2, round = FALSE, state_border_col = "white", state_border_size = 2, round = FALSE,
radius = grid::unit(6, "pt"),
ggplot2_scale_function = ggplot2::scale_fill_distiller, ...) ggplot2_scale_function = ggplot2::scale_fill_distiller, ...)
} }
\arguments{ \arguments{
@ -28,6 +29,9 @@ when the algorithm determines labels should be inverted.}
\item{round}{rounded corners (default: \code{FALSE})} \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} \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 :-)} since you're likely passing in continuous data when you shouldn't be :-)}

Loading…
Cancel
Save