• Stars
    star
    747
  • Rank 60,741 (Top 2 %)
  • Language
    R
  • Created almost 10 years ago
  • Updated about 1 year ago

Reviews

There are no reviews yet. Be the first to send feedback to the community and the maintainers!

Repository Details

🍁 Make waffle (square pie) charts in R

πŸ§‡ waffle

Create Waffle Chart Visualizations

Description

Square pie charts (a.k.a. waffle charts) can be used to communicate parts of a whole for categorical quantities. To emulate the percentage view of a pie chart, a 10x10 grid should be used with each square representing 1% of the total. Modern uses of waffle charts do not necessarily adhere to this rule and can be created with a grid of any rectangular shape. Best practices suggest keeping the number of categories small, just as should be done when creating pie charts. Tools are provided to create waffle charts as well as stitch them together, and to use glyphs for making isotype pictograms.

It uses ggplot2 and returns a ggplot2 object.

What’s Inside the Tin

The following functions are implemented:

  • waffle: Make waffle (square pie) charts

  • draw_key_pictogram: Legend builder for pictograms

  • fa_grep: Search Font Awesome glyph names for a pattern

  • fa_list: List all Font Awesome glyphs

  • fa5_brand: Font Awesome 5 Brand

  • fa5_solid: Font Awesome 5 Solid

  • geom_pictogram: Pictogram Geom

  • geom_waffle: Waffle (Square pie chart) Geom

  • install_fa_fonts: Install Font Awesome 5 Fonts

  • iron: Veritical, left-aligned layout for waffle plots

  • scale_label_pictogram: Used with geom_pictogram() to map Font Awesome fonts to labels

  • theme_enhance_waffle: Waffle chart theme cruft remover that can be used with any other theme

Installation

install.packages("waffle") # NOTE: CRAN version is 0.7.0
# or
remotes::install_github("hrbrmstr/waffle")

NOTE: To use the β€˜remotes’ install options you will need to have the {remotes} package installed.

Usage

library(waffle)
library(magrittr)
library(hrbrthemes)
library(ggplot2)
library(dplyr)
library(waffle)

# current verison
packageVersion("waffle")
## [1] '1.0.2'

Some new bits up first

data.frame(
  parts = factor(rep(month.abb[1:3], 3), levels=month.abb[1:3]),
  vals = c(10, 20, 30, 6, 14, 40, 30, 20, 10),
  col = rep(c("navy", "black", "maroon"), 3),
  fct = c(
    rep("Thing 1", 3),
    rep("Thing 2", 3),
    rep("Thing 3", 3)
  )
) -> xdf

xdf %>%
  count(parts, wt = vals) %>%
  ggplot(
    aes(fill = parts, values = n)
  ) +
  geom_waffle(
    n_rows = 20,
    size = 0.33, 
    colour = "white",
    flip = TRUE
  ) +
  scale_fill_manual(
    name = NULL,
    values = c("#a40000", "#c68958", "#ae6056"),
    labels = c("Fruit", "Sammich", "Pizza")
  ) +
  coord_equal() +
  theme_ipsum_rc(grid="") +
  theme_enhance_waffle()

xdf %>%
  count(parts, wt = vals) %>%
  ggplot(
    aes(label = parts, values = n)
  ) +
  geom_pictogram(
    n_rows = 10, 
    aes(colour = parts), 
    flip = TRUE, 
    make_proportional = TRUE
  ) +
  scale_color_manual(
    name = NULL,
    values = c("#a40000", "#c68958", "#ae6056"),
    labels = c("Fruit", "Sammich", "Pizza")
  ) +
  scale_label_pictogram(
    name = NULL,
    values = c("apple-alt", "bread-slice", "pizza-slice"),
    labels = c("Fruit", "Sammich", "Pizza")
  ) +
  coord_equal() +
  theme_ipsum_rc(grid="") +
  theme_enhance_waffle() +
  theme(
    legend.key.height = unit(2.25, "line"),
    legend.text = element_text(size = 10, hjust = 0, vjust = 0.75)
  ) 

xdf %>%
  count(parts, wt = vals) %>%
  ggplot(
    aes(label = parts, values = n)
  ) +
  geom_pictogram(
    n_rows = 20, 
    size = 6, 
    aes(colour = parts), 
    flip = TRUE,
    family = "FontAwesome5Brands-Regular"
  ) +
  scale_color_manual(
    name = NULL,
    values = c("#073f9c", "black", "#f34323"),
    labels = c("BitBucket", "GitHub", "Other")
  ) +
  scale_label_pictogram(
    name = NULL,
    values = c("bitbucket", "github", "git-alt"),
    labels = c("BitBucket", "GitHub", "Other")
  ) +
  coord_equal() +
  theme_ipsum_rc(grid="") +
  theme_enhance_waffle() +
  theme(
    legend.text = element_text(hjust = 0, vjust = 1)
  )

