stackplot | R Documentation |
Generates a stacked area plot of the absolute or relative frequencies of groups over time (or some other discrete or continuous variable). Stream plot functionality inspired by https://gist.github.com/menugget/7864454
stackplot(
obj,
x = NA,
freq = TRUE,
type = "n",
sort = FALSE,
spline = FALSE,
density = NA,
angle = NA,
col = NA,
border = NULL,
lwd = 1,
xlim = NA,
ylim = NA,
...
)
obj: |
an object of class 'matrix' or 'table'. Rows are assumed to correspond to repeated measures, and columns to different groups. |
x: |
optional, customize the horizontal location of data points (i.e., counts in 'obj') |
freq: |
if FALSE, normalize to relative frequencies |
type: |
determines baseline of stacked areas, defaults to 'n' for zero baseline (against horizontal axis); 't' applies symmetric layout (ThemeRiver), 'w' applies wiggle layout. |
sort: |
if TRUE, sort counts by marginal totals such that the most frequent types are drawn on the outside of the stack. This is effective for stream plots, as the most frequent type otherwise drives the overall shape. Defaults to FALSE ( plot by input order). |
spline: |
if TRUE, interpolate data points to draw areas with smoothed trendlines |
density: |
numeric, density of shading lines for each area; values recycled as necessary |
angle: |
numeric, angle of shading lines for each area; values recycled as necessary |
col: |
character, colour specification strings for filling areas, recycled as necessary |
border: |
border color for polygon areas |
lwd: |
line weight for polygon borders |
xlim: |
optional, allows user to override default horizontal plot range |
...: |
additional arguments to pass to 'plot' function |
Byron L, Wattenberg M. Stacked graphs–geometry & aesthetics. IEEE transactions on visualization and computer graphics. 2008 Oct 24;14(6):1245-52.
stackplot(EuStockMarkets, xlab='Days (1991-1998)',
ylab='Daily Closing Price', col=gg.rainbow(4), border=NA,
bty='n')
# a centered stackplot with shading
last.mar <- par()$mar
par(mar=rep(1,4))
stackplot(EuStockMarkets, type='t', density=seq(0, 60, 20),
angle=30, col='black', border='black', bty='n', xaxt='n',
yaxt='n', lwd=0.5)
par(mar=last.mar)
# Christmas bird counts in Hamilton, Ontario
# https://sharleenw.rbind.io/post/hamilton_cbc_part_1/hamilton-christmas-bird-count-part-1/
par(mar=c(3,1,1,1))
n <- ncol(cbc)
stackplot(cbc[2:n], x=cbc[,1], type='w', bty='n', yaxt='n',
spline=T, border='white', lwd=0.25)
labels <- gsub('\\.', ' ', names(cbc)[2:n])
legend(x=1959, y=-150, legend=rev(labels), bty='n', cex=0.7,
fill=rev(pal), y.intersp=0.8)
par(mar=last.mar)
Add the following code to your website.
For more information on customizing the embed code, read Embedding Snippets.