Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

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

Open
gilbertlzrus opened this issue Oct 20, 2022 · 2 comments
Open

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

gilbertlzrus opened this issue Oct 20, 2022 · 2 comments

Comments

@gilbertlzrus
Copy link

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
Copy link

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

@gilbertlzrus
Copy link
Author

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

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants