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)
Run the code above in your browser using DataLab