Browse Source

preserves factor levels

cran
boB Rudis 8 years ago
parent
commit
f5485de61a
  1. 4
      DESCRIPTION
  2. 33
      R/waffle.R
  3. 275
      R/waffle.html
  4. 17
      README.Rmd
  5. 39
      README.md
  6. BIN
      README_files/figure-markdown_github/f5-1.png
  7. BIN
      README_files/figure-markdown_github/f8-1.png
  8. BIN
      README_files/figure-markdown_github/fct-1.png
  9. BIN
      README_files/figure-markdown_github/fig1-1.png
  10. BIN
      README_files/figure-markdown_github/fig2-1.png
  11. BIN
      README_files/figure-markdown_github/fig3-1.png
  12. BIN
      README_files/figure-markdown_github/fig4a-1.png

4
DESCRIPTION

@ -1,8 +1,8 @@
Package: waffle
Type: Package
Title: Create Waffle Chart Visualizations in R
Version: 0.5.1
Date: 2016-02-02
Version: 0.6.0.9000
Date: 2016-04-20
Author: Bob Rudis <bob@rudis.net>
Maintainer: Bob Rudis <bob@rudis.net>
Description: Square pie charts (a.k.a. waffle charts) can be used

33
R/waffle.R

@ -58,41 +58,38 @@ x <- y <- value <- NULL
#' # print(chart)
waffle <- function(parts, rows=10, 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") {
use_glyph=FALSE, glyph_size=12, legend_pos="right") {
# fill in any missing names
part_names <- names(parts)
if (length(part_names) < length(parts)) {
part_names <- c(part_names, LETTERS[1:length(parts)-length(part_names)])
}
# use Set2 if no colors are specified
names(parts) <- part_names
if (all(is.na(colors))) {
colors <- brewer.pal(length(parts), "Set2")
}
# use Set2 if no colors are specified
if (all(is.na(colors))) colors <- suppressWarnings(brewer.pal(length(parts), "Set2"))
# make one big vector of all the bits
parts_vec <- unlist(sapply(1:length(parts), function(i) {
rep(LETTERS[i+1], parts[i])
rep(names(parts)[i], parts[i])
}))
if (reverse) { parts_vec <- rev(parts_vec) }
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))))
# add NAs if needed to fill in the "rectangle"
dat$value <- c(parts_vec, rep(NA, nrow(dat)-length(parts_vec)))
if(!inherits(use_glyph, "logical")){
fontlab <- rep(fa_unicode[use_glyph],length(unique(parts_vec)))
dat$fontlab <- c(fontlab[as.numeric(factor(parts_vec))], rep(NA, nrow(dat)-length(parts_vec)))
}
dat$value <- factor(dat$value, levels=part_names)
if (flip) {
gg <- ggplot(dat, aes(x=y, y=x))
} else {
@ -108,8 +105,11 @@ waffle <- function(parts, rows=10, xlab=NULL, title=NULL, colors=NA,
gg <- gg + geom_tile(aes(fill=value), color="white", size=size)
gg <- gg + scale_fill_manual(name="",
values=colors,
labels=part_names)
gg <- gg + guides(fill=guide_legend(override.aes=list(colour=NULL)))
labels=part_names,
drop=FALSE)
gg <- gg + guides(fill=guide_legend(override.aes=list(colour="#00000000")))
gg <- gg + theme(legend.background=element_rect(fill="#00000000", color="#00000000"))
gg <- gg + theme(legend.key=element_rect(fill="#00000000", color="#00000000"))
} else {
@ -134,10 +134,11 @@ waffle <- function(parts, rows=10, xlab=NULL, title=NULL, colors=NA,
family="FontAwesome", size=glyph_size, show.legend=FALSE)
gg <- gg + scale_color_manual(name="",
values=colors,
labels=part_names)
labels=part_names,
drop=FALSE)
gg <- gg + guides(color=guide_legend(override.aes=list(shape=15, size=7)))
gg <- gg + theme(legend.background=element_rect(fill=NA, color=NA))
gg <- gg + theme(legend.key=element_rect(color=NA))
gg <- gg + theme(legend.background=element_rect(fill="#00000000", color="#00000000"))
gg <- gg + theme(legend.key=element_rect(color="#00000000"))
}

275
R/waffle.html

File diff suppressed because one or more lines are too long

17
README.Rmd

