waffle: waffle

Description Usage Arguments Details Examples

View source: R/plot_misc.R

Description

A waffle chart.

Usage

1
waffle(x, rows, horiz = TRUE, cols = seq_along(x), mat, pct = 0.85, ...)

Arguments

x

an integer vector with counts for each group

rows

number of rows

horiz

logical; orientation of the chart and pattern of coloring boxes; default is TRUE (horiztonal)

cols

colors for each group, should be of length length(x)

mat

an optional matrix giving the layout; see details

pct

the percent the square spanned along the x- and y-axes; can be length one (recycled) or two

...

additional graphical parameters passed to par

Details

If mat is given, all other arguments except ... are ignored, and mat is used to specify dimensions, layout, and colors for the plot; see examples.

Examples

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
waffle(c(3, 10), 2)
waffle(c(3, 10), 2, pct = 1)
waffle(c(3, 10), 2, pct = 0.5)
waffle(c(3, 10), 2, pct = c(0.95, 0.5))

waffle(c(15, 700), rows = 40, horiz = FALSE, cols = c('salmon2', 'grey90'))

cols <- c("#F8766D", "#7CAE00", "#00BFC4", "#C77CFF")
waffle(c(80, 30, 20, 10), rows = 8, cols = cols, mar = c(0,0,0,7))
legend('right', legend = LETTERS[1:4], pch = 15, col = cols, pt.cex = 2,
       bty = 'n')

## using mat
mat <- rep(c(cols, NA), times = c(80, 30, 20, 10, 4))
waffle(mat = matrix(mat, 8))


## Not run: 
tf <- tempfile(fileext = '.pdf')
pdf(tf, width = 7, height = 3)
savings <- c('Mortgage ($84,911)' = 84911,
             'Auto and\ntuition loans ($14,414)'=14414,
             'Home equity loans ($10,062)' = 10062,
             'Credit cards\n($8,565)' = 8565)
w <- waffle(savings / 392, rows = 7, cols = c("#c7d4b6", "#a3aabd", "#a0d0de", "#97b5cf"),
            bg = 'cornsilk', mar = c(0,0,0,3))

xx <- c(-.05, .73, .85, .93)
yy <- c(-.05, -.05, -.35, -.05)
segments(x0 = xx, y0 = .05, y1 = yy, lty = 'dotted',
         lwd = 1, xpd = NA, col = 'grey50')
text(xx, yy + .05, labels = names(savings), xpd = NA, cex = .6, col = 'grey50', pos = 1)

p <- par('usr')
mtext('Average household savings each year', at = xx[1], font = 2,
      col = 'grey50', adj = 0, line = 1)
mtext('Source: Federal Reserve', side = 1, font = 3, at = xx[1],
      col = 'grey50', adj = 0, cex = .6, line = 2)
legend(.87, 1.3, legend = '$392', xpd = NA, bty = 'n', bg = 'cornsilk',
       col = 'orange', text.col = 'grey50', pch = 15, cex = .8, pt.cex = 1.5)
dev.off()

system(paste(getOption('pdfviewer'), tf))
unlink(tf)

## End(Not run)

raredd/plotr documentation built on Nov. 19, 2018, 11:02 a.m.