Skip to content

Instantly share code, notes, and snippets.

@yihui
Created May 2, 2012 03:07
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save yihui/2573310 to your computer and use it in GitHub Desktop.
Save yihui/2573310 to your computer and use it in GitHub Desktop.
a quick and dirty function to draw stream graphs
# data is a non-negative matrix; x is the timeline vector
stream_graph = function(data, x=1:nrow(data)) {
m = ncol(data); n = nrow(data)
data[is.na(data)] = 0
data = t(apply(cbind(0, as.matrix(data)), 1, cumsum))
data = sweep(data, 1, rowMeans(data))
plot(range(x), range(data, na.rm = TRUE), type = 'n', ann = FALSE)
for (j in 1:m) {
y = c(data[, j], rev(data[, j + 1]))
polygon(xspline(c(x, rev(x)), y, shape = -1, draw = FALSE), col = j, border = NA)
}
}
# test
palette(hsv(seq(0, 1, length = 10), .5, .7))
stream_graph(matrix(runif(1000), 100))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment