k8com官网

图片展示
图片展示

园槿绽红艳,郊桑柔绿滋——怎样用R绘制漂亮的桑基图(附源代码)

发表时间: 2022-04-05
姗师妹:这图真好看呀,嘤.....岚师弟:这图叫啥啊师兄?牛师兄:园槿绽红艳,郊桑柔绿滋,这个图叫桑基图,桑者,或浅黄浅白,或浅青浅黑 ,或浅玄浅丹。。。姗师妹
姗师妹:这图真好看呀,嘤.....

岚师弟:这图叫啥啊师兄?
牛师兄:园槿绽红艳,郊桑柔绿滋,这个图叫桑基图,桑者,或浅黄浅白,或浅青浅黑 ,或浅玄浅丹。。。
姗师妹:嘤.......
岚师弟:???

    于是便有了“牛栏山”师兄妹们共同学习桑基图绘制的桥段




牛师兄婉婉道来:
    桑基图可以用到哪些地方呢?有调控网络的场景都用得到,除了最常常常见的ceRNA调控网络,其他调控网络比如,RNA结合蛋白、转录因子靶基因、DNA/RNA甲基化酶与靶基因,都能用得到呢。
    桑基图(Sankeydiagram)是一种特定类型的流程图,图中延伸的分支的宽度对应数据流量的大小,通常应用于能源、材料成分、金融、生物信息等数据的可视化分析。因1898年Matthew Henry Phineas Riall Sankey绘制的“蒸汽机的能源效率图”而闻名,此后便以其名字命名为“桑基图”。在桑基图中,节点用矩形表示,边用弧表示,弧的宽度与流的重要性成正比。
    有不同的方法和软件可用于创建桑基图。在R 中,我习惯使用networkd3 包画此图。第一时间展示基本的 Sankey 图,然后展示绘制进阶版自定义Sankey 图。

    学好了啊师弟师妹!看我三步搞定~

第一部分:基本桑基图

本文以创建一个有 10 个节点的桑基图为例。随机创建一个输入数据框,包含节点的数量和边的值,利用函数 sankeyNetwork() 来画桑基图。所使用的代码如下:


library(networkD3)

## create a dataframe with 10 nodes
nodes = data.frame("name" = c("Node_0", "Node_1", "Node_2", "Node_3", "Node_4", "Node_5",
                              "Node_6", "Node_7", "Node_8", "Node_9"))

## create edges with weights
links = as.data.frame(matrix(c(0, 5, 2, # node 0 -> node 5 with weight 2
                               0, 6, 1, # node 0 -> node 6 with weight 1
                               1, 7, 3, # node 1 -> node 7 with weight 3
                               2, 8, 2, # node 2 -> node 8 with weight 2
                               2, 9, 3, # node 2 -> node 9 with weight 3
                               3, 5, 1, # node 3 -> node 5 with weight 1
                               3, 9, 5, # node 3 -> node 9 with weight 5
                               4, 9, 2  # node 4 -> node 9 with weight 2
                               ), byrow = TRUE, ncol = 3))

## set column names for links
names(links) = c("source", "target", "value")

## Draw Sankey Diagram
p = sankeyNetwork(Links = links, Nodes = nodes,
 Source = "source", Target = "target",
 Value = "value", NodeID = "name",
 fontSize = 16, nodeWidth = 40)
p

代码输出图如下所示:

    在此图中,节点颜色为sankeyNetwork() 函数自动设置。

第二部分:自定义节点颜色

    现在,让k8com官网看看如何设置自定义节点颜色。为此,k8com官网将使用 d3.scaleOrdinal() 创建自定义颜色列表。所使用代码如下:

library(networkD3)

## create a dataframe with 10 nodes
nodes = data.frame("name" = c("Node_0", "Node_1", "Node_2", "Node_3", "Node_4", "Node_5",
                              "Node_6", "Node_7", "Node_8", "Node_9"))

## create edges with weights
links = as.data.frame(matrix(c(0, 5, 2, # node 0 -> node 5 with weight 2
                               0, 6, 1, # node 0 -> node 6 with weight 1
                               1, 7, 3, # node 1 -> node 7 with weight 3
                               2, 8, 2, # node 2 -> node 8 with weight 2
                               2, 9, 3, # node 2 -> node 9 with weight 3
                               3, 5, 1, # node 3 -> node 5 with weight 1
                               3, 9, 5, # node 3 -> node 9 with weight 5
                               4, 9, 2  # node 4 -> node 9 with weight 2
                               ), byrow = TRUE, ncol = 3))

## set column names for links
names(links) = c("source", "target", "value")

## Create custom color list using d3 for each node
node_color <- 'd3.scaleOrdinal() .domain(["Node_0", "Node_1", "Node_2", "Node_3", "Node_4",
"Node_5", "Node_6", "Node_7", "Node_8", "Node_9"]) .range(["red", "blue", "orange" ,
"yellow", "cyan", "green", "magenta", "dodgerblue", "pink", "black"])'

