简体   繁体   中英

How to make a stacked Sankey diagram using ggplot in R?

I have this data and I want to create a stacked Sankey Diagram using ggplot. I want to try and recreate it and look like the following picture. What's the best way to go about it?

Risk Factors for Stroke             1990    1995    2000    2005    2010
Obesity                                 0.001   0.013   0.043   0.077   0.115
Diabetes                            0.359   0.316   0.26    0.187   0.092
Smoking                                 0.171   0.156   0.142   0.128   0.116
Hypercholesterolemia                    0.161   0.104   0.045   0.001   0.001
Hypertension                            0.654   0.633   0.602   0.561   0.509

I want to recreate this diagram with the data在此处输入图像描述

I tried this so far but I don't think that will make my data the way I want it to.

D2 <- Datatable1 %>% make_long(`Risk Factors for Stroke in Blacks`, `1990`, `1995`, `2000`, `2005`, `2010`)
D2

this looks close enough to get you started...

library(data.table)
library(ggplot2)
library(ggalluvial)
# read sample data
DT <- fread('"Risk Factors for Stroke"             1990    1995    2000    2005    2010
Obesity                                 0.001   0.013   0.043   0.077   0.115
Diabetes                            0.359   0.316   0.26    0.187   0.092
Smoking                                 0.171   0.156   0.142   0.128   0.116
Hypercholesterolemia                    0.161   0.104   0.045   0.001   0.001
Hypertension                            0.654   0.633   0.602   0.561   0.509', header = TRUE)
# create workable column-names
setnames(DT, janitor::make_clean_names(names(DT)))
# melt to long format
DT.melt <- melt(DT, id.vars = "risk_factors_for_stroke")
# create variable for sorting the riks by value
DT.melt[order(-value, variable), id := factor(rowid(variable))]
# create plot
ggplot(data = DT.melt, 
       aes(x = variable, y = value,
           stratum = id, 
           alluvium = risk_factors_for_stroke, 
           fill = risk_factors_for_stroke, 
           colour = id,
           label = value)) + 
  geom_flow(stat = "alluvium", lode.guidance = "frontback",
            color = "white") +
  geom_stratum(color = "white", width = 0.7) +
  geom_text(position = position_stack(vjust = 0.5), colour = "white")

在此处输入图像描述

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM