使用 sankeywheel / highcharter 绘制交互式桑基图和和弦图

使用 sankeywheel / highcharter 绘制交互式桑基图和和弦图

这里提供两种方法,方法一是通过 sankeywheel 包,这个包的使用比较简单,但是自定义程度比较低,另外一个是使用 highcharter 包,代码虽然复杂了点,但是自定义程度很高。

sankeywheel:绘制桑基图和和弦图

sankeywheel 可以用于快速绘制桑基图和和弦图,安装:

1
install.packages('sankeywheel')

示例数据:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
library(sankeywheel)
sankeydf -> df
df
#> # A tibble: 46 x 3
#> from to weight
#> <chr> <chr> <dbl>
#> 1 Brazil Portugal 5
#> 2 Brazil France 1
#> 3 Brazil Spain 1
#> 4 Brazil England 1
#> 5 Canada Portugal 1
#> 6 Canada France 5
#> 7 Canada England 1
#> 8 Mexico Portugal 1
#> 9 Mexico France 1
#> 10 Mexico Spain 5
#> # … with 36 more rows

使用方法有两种:

  1. 绘制桑基图:
1
2
3
4
sankeywheel(from = sankeydf$from,
to = sankeydf$to,
weight = sankeydf$weight,
type = "sankey")
  1. 绘制和弦图:
1
2
3
4
sankeywheel(from = sankeydf$from,
to = sankeydf$to,
weight = sankeydf$weight,
type = "dependencywheel")
  1. 在 Shiny Apps 中使用:
1
2
3
dir <- system.file("examples", "sankeywheel", package = "sankeywheel")
setwd(dir)
shiny::shinyAppDir(".")

该包的使用教程可以参考:https://cran.r-project.org/web/packages/sankeywheel/vignettes/sankeywheel.html

组合 HTML 控件可以这样:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
library(manipulateWidget)
combineWidgets(
sankeywheel(from = sankeydf$from,
to = sankeydf$to,
weight = sankeydf$weight,
type = "sankey",
width = "100%",
theme = "sunset",
title = "SUNSET"),
sankeywheel(from = sankeydf$from,
to = sankeydf$to,
weight = sankeydf$weight,
type = "dependencywheel",
width = "100%",
theme = "sunset",
title = "SUNSET"),
byrow = TRUE, ncol = 2, width = "100%", height = "300px"
)

同样,不再建议使用这个包了,highcharter 可以更好的绘制这两幅图。

使用 highcharter 绘制桑基图和和弦图

  1. 绘制桑基图:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
library(highcharter)

# 桑基图
highchart() %>%
hc_chart(type = 'sankey') %>%
hc_add_series(keys = c('from', 'to', 'weight'),
data = list_parse2(df),
name = "Sankey demo series") %>%
hc_title(text = 'Highcharts 桑基图') %>%
hc_add_theme(hc_theme_sandsignika(
chart = list(
divBackgroundImage = NULL,
style = list(background = "url(https://www.highcharts.com/samples/graphics/sand.png)",
fontFamily = "Source Han Serif")
)
)) %>%
hc_exporting(enabled = T) %>%
hc_credits(enabled = T,
text = "©RStata 绘制",
href = "https://tidyfriday.cn")


  1. 绘制和弦图:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
highchart() %>% 
hc_chart(type = 'dependencywheel') %>%
hc_add_series(keys = c('from', 'to', 'weight'),
data = list_parse2(df),
name = "Demo series",
dataLabels = list(
color = '#333',
distance = 20
),
size = '95%') %>%
hc_title(text = 'Highcharts 和弦图') %>%
hc_add_theme(hc_theme_sandsignika(
chart = list(
divBackgroundImage = NULL,
style = list(background = "url(https://www.highcharts.com/samples/graphics/sand.png)",
fontFamily = "Source Han Serif")
)
)) %>%
hc_exporting(enabled = T) %>%
hc_credits(enabled = T,
text = "©RStata 绘制",
href = "https://tidyfriday.cn")


代码虽多,但是好用!

阅读更多:https://mp.weixin.qq.com/s/EhKAP3Zgc47NLU7co-n6Ug

使用 sankeywheel / highcharter 绘制交互式桑基图和和弦图

https://tidyfriday.cn/posts/47133/

作者

Painter

发布于

2021-02-12

更新于

2021-05-31

许可协议

评论