一、桑基图介绍
桑基图(Sankey diagram),即桑基能量分流图,也叫桑基能量平衡图。它是一种特定类型的流程图,概述图中延伸的分支的宽度对应数据流量的大小,通常应用于能源、材料成分、金融等数据的可视化分析。因1898年Matthew Henry Phineas Riall Sankey绘制的“蒸汽机的能源效率图”而闻名,此后便以其名字命名为“桑基图”。
二、地学领域的桑基图
地学领域常常需要用到时间序列分析,例如某地区三个不同年份土地利用情况,有时我们不仅需要知道每一个年份的不同类的占比情况,还需要明晰土地利用转移变化的情况,常用的方法是制作转移矩阵,转移矩阵作为表格可以很好表达土地利用的转移情况,那有没有一种图可以一眼看清变化情况呢?桑基图就可以做到。它的流向可以很好的对不同地类的转化情况做可视化。下面我们就以制作土地利用变化的桑基图作为本文的示例。
三、使用R语言进行桑基图绘制
1.假设我们有2000年,2010年,2020年三期土地利用数据。首先,我们需要制作2000--2010和2010--2020的两个转移矩阵(如下图),如果有不会制作转移矩阵的小伙伴可以去查找资料,非常简单。
2.将转移矩阵转换一下格式,因为桑基图的绘制需要设置“source”,"target",“value”三个参数。转换后的格式如下图
3.编写代码,到这里我们已经做好了前期准备,让我们开始代码的编写吧!
library(networkD3)
library(dplyr)
# A connection data frame is a list of flows with intensity for each flow
links <- data.frame(
#source=c("group_A","group_A", "group_B", "group_C", "group_C", "group_E"),
#target=c("group_C","group_D", "group_E", "group_F", "group_G", "group_H"),
#value=c(2,3, 2, 3, 1, 3)
source = c("2000_1", "2000_1", "2000_1", "2000_1", "2000_1",
"2000_2", "2000_2", "2000_2", "2000_2", "2000_2",
"2000_3", "2000_3", "2000_3", "2000_3", "2000_3",
"2000_4", "2000_4", "2000_4", "2000_4", "2000_4",
"2000_5", "2000_5", "2000_5", "2000_5", "2000_5",
"2010_1", "2010_1", "2010_1", "2010_1", "2010_1",
"2010_2", "2010_2", "2010_2", "2010_2", "2010_2",
"2010_3", "2010_3", "2010_3", "2010_3", "2010_3",
"2010_4", "2010_4", "2010_4", "2010_4", "2010_4",
"2010_5", "2010_5", "2010_5", "2010_5", "2010_5"),
target = c("2010_1", "2010_2", "2010_3", "2010_4", "2010_5",
"2010_1", "2010_2", "2010_3", "2010_4", "2010_5",
"2010_1", "2010_2", "2010_3", "2010_4", "2010_5",
"2010_1", "2010_2", "2010_3", "2010_4", "2010_5",
"2010_1", "2010_2", "2010_3", "2010_4", "2010_5",
"2020_1", "2020_2", "2020_3", "2020_4", "2020_5",
"2020_1", "2020_2", "2020_3", "2020_4", "2020_5",
"2020_1", "2020_2", "2020_3", "2020_4", "2020_5",
"2020_1", "2020_2", "2020_3", "2020_4", "2020_5",
"2020_1", "2020_2", "2020_3", "2020_4", "2020_5"),
value = c(23419877, 1144255, 21295, 4564, 4130,
2720556, 14248249, 1053253, 86695, 36967,
78254, 3193646, 4743822, 803199, 179356,
21885, 438630, 2797731, 3528925, 1533316,
28602, 139432, 1093465, 4846975, 81766727,
25774551, 749247, 19469, 3701, 2221,
4006399, 13641523, 1542749, 101487, 20003,
102677, 3164185, 4935158, 1333107, 243882,
32020, 475142, 3036910, 4001235, 1810772,
38760, 157066, 1105013, 4712520, 78157451)
)
# From these flows we need to create a node data frame: it lists every entities involved in the flow
nodes <- data.frame(
name=c(as.character(links$source),
as.character(links$target)) %>% unique()
)
# With networkD3, connection must be provided using id, not using real name like in the links dataframe.. So we need to reformat it.
links$IDsource <- match(links$source, nodes$name)-1
links$IDtarget <- match(links$target, nodes$name)-1
# Make the Network
p <- sankeyNetwork(Links = links, Nodes = nodes,
Source = "IDsource", Target = "IDtarget",
Value = "value", NodeID = "name",
sinksRight=FALSE)
p
# save the widget
library(htmlwidgets)
saveWidget(p, "G:/CLCD30/sankey.html")
四、Result展示
到这里,本篇文章关于使用R语言绘制桑基图的教程就结束了,创作不易,希望大家多多支持,点赞转发!我是加拿大一枝黄花,我们下次再会。