Sankey Diagrams
Sankey plots illustrate the flow of information or material.
devtools::install_github("mangothecat/sankey")library(sankey)The API of the package is very simple. You can create a sankey object
with make_sankey, and plot it with sankey. make_sankey takes at
least one data frame that defines the edges of the plot. This should be a
data frame, with the starting points of the edges in the first column,
and the end point of the edges in the second column. Both must character
vectors.
Here is a simple example that shows function calls in the pkgsnap package:
edges <- read.table(stringsAsFactors = FALSE, textConnection(
" get_deps get_description
get_deps parse_deps
get_deps %||%
get_deps drop_internal
get_description pkg_from_filename
parse_deps str_trim
cran_file get_pkg_type
cran_file r_minor_version
download_urls split_pkg_names_versions
download_urls cran_file
pkg_download dir_exists
pkg_download download_urls
pkg_download filename_from_url
pkg_download try_download
restore pkg_download
restore drop_missing_deps
restore install_order
restore get_deps
split_pkg_names_versions data_frame
"))
pkgsnap_sankey <- make_sankey(edges = edges)
sankey(pkgsnap_sankey)Several properties of the plot can be customized by setting extra columns in the data frame that define the edges, or another data frame, that defines the nodes.
Current list of graphical parameters for edges:
colorstyleWhether the to use a solid color (col), orgradientto plot the edges. The color of a gradient edges is between the colors of the nodes.curvestyleEdge style,sinfor sinusoid curves,linefor straight lines.colEdge color, for edges with solid colors.weightEdge weight. Determines the width of the edges.
Current list of graphical parameters for nodes:
colNode color.sizeNode size.xHorizontal coordinates of the center of the node.yVertical coordinates of the center of the node.shapeShape of the node. Possible values:rectangle,point,invisible.ltyLite type, seepar.srtHow to rotate the label, seepar.textcolLabel color.labelLabel text. Defaults to node name.adjxHorizontal adjustment of the label. Seeadjin theparmanual.adjyVertical adjustment of the label. Seeadjin theparmanual.boxwWidth of the node boxes.cexLabel size multiplication factor.topVertical coordinate of the top of the node.centerVertical coordinate of the center of the node.bottomVertical coordinate of the bottom of the node.posPosition of the text label, seepar.textxHorizontal position of the text label.textyVertical position of the text label.
Here is the same plot again, with some customization.
nodes <- data.frame(
stringsAsFactors = FALSE,
id = c("snap", sort(unique(c(edges[,1], edges[,2]))))
)
nodes$col <- ifelse(nodes$id %in% c("snap", "restore"), "orange", "#2ca25f")
edges$colorstyle <- "gradient"
sankey(make_sankey(nodes, edges))We colored the nodes of the exported functions orange, and also added a color gradient along the edges.
GPL (>= 2) © Mango Solutions, January Weiner

