waffle: waffle

View source: R/plot.R

waffleR Documentation

waffle

Description

A waffle chart.

Usage

waffle(
  mat,
  xpad = 0,
  ypad = 0,
  heights = NULL,
  widths = NULL,
  colpad = 0,
  rowpad = 0,
  invert = "",
  ...,
  reset_par = TRUE,
  add = FALSE
)

Arguments

mat

a matrix of integers or character strings of color names; if mat is a matrix of integers, the colors used will correspond to the current palette

xpad, ypad

padding between rects, recycled as needed; note that these do not affect the center coordinates of the rectangles

heights, widths

row heights and column widths, respectively, usually in [0,1], recycled as needed; note xpad and/or ypad are ignored if widths and/or heights are used, respectively

colpad, rowpad

amount of padding between columns and rows; note that these shift the rect center coordinates rather than the heights and widths directly

invert

character string indicating about which axis the matrix should be inverted; possible values are "x", "y", or "xy"

...

additional graphical parameters passed to rect such as border, density, lty, etc.

reset_par

logical; if TRUE, resets par settings to state before function call; setting reset_par = FALSE along with the return value are useful for adding to a waffle plot

add

logical; use TRUE to add to an existing plot; otherwise, a new frame and window are initialized

Value

A list of four matrices:

$matrix

the input matrix as plotted including inversions

$origin

coordinates of the bottom-left corner for each box

$centers

coordinates for the centers of each box

$rect

coordinates for each corner of each box

Examples

waffle(matrix(1:9, 3))
waffle(matrix(1:9, 3), invert = 'x')
waffle(matrix(1:9, 3), heights = c(.25, .95, .5), border = NA)
waffle(matrix(1:9, 3), xpad = 0, colpad = c(0, .1, 0))


## heatmap
## convert the numeric data to color strings
cols <- c(cor(mtcars))
cols <- tcol(c('blue', 'red')[(cols > 0) + 1L], alpha = c(abs(cols)))

mat <- matrix(cols, 11)
waffle(mat, reset_par = FALSE, invert = 'x')
axis(3, at = 1:11 - .5, labels = names(mtcars), lwd = 0)


## use colpad/rowpad to create sections
w <- waffle(mat, reset_par = FALSE, invert = 'x',
            colpad = rep(c(0, 0.5, 1, 0), c(5, 1, 1, 4)),
            rowpad = rep(c(0, 0.5, 0), c(5, 1, 5)))
axis(3, unique(w$centers[, 'x']), names(mtcars), lwd = 0)


## adding to margins of another plot
set.seed(1)
n  <- 97
ng <- 3
cols <- c('beige', 'dodgerblue2', 'green', 'orange')
x <- sample(cols, n * ng, replace = TRUE, prob = c(.05,.31,.32,.32))
x <- kinda_sort(x, n = 20)

op <- par(fig = c(0, 1, 0.2, 0.9), mar = c(0, 5, 0, 1))
plot(cumsum(rnorm(n)), type = 'l', ann = FALSE, xaxt = 'n')

par(fig = c(0, 1, 0, 0.2), mar = c(1, 5, 0, 1), new = TRUE)
waffle(matrix(x, ng), heights = c(0.95, 0.5, 0.95), border = 'white',
       reset_par = FALSE) -> wf
text(0, rev(unique(wf$centers[, 'y'])), paste('Feature', 1:3),
     xpd = NA, pos = 2L)

par(fig = c(0, 1, 0.9, 1), mar = c(0.5, 5, 0.5, 1), new = TRUE)
waffle(matrix(x, ng)[1L, , drop = FALSE], heights = 0.5,
       border = 'white', reset_par = FALSE)
box()
box('outer')
par(op)


## waffle conveniently returns the centers of the rects
## be sure _not_ to reset pars for proper alignment
w <- waffle(matrix(1:8, 2), reset_par = FALSE, invert = 'xy')
text(w$c[, 'x'], w$c[, 'y'], labels = palette(), col = 'white')


## this is similar to ?rawr::show_colors
col <- colors()[1:25 ^ 2]
w <- waffle(matrix(col, 25), reset_par = FALSE, invert = 'xy', border = NA)
text(w$c[, 'x'], w$c[, 'y'], labels = col, col = 'black', cex = .4)


raredd/rawr documentation built on Feb. 25, 2025, 1:48 p.m.