美文网首页
R可视化:生成桑基图

R可视化:生成桑基图

作者: 生信学习者2 | 来源:发表于2024-05-19 13:48 被阅读0次

    介绍

    采用networkD3生成桑基图。

    library(networkD3)
    
    nodes <- data.frame(name = c("Raw Material A", "Raw Material B", "Raw Material C",
                                 "Processed Material A1", "Processed Material B1",
                                 "Component A1-B1", "Component B1-C1",
                                 "Final Product A", "Final Product B"))
    
    links <- data.frame(source = c(0, 1, 2,    # Raw materials to processed materials
                                   0, 1, 1, 2,  # Processed materials to components
                                   3, 4,       # Components to final products
                                   5, 6),
                        target = c(3, 4, 6,    # Processed materials
                                   5, 5, 6, 6,  # Components
                                   7, 8,       # Final products
                                   7, 8),
                        value = c(10, 20, 30,   # Values for raw to processed
                                  15, 25, 35, 30, # Values for processed to components
                                  20, 30,        # Values for components to final
                                  40, 50))       # Final values
    
    links$source <- as.integer(links$source)
    links$target <- as.integer(links$target)
    
    sankey <- sankeyNetwork(Links = links, Nodes = nodes,
                            Source = "source", Target = "target",
                            Value = "value", NodeID = "name",
                            fontSize = 12, nodeWidth = 30)
    
    sankey
    

    相关文章

      网友评论

          本文标题:R可视化:生成桑基图

          本文链接:https://www.haomeiwen.com/subject/doagfjtx.html