Geoms!

library(hrbrthemes)
library(waffle)
library(tidyverse)

tibble(
  parts = factor(rep(month.abb[1:3], 3), levels=month.abb[1:3]),
  values = c(10, 20, 30, 6, 14, 40, 30, 20, 10),
  fct = c(rep("Thing 1", 3), rep("Thing 2", 3), rep("Thing 3", 3))
) -> xdf

ggplot(
  data = xdf, 
  aes(fill=parts, values=values)
) +
  geom_waffle(
    color = "white", 
    size = 1.125, 
    n_rows = 6
  ) +
  facet_wrap(~fct, ncol=1) +
  scale_x_discrete(
    expand = c(0,0,0,0)
  ) +
  scale_y_discrete(
    expand = c(0,0,0,0)
  ) +
  ggthemes::scale_fill_tableau(name=NULL) +
  coord_equal() +
  labs(
    title = "Faceted Waffle Geoms"
  ) +
  theme_ipsum_rc(grid="") +
  theme_enhance_waffle()

Waffle Bar Charts with scales!

library(dplyr)
library(waffle)

storms %>% 
  filter(year >= 2010) %>% 
  count(year, status) -> storms_df

ggplot(
  data = storms_df, 
  aes(fill = status, values = n)
) +
  geom_waffle(
    color = "white", 
    size = .25, 
    n_rows = 10, 
    flip = TRUE
  ) +
  facet_wrap(
    ~year, 
    nrow = 1, 
    strip.position = "bottom"
  ) +
  scale_x_discrete() + 
  scale_y_continuous(
    labels = function(x) x * 10, # make this multiplier the same as n_rows
    expand = c(0,0)
  ) +
  ggthemes::scale_fill_tableau(name=NULL) +
  coord_equal() +
  labs(
    x = "Year", y = "Count",
    title = "Faceted Waffle Bar Chart",
    subtitle = "{dplyr} storms data"
  ) +
  theme_minimal(
    base_family = "Roboto Condensed"
  ) +
  theme(
    panel.grid = element_blank(), 
    axis.ticks.y = element_line()
  ) +
  guides(
    fill = guide_legend(reverse = TRUE)
  )

Basic example

parts <- c(80, 30, 20, 10)
waffle(parts, rows = 8)

Use a data frame

parts <- data.frame(
  names = LETTERS[1:4],
  vals = c(80, 30, 20, 10)
)

waffle(parts, rows = 8)

Slightly more complex example

c(
  `Un-breached\nUS Population` = (318 - 11 - 79), 
  `Premera` = 11, 
  `Anthem` = 79
) -> parts
waffle(
  parts = parts, 
  rows = 8, 
  size = 1, 
  colors = c("#969696", "#1879bf", "#009bda"),
  legend_pos = "bottom"
)

Health records breaches as fraction of US Population

One square == 1m ppl

waffle(
  parts = parts / 10, 
  rows = 3,
  colors = c("#969696", "#1879bf", "#009bda")
)

Health records breaches as fraction of US Population

(One square == 10m ppl)

library(extrafont)

waffle(
  parts = parts / 10, 
  rows = 3, 
  colors = c("#969696", "#1879bf", "#009bda"),
  use_glyph = "medkit", 
  size = 8
) + 
  expand_limits(
    y = c(0, 4)
  )

Replicating an old favourite

Via: https://www.nytimes.com/2008/07/20/business/20debt.html

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
) -> savings
waffle(
  parts = savings / 392, 
  rows = 7, 
  size = 0.5, 
  legend_pos = "bottom",
  colors = c("#c7d4b6", "#a3aabd", "#a0d0de", "#97b5cf")
)

Average Household Savings Each Year

(1 square == $392)

More replication

Similar to https://eagereyes.org/techniques/square-pie-charts

professional <- c(`Male` = 44, `Female (56%)` = 56)
waffle(
  parts = professional, 
  rows = 10, 
  size = 0.5,
  colors = c("#af9139", "#544616")
)

Keeps factor by default levels now

With:

iron(
  waffle(
    parts = c(thing1 = 0, thing2 = 100), 
    rows = 5
  ),
  waffle(
    parts = c(thing1 = 25, thing2 = 75), 
    rows = 5
  )
)

Without (you can disable this via keep parameter now):

