Google Sankey Chart with R
A sankey diagram is a visualization used to depict a flow from one set of values to another. The things being connected are called nodes and the connections are called links. They're named after Captain Sankey, who created a diagram of steam engine efficiency that used arrows having widths proportional to heat loss.
gvisSankey(data, from = "", to = "", weight = "", options = list(), chartid)
data |
data.frame that contains the data to be visualised |
from |
a string that referes to the column name in
|
to |
a string that referes to the column name in
|
weight |
name of the column with the numerical weight of the connections |
options |
list of configuration options. The options are documented in detail by Google online: https://developers.google.com/chart/interactive/docs/gallery/sankey#Configuration_Options The parameters can be set via a named list. The parameters have to map those of the Google documentation.
For more details see the Google API documentation and the R examples below. |
chartid |
character. If missing (default) a random chart id will be
generated based on chart type and |
gvisSankey returns list
of class
"gvis
" and "list
".
An object of class "gvis
" is a list containing at least the
following components:
type
Google visualisation type
chartid
character id of the chart object. Unique chart ids are required to place several charts on the same page.
html
a list with the building blocks for a page
header
a character string of a html page header:
<html>...<body>
,
chart
a named character vector of the chart's building blocks:
jsHeader
Opening <script>
tag and
reference to Google's JavaScript library.
jsData
JavaScript function defining the input
data
as a JSON object.
jsDrawChart
JavaScript function combing the data with the visualisation API and user options.
jsDisplayChart
JavaScript function calling the handler to display the chart.
jsFooter
End tag </script>
.
jsChart
Call of the jsDisplayChart
function.
divChart
<div>
container to embed the chart
into the page.
caption
character string of a standard caption, including data name and chart id.
footer
character string of a html page footer:
</body>...</html>
, including the used R and googleVis version
and link to Google's Terms of Use.
The sankey chart may be undergoing substantial revisions in future Google Charts releases.
Markus Gesmann markus.gesmann@gmail.com
Google Chart Tools API: https://developers.google.com/chart/interactive/docs/gallery/sankey
dat <- data.frame(From=c(rep("A",3), rep("B", 3)), To=c(rep(c("X", "Y", "Z"),2)), Weight=c(5,7,6,2,9,4)) sk1 <- gvisSankey(dat, from="From", to="To", weight="Weight") plot(sk1) sk2 <- gvisSankey(dat, from="From", to="To", weight="Weight", options=list(sankey="{link: {color: { fill: '#d799ae' } }, node: { color: { fill: '#a61d4c' }, label: { color: '#871b47' } }}")) plot(sk2)
Please choose more modern alternatives, such as Google Chrome or Mozilla Firefox.