/ Published in: R

My first attempt at a Sankey diagram in R, using rCharts and timelyportfolio's work
Expand |
Embed | Plain Text
Copy this code and paste it in your HTML
# sankey chart using d3 plugin for rCharts and the igraph library require(rCharts) require(igraph) # these are our vertices/nodes/end points/stages/categories/classes/whatever nodes = c("PhD", "Career Outside Science", "Early Career Researcher", "Research Staff", "Permanent Research Staff", "Professor", "Non-Academic Research") # the chart is basically a graph g <- graph(c(1, 2, 1, 3, 3, 4, 3, 7, 4, 2, 4, 5, 4, 6, 4, 7)) E(g)$weights <- c(53., 47., 30., 17., 22.5, 3., .5, 4.) # convert to data frame with appropriate node names edgelist <- get.data.frame(g) # name columns as what is expected by plugin colnames(edgelist) <- c("source", "target", "value") edgelist$source <- lapply(edgelist$source, FUN = function(x) {nodes[x]}) edgelist$target <- lapply(edgelist$target, FUN = function(x) {nodes[x]}) # now we plot sankeyPlot <- rCharts$new() sankeyPlot$setLib('http://timelyportfolio.github.io/rCharts_d3_sankey/libraries/widgets/d3_sankey') sankeyPlot$set( data = edgelist, nodeWidth = 15, nodePadding = 25, layout = 32, width = 800, height = 500 ) sankeyPlot
Comments
