美文网首页ggplot2绘图
利用ggforce绘制桑基图(Sankey diagram)

利用ggforce绘制桑基图(Sankey diagram)

作者: shenghuanjing | 来源:发表于2020-09-23 23:49 被阅读0次

什么是桑基图?

百度百科:桑基图(Sankey diagram),即桑基能量分流图,也叫桑基能量平衡图。它是一种特定类型的流程图,图中延伸的分支的宽度对应数据流量的大小,通常应用于能源、材料成分、金融等数据的可视化分析。因1898年Matthew Henry Phineas Riall Sankey绘制的蒸汽机的能源效率图而闻名,此后便以其名字命名为“桑基图”。
Wikipedia: Sankey diagrams are a type of flow diagram in which the width of the arrows is proportional to the flow rate.

ggforce

这里使用的是Titanic数据集,同时会用到reshape2,tidyr包。

package <- c("tidyr", "reshape2", "ggforce")
install.packages(package)
library(reshape2)
library(ggforce)
library(tidyr)
data <- reshape2::melt(Titanic)
 head(data)
#  Class    Sex   Age Survived value
# 1   1st   Male Child       No     0
# 2   2nd   Male Child       No     0
# 3   3rd   Male Child       No    35
# 4  Crew   Male Child       No     0
# 5   1st Female Child       No     0
# 6   2nd Female Child       No     0
data <- gather_set_data(data, 1:4)
head(data)
#   Class    Sex   Age Survived value id     x    y
# 1   1st   Male Child       No     0  1 Class  1st
# 2   2nd   Male Child       No     0  2 Class  2nd
# 3   3rd   Male Child       No    35  3 Class  3rd
# 4  Crew   Male Child       No     0  4 Class Crew
# 5   1st Female Child       No     0  5 Class  1st
# 6   2nd Female Child       No     0  6 Class  2nd
ggplot(data, aes(x, id = id, split = y, value = value)) +
  geom_parallel_sets(aes(fill = Sex), alpha = 0.3, axis.width = 0.1) +
  geom_parallel_sets_axes(axis.width = 0.3, colour = "black", fill = "white" ) + 
# colour和fill分别修改边框色和填充色
  geom_parallel_sets_labels(colour = 'black', angle = 0)+
# angle修改方框填充文字的角度
  theme_classic()

结果如下图所示


image.png

参考链接
ggforce: Visual Guide
百度百科:桑基图
Wikipedia: Sankey diagram

相关文章

网友评论

    本文标题:利用ggforce绘制桑基图(Sankey diagram)

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