## Draw Sankey Diagram
p = sankeyNetwork(Links = links, Nodes = nodes,
 Source = "source", Target = "target",
 Value = "value", NodeID = "name",
 fontSize = 16, nodeWidth = 40,
 colourScale = node_color)
p

代码输出图如下所示:


    这样可以根据自己的需求进行节点颜色自定义,下面k8com官网将学习如何同时为节点和边自定义颜色。

第三部分:自定义节点和边颜色

    要为边设置自定义颜色,k8com官网需要在链接数据框中添加一个名为“group”的列。在此列中,k8com官网可以为每个单独的边设置类型名称。最后,k8com官网可以将这些类型合并到k8com官网的 d3.scaleOrdinal() 函数的颜色列表和 sankeyNetwork() 函数中;同时,k8com官网需要设置 LinkGroup = “group”。

所使用代码如下:

library(networkD3)

## create a dataframe with 10 nodes
nodes = data.frame("name" = c("Node_0", "Node_1", "Node_2", "Node_3", "Node_4", "Node_5",
                              "Node_6", "Node_7", "Node_8", "Node_9"))

## create edges with weights
links = as.data.frame(matrix(c(0, 5, 2, # node 0 -> node 5 with weight 2
                               0, 6, 1, # node 0 -> node 6 with weight 1
                               1, 7, 3, # node 1 -> node 7 with weight 3
                               2, 8, 2, # node 2 -> node 8 with weight 2
                               2, 9, 3, # node 2 -> node 9 with weight 3
                               3, 5, 1, # node 3 -> node 5 with weight 1
                               3, 9, 5, # node 3 -> node 9 with weight 5
                               4, 9, 2  # node 4 -> node 9 with weight 2
                               ), byrow = TRUE, ncol = 3))

## set column names for links
names(links) = c("source", "target", "value")

## add edge types for coloring purpose
links$group = c("type_0",
                "type_0",
                "type_1",
                "type_2",
                "type_2",
                "type_3",
                "type_3",
                "type_4")

## Create custom color list using d3 for each node
node_color <- 'd3.scaleOrdinal() .domain(["Node_0", "Node_1", "Node_2", "Node_3", "Node_4",
"Node_5", "Node_6", "Node_7", "Node_8", "Node_9", "type_0", "type_1", "type_2",
"type_3", "type_4"]) .range(["red", "blue", "orange" , "yellow", "cyan", "green", "magenta",
"dodgerblue", "pink", "black", "red", "blue", "orange" , "yellow", "cyan"])'

## Draw Sankey Diagram
p = sankeyNetwork(Links = links, Nodes = nodes,
 Source = "source", Target = "target",
 Value = "value", NodeID = "name",
 fontSize = 16, nodeWidth = 40,
 colourScale = node_color,
 LinkGroup="group")
P

代码输出图如下所示:


最后,保存桑基图。通常k8com官网保存为PDF文件,Rstudio 输出为PDF格式即可。如果需要保存为 HTML 文件,需要添加一个名为“htmlwidgets”的R包。请看下面的代码:

# save the widget
library(htmlwidgets)
saveWidget(p, file=paste0( getwd(), "/plots/sankeyDiagram.html"))


基础和进阶版的桑基图就完成啦,小伙伴学会了吗?


浏览:
分享
园槿绽红艳,郊桑柔绿滋——怎样用R绘制漂亮的桑基图(附源代码)
姗师妹:这图真好看呀,嘤.....岚师弟:这图叫啥啊师兄?牛师兄:园槿绽红艳,郊桑柔绿滋,这个图叫桑基图,桑者,或浅黄浅白,或浅青浅黑 ,或浅玄浅丹。。。姗师妹
长按图片保存/分享
图片展示

 电话:176-3859-9526

 邮件:market@writegene.cn

 地址:河南省郑州市管城回族区鼎盛街6号C3栋309-312

河南k8com官网生物技术有限公司  Copyright © 2020 all rights reserved

豫ICP备19046352号-1    技术支持:闪猴科技

图片展示

公司名称:河南k8com官网生物技术有限公司

公司官网:http://www.tjwxys.com

地址:河南省郑州市管城回族区鼎盛街6号C3栋309-312

图片展示
Top

特色服务 

在线留言 

在线表单

  • 姓名: *

  • 电话: *

  • 邮箱:

  • 地址:

  • 咨询项目:

  • 提交

  • 验证码
    看不清?换一张
    取消
    确定

河南k8com官网生物技术有限公司  Copyright ©  2020 All rights reserved

豫ICP备19046352号-1    技术支持:闪猴科技

客服中心
咨询电话
176-3859-9526
上班时间
周一到周五
E-mail地址
market@writegene.cn
二维码
扫码关注
添加微信好友,详细分析产品
使用企业微信
“扫一扫”加入群聊
复制成功
添加微信好友,详细分析产品
我知道了