【问题标题】:How to make a stacked Sankey diagram using ggplot in R?如何在 R 中使用 ggplot 制作堆叠桑基图?
【发布时间】:2022-12-31 04:13:51
【问题描述】:

我有这些数据,我想使用 ggplot 创建一个堆叠的 Sankey 图。我想尝试重新创建它,如下图所示。最好的方法是什么?

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

我想用数据重新创建这个图表

到目前为止,我已经尝试过了,但我认为这不会按照我想要的方式制作我的数据。

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

【问题讨论】:

标签: r ggplot2 sankey-diagram


【解决方案1】:

这看起来足够让你开始......

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")

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2022-11-23
    • 2016-01-29
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-04-06
    相关资源
    最近更新 更多