davidsjoberg / ggsankey

Make sankey, alluvial and sankey bump plots in ggplot
Other
262 stars 30 forks source link

How to skip nodes with NA value in ggsankey? #28

Open gilbertlzrus opened 1 year ago

gilbertlzrus commented 1 year ago

Suppose I have this dataset (the actual dataset has 30+ columns and thousands of ids)

    df <- data. Frame(id = 1:5,
                admission = c("Severe", "Mild", "Mild", "Moderate", "Severe"),
                d1 = c(NA, "Moderate", "Mild", "Moderate", "Severe"),
                d2 = c(NA, "Moderate", "Mild", "Mild", "Moderate"),
                d3 = c(NA, "Severe", "Mild", "Mild", "Severe"),
                d4 = c(NA, NA, "Mild", "Mild", NA),
                outcome = c("Dead", "Dead", "Alive", "Alive", "Dead"))

I want to make a Sankey diagram that illustrates the daily severity of the patients over time. However, when the observation reaches NA (means that an outcome has been reached), I want the node to directly link to the outcome.

This is how the diagram should look like: enter image description here

Image fetched from the question asked by @qdread here

Is this possible with ggsankey?

This is my current code:

df.sankey <- df %>%
    make_long(admission, d1, d2, d3, d4, outcome)
ggplot(df.sankey, aes(x = x,
                     next_x = next_x,
                     node = node,
                     next_node = next_node,
                     fill = factor(node),
                     label = node)) +
    geom_sankey(flow.alpha = 0.5,
                node.color = NA,
                show.legend = TRUE) +
    geom_sankey_text(size = 3, color = "black", fill = NA, hjust = 0, position = position_nudge(x = 0.1))

Which results in this diagram: enter image description here

Thanks in advance for the help.

nsdelablancac commented 1 year ago

Hello! I have the same issue in the ggsankey package. Did you solve it? Thanks for sharing.

gilbertlzrus commented 1 year ago

Hello! I have the same issue in the ggsankey package. Did you solve it? Thanks for sharing.

Hi, I've found the answer for the issue with the help of others in Stackoverlow. Please see the solution here and consider giving upvotes for the question👍

Thanks! Hope this helps