ggsankey绘制流动的桑基图
2021-04-16 本文已影响0人
R语言数据分析指南
我们都知道常见的桑基图只能表示A-B,B-C,但当我们有多组数据需要进行展示很明显传统的桑基图已经不能满足我们的需求了,感谢瑞典科学家David Sjoberg开发了ggsanke包,下面通过几个案例来展示如何绘制流动的桑基图
Google 将sankey定义为:
sankey图是一种可视化视图,用于描述从一组值到另一组值的流程。被连接的事物称为节点,而连接称为链接。当您要显示一组数据的多个路径之间的多对多映射时,最好使用Sankey
请看下图方便对sankey进行理解
ggsankey基本用法
devtools::install_github("davidsjoberg/ggsankey")
library(ggsankey)
library(tidyverse)
install.packages("gapminder")
library(gapminder)
colors <- c("#3B9AB2","#78B7C5","#EBCC2A","#E1AF00","#F21A00",
"#ECCBAE","#046C9A","#D69C4E","#ABDDDE","#000000")
df <- mtcars %>%
make_long(cyl, vs, am, gear, carb)
ggplot(df, aes(x = x,
next_x = next_x,
node = node,
next_node = next_node,
fill = factor(node))) +
geom_sankey()+
scale_fill_manual(values = colors)+
theme_minimal()+labs(x=NULL)
ggplot(df, aes(x = x, next_x = next_x,
node = node, next_node = next_node,
fill = factor(node), label = node)) +
geom_sankey(flow.alpha = .6,
node.color = "gray30") +
geom_sankey_label(size = 3, color = "white", fill = "gray40") +
scale_fill_manual(values = colors)+
theme_sankey(base_size = 18) +
labs(x = NULL) +
theme(legend.position = "none",
plot.title = element_text(hjust = .5)) +
ggtitle("Car features")
geom_alluvial;冲积图非常类似于sankey图,但是节点之间没有空格
ggplot(df, aes(x = x, next_x = next_x, node = node,
next_node = next_node,
fill = factor(node), label = node)) +
geom_alluvial(flow.alpha = .6) +
geom_alluvial_text(size = 4, color = "black") +
scale_fill_manual(values = colors)+
theme_alluvial(base_size = 18) +
labs(x = NULL) +
theme(legend.position = "none",
plot.title = element_text(hjust = .5)) +
ggtitle("Car features")
df <- gapminder %>%
group_by(continent, year) %>%
summarise(gdp = (sum(pop * gdpPercap)/1e9) %>%
round(0)) %>%
ungroup()
geom_sankey_bump;Sankey凹凸曲线图是凹凸曲线图和sankey的混合体,对于时间序列最有用。当一个组变得比另一个大时,它会在其上方碰撞
ggplot(df, aes(x = year,
node = continent,
fill = continent,
value = gdp)) +
geom_sankey_bump(space = 0, type = "alluvial",
color = "transparent", smooth = 6) +
scale_fill_manual(values = colors)+
theme_sankey_bump(base_size = 16) +
labs(x = NULL,
y = "GDP ($ bn)",
fill = NULL,
color = NULL) +
theme(legend.position = "bottom") +
labs(title = "GDP development per continent")