前往小程序,Get更优阅读体验!
立即前往
首页
学习
活动
专区
工具
TVP
发布
社区首页 >专栏 >ggalluvial绘制桑基图

ggalluvial绘制桑基图

作者头像
作图丫
发布2022-03-29 10:59:34
2.8K0
发布2022-03-29 10:59:34
举报
文章被收录于专栏:作图丫

导语

GUIDE ╲

桑基图(Sankey diagram),即桑基能量分流图,也叫桑基能量平衡图。

背景介绍

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

数据介绍

冲积图(桑基图)使用变化的宽条带和堆叠条形图来表示具有分类或顺序变量的多维或重复测量数据。

冲积图主要由这几个组分组成。冲积图由多个水平分布的柱(axes)表示因子变量,这些轴的垂直划分(strata)表示变量的值;曲线(alluvial flows)连接着相邻轴层内的垂直细分(lodes),表示取相应变量的相应值的观测子集或观测量。

1. alluvial-data函数检查数据

alluvial-data这个函数用来检查数据框的两种类型的冲积结构。to_lodes_form在数据框中指定几个变量作为坐标轴,并对该数据框进行重塑,使坐标轴变量名构成一个新的因子变量,其值构成另一个因子变量。其他变量的值将被重复,并且可以引入行分组变量。

to_alluvia_form取一个包含要用于冲积图的轴和轴值变量的数据框,对数据框进行重塑,使轴组成单独的变量,其值由值变量给出。

示例:

代码语言:javascript
复制
data(majors)
head(majors)
代码语言:javascript
复制
# 生成lodes格式
data(majors)
head(majors)
is_lodes_form(majors,
              key = "semester", value = "curriculum", id = "student")

#生成alluvia格式
majors_alluvia <- to_alluvia_form(majors,
                                  key = "semester", value = "curriculum",
                                  id = "student")
head(majors_alluvia)
#key,semester列是代表坐标轴axis
#value,curriculum列是层
#id,student列是冲积流

2. 绘图

geom_alluvium接收到冲积图的lodes位置数据,包括水平(x)和垂直(y, ymin, ymax)位置,以及冲积流与strata的交叉点。它使用geom_lode()绘制lodes,并使用geom_flow()绘制它们之间的流。

geom_stratum接收到冲积图的strata位置数据,包括水平(x)和垂直(y, ymin, ymax)位置,它为这些地层画出一定宽度的矩形

示例:

代码语言:javascript
复制
gg <- ggplot(majors_alluvia,
             aes(axis1 = CURR1, axis2 = CURR7, axis3 = CURR13))
#定义三条strate层
gg +
  #冲击流:
  geom_alluvium(aes(fill = as.factor(student)), width = 2/5, discern = FALSE) +
  #width,宽度所占图的比例,默认1/3

  #strate层:
  geom_stratum(width = 2/5, discern = FALSE) +

  #添加文本
  geom_text(stat = "stratum", discern = FALSE,
            aes(label = after_stat(stratum)))

3. 直线冲积图

代码语言:javascript
复制
ggplot(as.data.frame(Titanic),
       aes(y = Freq,
           axis1 = Class, axis2 = Sex, axis3 = Age,
           color = Survived)) +
  stat_stratum(geom = "errorbar") +
  #计算每个轴上strata矩心(x和y)和高度(ymin和ymax)
  #geom用几何对象来显示数据
  geom_line(stat = "alluvium") +
  stat_alluvium(geom = "pointrange") +
  #计算积层的矩心(x和y)和高度(ymin和ymax),冲积流与strata的交点
  geom_text(stat = "stratum", aes(label = after_stat(stratum))) +
  scale_x_discrete(limits = c("Class", "Sex", "Age"))

其他示例

1.

代码语言:javascript
复制
head(as.data.frame(UCBAdmissions))
#判断数据形式是否符合绘图
is_alluvia_form(as.data.frame(UCBAdmissions), axes = 1:3, silent = TRUE)
#silent是否打印messages
ggplot(as.data.frame(UCBAdmissions),
       aes(y = Freq, axis1 = Gender, axis2 = Dept)) +
  geom_alluvium(aes(fill = Admit), width = 1/12) +
  geom_stratum(width = 1/12, fill = "black", color = "grey") +
  #fillc冲积层的填充颜色,color冲积层边框颜色
  geom_label(stat = "stratum", aes(label = after_stat(stratum))) +
  #冲积层添加标签
  scale_x_discrete(limits = c("Gender", "Dept"), expand = c(.05, .05)) +
  #设置x轴
  scale_fill_brewer(type = "qual", palette = "Set1") +
  设置颜色
  ggtitle("UC Berkeley admissions and rejections, by sex and department")
 

