Browse Source

Can use either a named vector or a data frame now

pull/36/head
boB Rudis 7 years ago
parent
commit
39fe83d6b4
No known key found for this signature in database GPG Key ID: 2A514A4997464560
  1. 4
      DESCRIPTION
  2. 4
      NEWS.md
  3. 31
      R/waffle.R
  4. 34
      README.Rmd
  5. 39
      README.md
  6. BIN
      README_files/figure-markdown_github/fig0-1.png
  7. 23
      man/waffle.Rd

4
DESCRIPTION

@ -1,8 +1,8 @@
Package: waffle
Type: Package
Title: Create Waffle Chart Visualizations in R
Version: 0.7.0
Date: 2017-01-07
Version: 0.8.0
Date: 2017-04-07
Authors@R: c(
person("Bob", "Rudis", email = "bob@rud.is", role = c("aut", "cre")),
person("Dave", "Gandy", role = "aut", comment = "Font Awesome")

4
NEWS.md

@ -1,5 +1,9 @@
### News
waffle 0.8.0
- `parts` can be now be a data frame. the first two columns will be uses as
names and values (respectively)
waffle 0.7.0
- yet more fixes for latest ggplot2 2.x
- made factor-level keeping a parameter (TRUE by default)

31
R/waffle.R

@ -1,13 +1,18 @@
#' Make waffle (square pie) charts
#'
#' Given a named vector, this function will return a ggplot object that
#' Given a named vector or a data frame, this function will return a ggplot object that
#' represents a waffle chart of the values. The individual values will be
#' summed up and each that will be the total number of squares in the grid.
#' You can perform appropriate value transformation ahead of time to get the
#' desired waffle layout/effect.
#'
#' If a data frame is used, the first two columns should contain the desired names
#' and the values, respectively.
#'
#' If the vector is not named or only partially named, capital letters will be
#' used instead. It is highly suggested that you limit the number of elements
#' used instead.
#'
#' It is highly suggested that you limit the number of elements
#' to plot, just like you should if you ever got wasted and decided that a
#' regular pie chart was a good thing to create and then decide to be totally
#' evil and make one to pollute this beautiful world of ours.
@ -18,14 +23,14 @@
#' If you specify a string (vs \code{FALSE}) to \code{use_glyph} the function
#' will map the input to a FontAwesome glyph name and use that glyph for the
#' tile instead of a block (making it more like an isotype pictogram than a
#' waffle chart). You'll need to actually install FontAwesome and use
#' waffle chart). You'll need to install FontAwesome and use
#' the \code{extrafont} package (\code{https://github.com/wch/extrafont}) to
#' be able to use the FontAwesome glyphs. Sizing is also up to the user since
#' fonts do not automatically scale with graphic resize.
#'
#' Glyph idea inspired by Ruben C. Arslan (@@_r_c_a)
#'
#' @param parts named vector of values to use for the chart
#' @param parts named vector of values or a data frame to use for the chart
#' @param rows number of rows of blocks
#' @param keep keep factor levels (i.e. for consistent legends across waffle plots)
#' @param xlab text for below the chart. Highly suggested this be used to
@ -46,8 +51,14 @@
#' @export
#' @examples
#' parts <- c(80, 30, 20, 10)
#' chart <- waffle(parts, rows=8)
#' # print(chart)
#' waffle(parts, rows=8)
#'
#' parts <- data.frame(
#' names = LETTERS[1:4],
#' vals = c(80, 30, 20, 10)
#' )
#'
#' waffle(parts, rows=8)
#'
#' # library(extrafont)
#' # waffle(parts, rows=8, use_glyph="shield")
@ -59,6 +70,11 @@ waffle <- function(parts, rows=10, keep=TRUE, xlab=NULL, title=NULL, colors=NA,
size=2, flip=FALSE, reverse=FALSE, equal=TRUE, pad=0,
use_glyph=FALSE, glyph_size=12, legend_pos="right") {
if (inherits(parts, "data.frame")) {
setNames(unlist(parts[,2], use.names = FALSE),
unlist(parts[,1], use.names = FALSE)) -> parts
}
# fill in any missing names
part_names <- names(parts)
if (length(part_names) < length(parts)) {
@ -78,7 +94,7 @@ waffle <- function(parts, rows=10, keep=TRUE, xlab=NULL, title=NULL, colors=NA,
if (reverse) parts_vec <- rev(parts_vec)
# setup the data frame for geom_rect
dat <- expand.grid(y=1:rows, x=seq_len(pad + (ceiling(sum(parts) / rows))))
dat <- expand.grid(y=1:rows, x=seq_len(pad + (ceiling(sum(parts) / rows))))
# add NAs if needed to fill in the "rectangle"
dat$value <- c(parts_vec, rep(NA, nrow(dat)-length(parts_vec)))
@ -133,7 +149,6 @@ dat <- expand.grid(y=1:rows, x=seq_len(pad + (ceiling(sum(parts) / rows))))
message("Font Awesome by Dave Gandy - http://fontawesome.io")
}
gg <- gg + geom_tile(color="#00000000", fill="#00000000", size=size, alpha=0, show.legend=FALSE)
gg <- gg + geom_point(aes(color=value), fill="#00000000", size=0, show.legend=TRUE)
gg <- gg + geom_text(aes(color=value,label=fontlab),

34
README.Rmd

@ -34,17 +34,29 @@ library(waffle)
# current verison
packageVersion("waffle")
```
### Basic example
# basic example
```{r fig0, fig.width=6, fig.height=2.5}
parts <- c(80, 30, 20, 10)
waffle(parts, rows=8)
```
### Use a data frame
```{r fig1, fig.width=6, fig.height=2.5}
parts <- data.frame(
names = LETTERS[1:4],
vals = c(80, 30, 20, 10)
)
waffle(parts, rows=8)
```
### Slightly more complex example
```{r s4}
# slightly more complex example
parts <- c(`Un-breached\nUS Population`=(318-11-79), `Premera`=11, `Anthem`=79)
```
@ -82,11 +94,14 @@ waffle(parts/10, rows=3, colors=c("#969696", "#1879bf", "#009bda"),
use_glyph="medkit", glyph_size=8)
```
```{r s5}
# replicating an old favourite
### Replicating an old favourite
# http://graphics8.nytimes.com/images/2008/07/20/business/20debtgraphic.jpg
# http://www.nytimes.com/2008/07/20/business/20debt.html
![](http://graphics8.nytimes.com/images/2008/07/20/business/20debtgraphic.jpg)
Via: <http://www.nytimes.com/2008/07/20/business/20debt.html>
```{r s5}
savings <- c(`Mortgage\n($84,911)`=84911, `Auto and\ntuition loans\n($14,414)`=14414,
`Home equity loans\n($10,062)`=10062, `Credit Cards\n($8,565)`=8565)
```
@ -105,10 +120,11 @@ waffle(savings/392, rows=7, size=0.5, legend_pos="bottom",
<span style="font-size:8pt">(1 square == $392)</span>
```{r s6}
# similar to but not exact
### More replication
Similar to <https://eagereyes.org/techniques/square-pie-charts>
# https://eagereyes.org/techniques/square-pie-charts
```{r s6}
professional <- c(`Male`=44, `Female (56%)`=56)
```

39
README.md

@ -23,20 +23,34 @@ library(waffle)
# current verison
packageVersion("waffle")
## [1] '0.7.0'
## [1] '0.8.0'
```
### Basic example
# basic example
``` r
parts <- c(80, 30, 20, 10)
waffle(parts, rows=8)
```
<img src="README_files/figure-markdown_github/fig0-1.png" width="576" />
### Use a data frame
``` r
parts <- data.frame(
names = LETTERS[1:4],
vals = c(80, 30, 20, 10)
)
waffle(parts, rows=8)
```
<img src="README_files/figure-markdown_github/fig1-1.png" width="576" />
### Slightly more complex example
``` r
# slightly more complex example
parts <- c(`Un-breached\nUS Population`=(318-11-79), `Premera`=11, `Anthem`=79)
```
@ -64,11 +78,13 @@ waffle(parts/10, rows=3, colors=c("#969696", "#1879bf", "#009bda"),
<img src="README_files/figure-markdown_github/ww2-1.png" width="576" />
``` r
# replicating an old favourite
### Replicating an old favourite
![](http://graphics8.nytimes.com/images/2008/07/20/business/20debtgraphic.jpg)
Via: <http://www.nytimes.com/2008/07/20/business/20debt.html>
# http://graphics8.nytimes.com/images/2008/07/20/business/20debtgraphic.jpg
# http://www.nytimes.com/2008/07/20/business/20debt.html
``` r
savings <- c(`Mortgage\n($84,911)`=84911, `Auto and\ntuition loans\n($14,414)`=14414,
`Home equity loans\n($10,062)`=10062, `Credit Cards\n($8,565)`=8565)
```
@ -82,10 +98,11 @@ waffle(savings/392, rows=7, size=0.5, legend_pos="bottom",
<span style="font-size:8pt">(1 square == $392)</span>
``` r
# similar to but not exact
### More replication
# https://eagereyes.org/techniques/square-pie-charts
Similar to <https://eagereyes.org/techniques/square-pie-charts>
``` r
professional <- c(`Male`=44, `Female (56%)`=56)
```
@ -157,7 +174,7 @@ library(waffle)
library(testthat)
date()
## [1] "Sat Jan 7 08:05:00 2017"
## [1] "Fri Apr 7 06:59:41 2017"
test_dir("tests/")
## testthat results ========================================================================================================

BIN
README_files/figure-markdown_github/fig0-1.png

Binary file not shown.

After

Width:  |  Height:  |  Size: 22 KiB

23
man/waffle.Rd

@ -9,7 +9,7 @@ waffle(parts, rows = 10, keep = TRUE, xlab = NULL, title = NULL,
pad = 0, use_glyph = FALSE, glyph_size = 12, legend_pos = "right")
}
\arguments{
\item{parts}{named vector of values to use for the chart}
\item{parts}{named vector of values or a data frame to use for the chart}
\item{rows}{number of rows of blocks}
@ -42,15 +42,20 @@ ggsave or knitr to control output sizes (or manually sizing the chart)}
\item{legend_pos}{position of legend}
}
\description{
Given a named vector, this function will return a ggplot object that
Given a named vector or a data frame, this function will return a ggplot object that
represents a waffle chart of the values. The individual values will be
summed up and each that will be the total number of squares in the grid.
You can perform appropriate value transformation ahead of time to get the
desired waffle layout/effect.
}
\details{
If a data frame is used, the first two columns should contain the desired names
and the values, respectively.
If the vector is not named or only partially named, capital letters will be
used instead. It is highly suggested that you limit the number of elements
used instead.
It is highly suggested that you limit the number of elements
to plot, just like you should if you ever got wasted and decided that a
regular pie chart was a good thing to create and then decide to be totally
evil and make one to pollute this beautiful world of ours.
@ -61,7 +66,7 @@ exporting to another program for use/display.
If you specify a string (vs \code{FALSE}) to \code{use_glyph} the function
will map the input to a FontAwesome glyph name and use that glyph for the
tile instead of a block (making it more like an isotype pictogram than a
waffle chart). You'll need to actually install FontAwesome and use
waffle chart). You'll need to install FontAwesome and use
the \code{extrafont} package (\code{https://github.com/wch/extrafont}) to
be able to use the FontAwesome glyphs. Sizing is also up to the user since
fonts do not automatically scale with graphic resize.
@ -70,8 +75,14 @@ Glyph idea inspired by Ruben C. Arslan (@_r_c_a)
}
\examples{
parts <- c(80, 30, 20, 10)
chart <- waffle(parts, rows=8)
# print(chart)
waffle(parts, rows=8)
parts <- data.frame(
names = LETTERS[1:4],
vals = c(80, 30, 20, 10)
)
waffle(parts, rows=8)
# library(extrafont)
# waffle(parts, rows=8, use_glyph="shield")

Loading…
Cancel
Save