Hi community,
I'm trying to create a Sankey diagram using the {ggsankey}
package to replicate a 10-step semaglutide dose titration pattern as seen in a published paper. I’ve carefully followed all steps and verified that my dataset has the required structure.
Here’s a minimal summary of what I’ve done:
- Created rx_long_clean using
make_long()
- Confirmed it has all four columns:
x
,node
,next_x
,next_node
names(rx_long_clean)
#> [1] "x" "node" "next_x" "next_node"
- Created a
label_data
object using:
label_data <- rx_long_clean %>%
group_by(x, node) %>%
summarise(n = n(), .groups = "drop") %>%
mutate(
perc = round(n / sum(n) * 100, 1),
label = paste0(perc, "%"),
x = factor(x, levels = paste0("Rx", 1:10)),
node = factor(node, levels = dose_levels)
)
4. But when I try to run `geom_sankey_label()`:
geom_sankey_label(data = label_data, aes(x = x, node = node, label = label))
I get the error:
Error in geom_sankey_label():
Problem while computing aesthetics.
Caused by error in mutate()
-> in across()
:
Can't subset columns that don't exist.
Column next_x
doesn't exist.
5. I **triple-checked** that `label_data` does not need `next_x`, as I’m only mapping `x`, `node`, and `label`.
---
### ❓ What I’ve Tried:
* Verified column names and types
* Ensured both `rx_long_clean` and `label_data` are correctly grouped/factored
* Ignoring the warning `"Ignoring unknown aesthetics: node"` in output still throws error
---
### 🙏 Help Needed:
Is there an internal expectation inside `geom_sankey_label()` for `next_x`, even when it’s not mapped? Or is there another workaround to use labels safely with 10-step Sankey transitions?
Happy to provide a reproducible example if needed!
Thanks so much!