2.

代码语言:javascript
复制
ggplot(as.data.frame(Titanic),
       aes(y = Freq,
           axis1 = Survived, axis2 = Sex, axis3 = Class)) +
  geom_alluvium(aes(fill = Class),
                width = 0, knot.pos = 0.1, reverse = FALSE) +
  #knot.pos,冲击流条节点到各地层的水平距离(距离轴线的宽度/2)
  #reverse是否按照变量值的相反顺序排列各轴上的strata层,使其与图例中值的顺序相匹配。
  guides(fill = FALSE) +
  geom_stratum(width = 1/8, reverse = FALSE) +
  geom_text(stat = "stratum", aes(label = after_stat(stratum)),
            reverse = FALSE) +
  scale_x_continuous(breaks = 1:3, labels = c("Survived", "Sex", "Class")) +
  coord_flip() +
  #翻转
  ggtitle("Titanic survival by class and sex")

3.

巧妙的设置透明度,图形会变得小清新又可爱哦!

代码语言:javascript
复制
library(alluvial)
data(Refugees, package = "alluvial")\
head(Refugees)
代码语言:javascript
复制

country_regions <- c(
  Afghanistan = "Middle East",
  Burundi = "Central Africa",
  `Congo DRC` = "Central Africa",
  Iraq = "Middle East",
  Myanmar = "Southeast Asia",
  Palestine = "Middle East",
  Somalia = "Horn of Africa",
  Sudan = "Central Africa",
  Syria = "Middle East",
  Vietnam = "Southeast Asia"
)
Refugees$region <- country_regions[Refugees$country]
ggplot(data = Refugees,
       aes(x = year, y = refugees, alluvium = country)) +
  geom_alluvium(aes(fill = country, colour = country),
                alpha = .75, decreasing = FALSE) +
  #alpha,透明度
  #decreasing是否按变量值(NA,默认值)的顺序在每个轴上排列strata,按升序排列
  scale_x_continuous(breaks = seq(2003, 2013, 2)) +
  #划分x轴
  theme_bw() +
  #设置主题
  theme(axis.text.x = element_text(angle = -30, hjust = 0)) +
  #横坐标注释旋转
  scale_fill_brewer(type = "qual", palette = "Set3") +
  #q曲线填充颜色
  scale_color_brewer(type = "qual", palette = "Set3") +
  #曲线边框颜色
  facet_wrap(~ region, scales = "fixed") +
  #分页
  ggtitle("refugee volume by country and region of origin")

4.

适当加粗边框,并选用饱和度较高的颜色颜色搭配其透明色,会增加图形高级感!

代码语言:javascript
复制
data(majors)
majors$curriculum <- as.factor(majors$curriculum)
ggplot(majors,
       aes(x = semester, stratum = curriculum, alluvium = student,
           fill = curriculum, label = curriculum)) +
  scale_fill_brewer(type = "qual", palette = "Set2") +
  #type = "qual"离散型颜色
  geom_flow(stat = "alluvium", lode.guidance = "frontback",
            color = "darkgray") +
  #lode.guidance在每个层中排序lodes,或标识函数的字符串
  #lode.guidance="zigzag", "frontback", "backfront", "forward", and "backward"
  #darkgray冲积流边框色
  geom_stratum() +
  theme(legend.position = "bottom") +
  #图例位置
  ggtitle("student curricula across several semesters")

5.

代码语言:javascript
复制
data(vaccinations)
levels(vaccinations$response) <- rev(levels(vaccinations$response))
ggplot(vaccinations,
       aes(x = survey, stratum = response, alluvium = subject,
           y = freq,
           fill = response, label = response)) +
  scale_x_discrete(expand = c(.1, .1)) +
  geom_flow() +
  geom_stratum(alpha = .5) +
  geom_text(stat = "stratum", size = 3) +
  theme(legend.position = "none") +
  ggtitle("vaccination survey responses at three points in time")

小编总结

桑基图是可以用于生信分析的统计学展示和流程展示等方面,也算是比较常见的展示方法。仔细挖掘,可以使用ggalluvial包绘制出满满高级感的图型哦!

本文参与 腾讯云自媒体同步曝光计划,分享自微信公众号。
原始发表:2020-08-31,如有侵权请联系 cloudcommunity@tencent.com 删除

本文分享自 作图丫 微信公众号,前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文参与 腾讯云自媒体同步曝光计划  ,欢迎热爱写作的你一起参与!

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档