ggsankey绘制桑基图(2)
2023-02-18 16:35:33 时间
❝本节再来介绍使用「ggsankey」绘制桑基图,下面小编就通过一个具体案例来介绍如何绘制此图;
library(tidyverse)
library(ggsankey)
library(wesanderson)
导入数据
records <- read_csv('records.txt')
drivers <- read_csv('drivers.txt')
定义调色板
pal <- wes_palette("Zissou1")
设置主题
theme_set(
theme_minimal() +
theme(
legend.title = element_blank(),
legend.key.size = unit(1, "line"),
axis.title = element_blank(),
plot.background = element_rect(fill = "grey97", color = NA),
plot.margin = margin(10, 10, 10, 10),
plot.title = element_text(face = "bold"),
plot.subtitle = element_text(color = "grey30")
)
)
数据清洗
drivers_top <- drivers %>%
filter(position < 6) %>%
mutate(
player_rec = paste0(player, " - ", total, " records"),
player_rec = fct_reorder(player_rec, -total)
)
转换数据格式
df <- records %>%
filter(player %in% drivers_top$player) %>%
make_long(player, track) %>% #使用make_long函数转化为ggsankey所需要的格式,
left_join(drivers_top %>%
distinct(player,position),
by = c("node" = "player")) %>%
mutate(color = if_else(!is.na(next_node), pal[position], "white"))
数据可视化
ggplot(df,aes(x = x, next_x = next_x, node = node,
next_node = next_node, fill = color,label = node)) +
geom_sankey() +
# 设置文本
geom_sankey_text(aes(hjust = if_else(x == "track", 1, 0)),size = 3) +
scale_fill_identity() +
coord_cartesian(expand = FALSE, clip = "off") +
theme(
legend.position = "none",
axis.text = element_blank(),
panel.grid = element_blank(),
plot.title = element_text(hjust = 0.5, size = 23, face = "bold"),
plot.caption = element_text(hjust = 0.5)
)
相关文章
- c语言从0->1入门——结构体
- 先收藏!关于Java类、接口、枚举的知识点大汇总
- Java之五种遍历Map集合的方式
- Jmeter扩展组件开发(7) - 自定义java请求的开发
- c字符串详细解析
- 断网了怎么办?那就快去体验【Microsoft Edge浏览器】提供的离线冲浪小游戏~娱乐一下
- 大一学生课设c——服装管理系统
- 1篇文章教会你MarkDown语法,从此开始博客之路
- 蓝桥杯每日一刷(第一天)
- 蓝桥杯每日一刷(第二天)
- 蓝桥杯每日一刷(第三天)
- 二叉树的三种遍历方式
- CONCATENATEX函数的不归路
- 搜索算法dfs和bfs解析(附有例题)
- 蓝桥杯每日一刷(第四天2016)
- 听说你还不会滑动窗口?来一篇文章带你学会滑动窗口算法
- Microsoft VS Code安装教程
- 肝一个周整理Java中容易混淆的基础知识
- ASICS的报表有哪些值得学习的地方?
- 蓝桥杯每日一刷(第六天)——暂会哈希