PositionSankey {ggsankeyfier} | R Documentation |
Position nodes and edges in a Sankey diagram
Description
Calculates the x
and y
positions of elements (nodes and edges) in a
Sankey diagram.
Usage
PositionSankey
position_sankey(
width = "auto",
align = c("bottom", "top", "center", "justify"),
order = c("ascending", "descending", "as_is"),
h_space = "auto",
v_space = 0,
nudge_x = 0,
nudge_y = 0,
split_nodes = FALSE,
split_tol = 0.001,
direction = c("forward", "backward"),
...
)
Arguments
width |
Width of the node ( |
align |
A |
order |
A |
h_space |
Horizontal space between split nodes ( |
v_space |
Vertical space between nodes ( |
nudge_x , nudge_y |
Horizontal and vertical adjustment to nudge items by. Can be useful for offsetting labels. |
split_nodes |
A |
split_tol |
When the relative node size (resulting source and destination edges) differs more than this fraction, the node will be displayed as two separate bars. |
direction |
One of |
... |
Arguments passed on to |
Format
An object of class PositionSankey
(inherits from Position
, ggproto
, gg
) of length 13.
Details
Based on the stat_*
function applied to the parent's (stat_sankeynode()
,
stat_sankeyedge
) object either node or edge positions are calculated respectively.
These positions can be used to add additional layers (e.g., text or labels) to the
plot.
Value
Returns a ggplot2::Position
class object.
Author(s)
Pepijn de Vries
Examples
library(ggplot2)
data("ecosystem_services")
pos <- position_sankey(v_space = "auto", order = "ascending")
pos2 <- position_sankey(v_space = "auto", order = "ascending", direction = "backward")
## Let's subset the data, to make the plot less cluttered:
es_subset <- pivot_stages_longer(
subset(ecosystem_services, RCSES > 0.01),
c("activity_realm", "biotic_realm", "service_section"),
"RCSES",
"service_section"
)
plot <-
ggplot(es_subset, aes(x = stage, y = RCSES, group = node,
connector = connector, edge_id = edge_id,
fill = node)) +
geom_sankeynode(position = pos) +
geom_sankeyedge(position = pos, aes(fill = service_section))
# position labels at nodes
plot + geom_text(aes(label = node), stat = "sankeynode", position = pos)
# position labels at the start of edges
plot + geom_text(aes(label = sprintf("%0.2f", RCSES)), stat = "sankeyedge", position = pos)
# position labels at the end of edges
plot + geom_text(aes(label = sprintf("%0.2f", RCSES)), stat = "sankeyedge", position = pos2)