## ----include = FALSE---------------------------------------------------------- knitr::opts_chunk$set( collapse = TRUE, comment = "#>", fig.ext = "png", dev = "ragg_png" ) ggplot2::theme_set(ggplot2::theme_light()) ## ----setup-------------------------------------------------------------------- library(ggsankeyfier) library(ggplot2) ## Let's start with subsetting the data to make it less cluttered es_sub <- ecosystem_services |> subset(RCSES > quantile(RCSES, 0.99)) |> pivot_stages_longer(c("activity_realm", "biotic_realm", "service_section"), "RCSES", "service_section") p <- ggplot(es_sub, aes(x = stage, y = RCSES, group = node, connector = connector, edge_id = edge_id)) ## ----stack_order_asc, results='hide', fig.width=6, fig.height=3--------------- pos <- position_sankey(v_space = "auto", order = "ascending") p + geom_sankeyedge(aes(fill = service_section), position = pos) + geom_sankeynode(position = pos) ## ----stack_order_des, results='hide', fig.width=6, fig.height=3--------------- pos <- position_sankey(v_space = "auto", order = "descending") p + geom_sankeyedge(aes(fill = service_section), position = pos) + geom_sankeynode(position = pos) ## ----stack_order_plus, results='hide', fig.width=6, fig.height=3-------------- pos <- position_sankey(order = "descending+", v_space = "auto", align = "justify") p + geom_sankeyedge(aes(fill = service_section), position = pos) + geom_sankeynode(position = pos) ## ----stack_order_custom, results='hide', fig.width=6, fig.height=3------------ ## Definition of a custom ordering function: custom_order <- function(data) { if ("edge_id" %in% names(data)) { # data contains edge info ## Order incoming edges from big to small data$edge_order_end <- data$y ## Order outgoing edges from small to big (note minus sign) data$edge_order <- -data$y } else { ## data contains node info data$node_order <- data$y } return(data) } pos <- position_sankey(v_space = "auto", order = custom_order) p + geom_sankeyedge(aes(fill = service_section), position = pos) + geom_sankeynode(position = pos)