iron(
  waffle(
    parts = c(thing1 = 0, thing2 = 100), 
    rows = 5, 
    keep = FALSE
  ),
  waffle(
    parts = c(thing1 = 25, thing2 = 75), 
    rows = 5, 
    keep = FALSE
  )
)

Professional Workforce Makeup

Iron example (left-align & padding for multiple plots)

pain.adult.1997 <- c(`YOY (406)` = 406, `Adult (24)` = 24)

waffle(
  parts = pain.adult.1997 / 2,
  rows = 7, 
  size = 0.5,
  colors = c("#c7d4b6", "#a3aabd"),
  title = "Paine Run Brook Trout Abundance (1997)",
  xlab = "1 square = 2 fish", pad = 3
) -> A

pine.adult.1997 <- c(`YOY (221)` = 221, `Adult (143)` = 143)

waffle(
  parts = pine.adult.1997 / 2, 
  rows = 7, 
  size = 0.5,
  colors = c("#c7d4b6", "#a3aabd"),
  title = "Piney River Brook Trout Abundance (1997)",
  xlab = "1 square = 2 fish", pad = 8
) -> B

stan.adult.1997 <- c(`YOY (270)` = 270, `Adult (197)` = 197)

waffle(
  parts = stan.adult.1997 / 2, 
  rows = 7, 
  size = 0.5,
  colors = c("#c7d4b6", "#a3aabd"),
  title = "Staunton River Trout Abundance (1997)",
  xlab = "1 square = 2 fish"
) -> C

iron(A, B, C)

Package Code Metrics

cloc::cloc_pkg_md()
Lang # Files (%) LoC (%) Blank lines (%) # Lines (%)
R 14 0.44 624 0.35 218 0.36 439 0.38
Rmd 2 0.06 255 0.15 88 0.14 139 0.12
SUM 16 0.50 879 0.50 306 0.50 578 0.50

{cloc} πŸ“¦ metrics for waffle

Code of Conduct

Please note that this project is released with a Contributor Code of Conduct. By participating in this project you agree to abide by its terms.

More Repositories

1

hrbrthemes

πŸ” Opinionated, typographic-centric ggplot2 themes and theme components
R
1,139
star
2

pewpew

⭐ ⭐ ⭐ Build your own IP Attack Maps with SOUND!
JavaScript
1,011
star
3

ggalt

🌎 Extra Coordinate Systems, Geoms, Statistical Transformations & Scales for 'ggplot2'
R
641
star
4

markdowntemplates

βœ…πŸ”» A collection of alternate R markdown templates
CSS
318
star
5

docxtractr

βœ‚οΈ Extract Tables from Microsoft Word Documents with R
R
169
star
6

vegalite

R ggplot2 "bindings" for Vega-Lite
JavaScript
158
star
7

ggchicklet

πŸ€« Create Chicklet (Rounded Segmented Column) Charts
HTML
157
star
8

streamgraph

〰️ htmlwidget for creating streamgraph visualizations in R
HTML
146
star
9

metricsgraphics

πŸ“ˆ htmlwidget interface to the MetricsGraphics.js D3 chart library
HTML
133
star
10

sergeant

πŸ’‚β€β™‚οΈ Tools to Transform and Query Data with 'Apache' 'Drill'
R
126
star
11

statebins

Alternative to choropleths of US States ala http://bit.ly/statebins
R
120
star
12

splashr

πŸ’¦ Tools to Work with the 'Splash' JavaScript Rendering Service in R
R
99
star
13

RSwitch

πŸŽ› A small menubar app that allows you to switch between R versions quickly (if you have multiple versions of R framework installed).
Swift
99
star
14

newsflash

Tools to Work with the Internet Archive and GDELT Television Explorer in R
R
88
star
15

curlconverter

➰ ➑️ βž– Translate cURL command lines into parameters for use with httr or actual httr calls (R)
R
88
star
16

darksky

☁️ R interface to the Dark Sky API [APPLE IS SHUTTING DOWN THE API 2022-12-31]
R
82
star
17

freebase

πŸ‘ƒπŸ½A 'usethis'-like Package for Base R Pseudo-equivalents of 'tidyverse' Code
R
82
star
18

albersusa

Tools, shapefiles & data to work with an "AlbersUSA" composite projection in R
R
75
star
19

21-recipes

πŸ“• An R/rtweet edition of Matthew A. Russell's Python Twitter Recipes Book
CSS
72
star
20

nominatim

🌏 Tools for Working with the 'Nominatim' API in R
R
71
star
21

hrbraddins

Additional Addins for RStudio
R
68
star
22

