[R] Sankey Diagrams in R

Eric Fail eric.fail at gmx.us
Mon Mar 19 07:52:16 CET 2012


Dear R-list,

I am trying to visualize where the dropout happens in our patient flow. We are currently using traditional flowcharts and it bothers me that I can't visualize both the percentage and the flow in one diagram.

The other day I came across some interesting diagrams doing exactly what I wanted, they had both flow and percentages visualized on one diagram. Here is some nice examples apparently made with ‘sankeypython’ http://www.sankey-diagrams.com/tag/software/

It didn't take long to find a blog where a Ruser (thanks!) had posted an R script that actually produces an Sankey Diagram in R http://biologicalposteriors.blogspot.com/2010/07/sankey-diagrams-in-r.html

See below for working example.

My questions are, is this the most updated Sankey Diagram-script we have in the R community? Is there a better way to visualize flow and percentages in one diagram in R?

Thanks,
Eric

## the working example

## th, https://tonybreyal.wordpress.com/2011/11/24/source_https-sourcing-an-r-script-from-github/
sourc.https <- function(url, ...) {
  # load package
require(RCurl)
  # install.packages(c("RCurl"), dependencies = TRUE)

  # parse and evaluate each .R script
  sapply(c(url, ...), function(u) {
    eval(parse(text = getURL(u, followlocation = TRUE, cainfo = system.file("CurlSSL", "cacert.pem", package = "RCurl"))), envir = .GlobalEnv)
  })
}

# Example from https://gist.github.com/1423501
sourc.https("https://raw.github.com/gist/1423501/55b3c6f11e4918cb6264492528b1ad01c429e581/Sankey.R")

# My example (there is another example inside Sankey.R):
inputs = c(6, 144)
losses = c(6,47,14,7, 7, 35, 34)
unit = "n ="
labels = c("Transfers",
           "Referrals\n",
           "Unable to Engage",
           "Consultation only",
           "Did not complete the intake",
           "Did not engage in Treatment",
           "Discontinued Mid-Treatment",
           "Completed Treatment",
           "Active in \nTreatment")
SankeyR(inputs,losses,unit,labels)

# Clean up my mess
rm("inputs", "labels", "losses", "SankeyR", "sourc.https", "unit")



More information about the R-help mailing list