@ -1,10 +1,5 @@
---
title: "README"
author: "Bob Rudis"
date: "`r Sys.Date()`"
output:
md_document:
variant: markdown_github
output: rmarkdown::github_document
---
[![Build Status](https://travis-ci.org/hrbrmstr/waffle.svg)](https://travis-ci.org/hrbrmstr/waffle)
@ -31,6 +26,7 @@ The following functions are implemented:
- Version `0.4` released - added `use_glyph` and `glpyh_size` to `waffle` so you can now make isotype pictograms
- Version `0.5` released - new & improved ggplot2 compatibility
- Version `0.5.1` released - even moar improved ggplot2 compatibility
- Version `0.6.0` - keep factor levels; improve default aesthetics
### Installation
@ -131,6 +127,15 @@ professional <- c(`Male`=44, `Female (56%)`=56)
waffle(professional, rows=10, size=0.5, colors=c("#af9139", "#544616"))
```
### Keeps factor levels now
```{r fct, fig.height=3, fig.width=6, message=FALSE, warning=FALSE}
gridExtra::grid.arrange(
waffle(c(thing1=0, thing2=100), rows=5),
waffle(c(thing1=25, thing2=75), rows=5)
)
```
**Professional Workforce Makeup**

39
README.md

@ -1,3 +1,4 @@
[![Build Status](https://travis-ci.org/hrbrmstr/waffle.svg)](https://travis-ci.org/hrbrmstr/waffle) [![Project Status: Active - The project has reached a stable, usable state and is being actively developed.](http://www.repostatus.org/badges/0.1.0/active.svg)](http://www.repostatus.org/#active) [![CRAN\_Status\_Badge](http://www.r-pkg.org/badges/version/waffle)](http://cran.r-project.org/web/packages/waffle) ![downloads](http://cranlogs.r-pkg.org/badges/grand-total/waffle)
waffle is a package to make waffle charts (square pie charts)
@ -19,15 +20,12 @@ The following functions are implemented:
- Version `0.4` released - added `use_glyph` and `glpyh_size` to `waffle` so you can now make isotype pictograms
- Version `0.5` released - new & improved ggplot2 compatibility
- Version `0.5.1` released - even moar improved ggplot2 compatibility
- Version `0.6.0` - keep factor levels; improve default aesthetics
### Installation
``` r
## github dev version
## devtools::install_github("hrbrmstr/waffle")
## CRAN
install.packages("waffle")
install.pacakges("hrbrmstr/waffle")
```
### Usage
@ -39,7 +37,7 @@ library(waffle)
packageVersion("waffle")
```
## [1] '0.5.1'
## [1] '0.6.0.9000'
``` r
# basic example
@ -50,7 +48,7 @@ parts <- c(80, 30, 20, 10)
waffle(parts, rows=8)
```
![](README_files/figure-markdown_github/fig1-1.png)<!-- -->
![](README_files/figure-markdown_github/fig1-1.png)
``` r
# slightly more complex example
@ -61,7 +59,7 @@ parts <- c(`Un-breached\nUS Population`=(318-11-79), `Premera`=11, `Anthem`=79)
waffle(parts, rows=8, size=1, colors=c("#969696", "#1879bf", "#009bda"))
```
**Health records breaches as fraction of US Population** ![](README_files/figure-markdown_github/fig2-1.png)<!-- -->
**Health records breaches as fraction of US Population** ![](README_files/figure-markdown_github/fig2-1.png)
<smaller>One square == 1m ppl</smaller>
@ -69,7 +67,7 @@ waffle(parts, rows=8, size=1, colors=c("#969696", "#1879bf", "#009bda"))
waffle(parts/10, rows=3, colors=c("#969696", "#1879bf", "#009bda"))
```
**Health records breaches as fraction of US Population** ![](README_files/figure-markdown_github/fig3-1.png)<!-- -->
**Health records breaches as fraction of US Population** ![](README_files/figure-markdown_github/fig3-1.png)
<smaller>(One square == 10m ppl)</smaller>
@ -79,7 +77,7 @@ waffle(parts/10, rows=3, colors=c("#969696", "#1879bf", "#009bda"),
use_glyph="medkit", size=8)
```
![](README_files/figure-markdown_github/ww2-1.png)<!-- -->
![](README_files/figure-markdown_github/ww2-1.png)
``` r
# replicating an old favourite
@ -95,7 +93,7 @@ waffle(savings/392, rows=7, size=0.5,
colors=c("#c7d4b6", "#a3aabd", "#a0d0de", "#97b5cf"))
```
\*Average Household Savings Each Year\*\* ![](README_files/figure-markdown_github/fig4a-1.png)<!-- -->
\*Average Household Savings Each Year\*\* ![](README_files/figure-markdown_github/fig4a-1.png)
<smaller> (1 square == $392)</smaller>
@ -110,9 +108,20 @@ professional <- c(`Male`=44, `Female (56%)`=56)
waffle(professional, rows=10, size=0.5, colors=c("#af9139", "#544616"))
```
### Keeps factor levels now
``` r
gridExtra::grid.arrange(
waffle(c(thing1=0, thing2=100), rows=5),
waffle(c(thing1=25, thing2=75), rows=5)
)
```
![](README_files/figure-markdown_github/fct-1.png)
**Professional Workforce Makeup**
![](README_files/figure-markdown_github/f5-1.png)<!-- -->
![](README_files/figure-markdown_github/f5-1.png)
Iron example (left-align & padding for multiple plots)
@ -139,7 +148,7 @@ C <- waffle(stan.adult.1997/2, rows=7, size=0.5,
iron(A, B, C)
```
![](README_files/figure-markdown_github/f8-1.png)<!-- -->
![](README_files/figure-markdown_github/f8-1.png)
### Test Results
@ -150,7 +159,7 @@ library(testthat)
date()
```
## [1] "Mon Feb 1 16:43:10 2016"
## [1] "Wed Apr 20 09:59:11 2016"
``` r
test_dir("tests/")
@ -158,3 +167,5 @@ test_dir("tests/")
## testthat results ========================================================================================================
## OK: 1 SKIPPED: 0 FAILED: 0
##
## DONE ===================================================================================================================

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

Binary file not shown.

Before

Width:  |  Height:  |  Size: 11 KiB

After

Width:  |  Height:  |  Size: 11 KiB

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

Binary file not shown.

Before

Width:  |  Height:  |  Size: 63 KiB

After

Width:  |  Height:  |  Size: 63 KiB

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

Binary file not shown.

After

Width:  |  Height:  |  Size: 17 KiB

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

Binary file not shown.

Before

Width:  |  Height:  |  Size: 13 KiB

After

Width:  |  Height:  |  Size: 13 KiB

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

Binary file not shown.

Before

Width:  |  Height:  |  Size: 15 KiB

After

Width:  |  Height:  |  Size: 15 KiB

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

Binary file not shown.

Before

Width:  |  Height:  |  Size: 9.8 KiB

After

Width:  |  Height:  |  Size: 9.7 KiB

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

Binary file not shown.

Before

Width:  |  Height:  |  Size: 26 KiB

After

Width:  |  Height:  |  Size: 26 KiB

Loading…
Cancel
Save