ggeconodist

πŸ“‰ Create Diminutive Distribution Charts
R
67
star
23

decapitated

Headless 'Chrome' Orchestration in R
R
66
star
24

taucharts

πŸ“Š An R htmlwidget interface to the TauCharts javascript library
HTML
65
star
25

speedtest

πŸ“ Measure upload/download speed/bandwidth for your network with R
R
64
star
26

ggcounty

🌐 Generate ggplot2 geom_map county maps
R
62
star
27

pluralize

An R package to "Pluralize and Singularize Any Word"
JavaScript
60
star
28

qrencoder

πŸ”³ Make QR codes in R via libqrencode
C
59
star
29

quarto-organization-template

A Quarto RevealJS Organization Boilerplate Template You Can Clone And Modify Quickly
SCSS
59
star
30

swatches

🎨 Read, Inspect, Manipulate, and Save (ASE-only for save) Color Swatch Files
R
56
star
31

cdcfluview

😷 R package to Retrieve U.S. Flu Season Data from the CDC FluView Portal (WHO & ILINet)
R
56
star
32

rgeocodio

Tools to Work with the https://geocod.io/ API
R
56
star
33

cloc

πŸ”’ R package to the perl cloc script (which counts blank lines, comment lines, and physical lines of source code in source files/trees/archives)
Perl
55
star
34

dtupdate

The dtupdate package has functions that try to make it easier to keep up with the non-CRAN universe
R
55
star
35

wayback

βͺ Tools to Work with the Various Internet Archive Wayback Machine APIs
R
54
star
36

orangetext

πŸŠπŸ“„ : An #rstats project to keep track of The 🍊 One's speeches
R
53
star
37

rstudioconf2017

Slides/code/data from rstudio:: conf 2017
ASP
52
star
38

ndjson

♨️ Wicked-Fast Streaming 'JSON' ('ndjson') Reader in R
C++
51
star
39

ggvis-maps

Examples of various kinds of maps in ggvis (with & without shiny)
R
51
star
40

iptools

🍴 A toolkit for manipulating, validating and testing IP addresses and ranges, along with datasets relating to IP addresses. While it primarily has support for the IPv4 address space, more extensive IPv6 support is intended.
Scilab
51
star
41

tidyweb

Easily Install and Load Modern Web-Scraping Packages
R
50
star
42

weatherkit

πŸŽπŸŒ‘πŸ”Ž Obtain Historical, Current, and Predictive Weather Data from Apple WeatherKit REST API in R
R
46
star
43

pdfbox

πŸ“„β—»οΈ Create, Maniuplate and Extract Data from PDF Files (R Apache PDFBox wrapper)
Java
46
star
44

xmlview

πŸ“ƒ Format, Query and Pretty Print 'HTML'/'XML' Content in R (RStudio viewer or browser)
JavaScript
46
star
45

msgxtractr

πŸ“‡ Extract contents from Outlook '.msg' files in R
C
44
star
46

worldtilegrid

πŸ”²πŸ—Ί World Tile Grid Geom for ggplot2 [WIP]
R
43
star
47

QuickLookR

macOS QuickLook plugin for R save(), saveRDS() & feather files
C
42
star
48

voteogram

U.S. House and Senate Voting Cartogram Generators in R
R
41
star
49

nifffty

Small R package to post events to IFTTT Maker channel/recipes
R
40
star
50

githubdashboard

#rstats github flexdashboard
HTML
40
star
51

overpass

ℹ️ Tools to Work With the OpenStreetMap (OSM) Overpass API in R
HTML
40
star
52

hrbragg

Typography-centric Themes, Theme Components, and Utilities for 'ggplot2' and 'ragg'.
R
39
star
53

netintel

A collection of "network intelligence" utilities for R. ASN info, IP reputation, etc.
R
39
star
54

htmlunit

πŸ•ΈπŸ§°β˜•οΈTools to Scrape Dynamic Web Content via the 'HtmlUnit' Java Library
R
38
star
55

Rforecastio

☁️ Simple R interface to forecast.io weather data
R
38
star
56

omdbapi

