plot_sankey
enhances the networkD3::sankeyNetwork
function, utilising htmlwidgets
to create an interactive Sankey diagram.
This type of flow diagram is particularly effective for illustrating data movement or transfer
between different entities (nodes), such as in the distribution of votes across elections or
in preferential voting systems.
Usage
plot_sankey(
data,
source,
target,
value,
units = "",
colours = colour_pal("catExtended"),
fontSize = 20,
fontFamily = "Calibri",
nodeWidth = 20,
nodePadding = 10,
margin = list(top = 0, right = 0, bottom = 0, left = 0),
width = 1200,
height = 800,
shiftLabel = NULL,
heading = NULL,
sourceTitle = NULL,
targetTitle = NULL
)
Arguments
- data
A data frame containing the flow data, where each row represents a unique flow from a source to a target node.
- source
The column in
data
representing the source nodes.- target
The column in
data
representing the target nodes.- value
The column in
data
representing the flow's magnitude or volume.- units
A string indicating the units for
value
, displayed in the tooltip. Defaults to an empty string, implying no units.- colours
A list or vector of colours for nodes and links in the diagram, either as hexadecimal codes or colour names. Can be a named list for specific node colour mapping.
- fontSize
Font size for text in the diagram.
- fontFamily
Font family for text in the diagram.
- nodeWidth
Width of nodes in the diagram.
- nodePadding
Padding between nodes in the diagram.
- margin
Margins around the diagram, specified as a list.
- width
Width of the plot in pixels.
- height
Height of the plot in pixels.
- shiftLabel
Numeric value to adjust the position of labels;
NA
for auto-placement.- heading
An optional string for the main title of the Sankey diagram.
- sourceTitle
An optional string for labeling the source nodes.
- targetTitle
An optional string for labeling the target nodes.
Value
An interactive Sankey diagram as an HTML widget, which can be used in R Markdown documents, Shiny applications, or the R console. The diagram provides a visual representation of the flow data, with customisable aspects such as colours, fonts, and node dimensions.
Examples
if (FALSE) { # \dontrun{
# Example: Visualising the flow of votes between parties
plot_sankey(data = election_data,
source = "Party_Previous_Election",
target = "Party_Current_Election",
value = "Number_of_Votes",
colours = c("Labour" = "red", "Green" = "green", "National" = "blue"),
heading = "Election Vote Flow",
sourceTitle = "Previous Election",
targetTitle = "Current Election")
} # }