Return to Snippet

Revision: 65242
at November 11, 2013 10:53 by merian


Updated Code
# 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

Revision: 65241
at November 11, 2013 10:27 by merian


Initial Code
# 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 note 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]})
sa

# 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

Initial URL


Initial Description
My first attempt at a Sankey diagram in R, using rCharts and timelyportfolio's work

Initial Title
Sankey diagram with R, rCharts, d3

Initial Tags


Initial Language
R