R package to access the OMDB API (http://www.omdbapi.com/)
R
38
star
57

mactheknife

🦈 Various β€˜macOS’-oriented Tools and Utilities in R
R
37
star
58

tdigest

Wicked Fast, Accurate Quantiles Using 't-Digests'
C
36
star
59

hrbrmisc

personal R pkg
R
35
star
60

webr-experiments

πŸ•ΈοΈ πŸ§ͺ hrbrmstr's WebR Experiments
HTML
34
star
61

2017-year-in-review

Year in Review with R Rmd Template
34
star
62

crafter

πŸ”¬ An R package to work with PCAPs
R
33
star
63

longurl

ℹ️ Small R package for no-API-required URL expansion
R
32
star
64

greywatch

πŸ•΅πŸ½ macOS Big Sur desktop app to monitor active TCP connections through the lens of GreyNoise
Swift
32
star
65

ipv4-heatmap

Update to The Measurement Factory ipv4-heatmap codebase
C
32
star
66

jsonview

JSON pretty printer & viewer in R
JavaScript
30
star
67

rpwnd

πŸ™… The Most Benignly Malicious R Package on the Internet
R
30
star
68

statically

πŸ“Έ Generate Webpage Screenshots Using the Statically API
R
28
star
69

rradar

🌊 Animate current U.S. NOAA NWS N0R Radar Images
R
27
star
70

archinfo

π–Ό† Returns a list of running processes and the architecture (x86_64/arm64) they are running under.
C
26
star
71

ohq2quarto

Save an Observable HQ Notebook to a Quarto project
Rust
25
star
72

osqueryr

⁇ 'osquery' 'DBI' and 'dbplyr' Interface for R
R
25
star
73

ulid

βš™οΈ Universally Unique Lexicographically Sortable Identifiers in R
C++
25
star
74

webr-monaco-repl

πŸ§ͺ πŸ•ΈοΈ Monaco-powered WebR "REPL"
JavaScript
24
star
75

imprint

Create Customized 'ggplot2' and 'R Markdown' Themes for Your Organization
R
24
star
76

gdns

Tools to work with the Google DNS over HTTPS API in R
R
24
star
77

2020-george-floyd-protests

Code to collect data from various sources on the 2020 George Floyd protests.
HTML
23
star
78

fileio

⏳ Ephemeral File, Text or R Data Sharing with 'file.io'
R
23
star
79

widgetcard

Tools to Enable Easier Content Embedding in Tweets
R
23
star
80

attckr

βš”οΈMITRE ATT&CK Machinations in R
R
23
star
81

mgrs

🌐 An R Package to Convert 'MGRS' (Military Grid Reference System) References From/To Other Coordiante Systems
C
23
star
82

webr-app

πŸ§ͺ πŸ•ΈοΈ A Way Better Structured WebR Demo App
JavaScript
23
star
83

pubcrawl

πŸΊπŸ“– Convert 'epub' Files to Text (Use https://github.com/ropensci/epubr instead)
R
22
star
84

xslt

lightweight XSLT processing package for R based on xmlwrapp
R
22
star
85

swiftr

Seamless R and Swift Integration
R
22
star
86

ggsolar

πŸͺ Generate "solar system" plots with {ggplot2}
R
22
star
87

facetedcountryheatmaps

Small sample Rmd to show how to make faceted country heatmaps in a couple different ways in R
HTML
22
star
88

ipapi

An R package to geolocate IPv4/6 addresses and/or domain names using ip-api.com's API
HTML
22
star
89

firasans

πŸ” Fira Sans Condensed + Fira Mono Font Theme Based on hrbrthemes
R
22
star
90

drill-sergeant-rstats

πŸ“— A Little Book About Using Apache Drill and R
R
22
star
91

slopegraph

A 'slopegraph' ('table-chart') generator in Python using Cairo/RaphaΓ«l. Currently handles a two column chart with _many_ output options. Look at the '/examples' directory for sample configurations, data files and output formats.
JavaScript
22
star
92

reveal-qmd

Chrome Extension To Reveal Observable Notebooks As Quarto QMD {ojs} Blocks & provide downloads of FileAttachments and zipped Quarto project
JavaScript
21
star
93

elpresidente

πŸ‡ΊπŸ‡Έ Search and Extract Corpus Elements from 'The American Presidency Project'
R
21
star
94

wand

Use 'magic' to guess file types
R
21
star
95

warc

πŸ“‡ Tools to Work with the Web Archive Ecosystem in R
R
21
star
96

rstudio-electron-quarto-installer

Download and install the latest macOS RStudio (electron) daily along with the latest Quarto pre-release
Shell
21
star
97

urlscan

πŸ‘€ Analyze Websites and Resources They Request
R
21
star
98

wondr

Tools to Work with there CDC WONDER API in R
R
20
star
99

supercaliheatmapwidget

πŸ“… Supercalifragilistic HTML Calendar Heatmaps
JavaScript
20
star
100

secede-2014

R dplyr/tidyr/rvest/TopoJSON tutorial using the 2014 Scotland secession vote
R
20
star