ggplot2优雅的绘制流程图
优雅 绘制 ggplot2 流程图
2023-06-13 09:13:01 时间
❝本节来介绍一个用R语言绘制流程图的案例 ❞
「代码链接」https://gist.github.com/AlbertRapp/438102c458fc8fbdffcb6feb76ff93f7 可以从网站直接获取,如果你下载网速很慢,可以从文末直接获取
代码展示
加载R包
library(tidyverse)
library(igraph)
library(showtext)
library(thematic)
# install.packages("thematic")
构建数据
dat <- tribble(
~from, ~to,
'Are you a horse?', 'No',
'Are you a horse?', 'Yes',
'Are you a horse?', 'Maybe',
'Maybe', 'How many legs\ndo you walk on?',
'Yes', 'How many legs\ndo you walk on?',
'No', 'You\'re not a horse',
'How many legs\ndo you walk on?', 'Two',
'How many legs\ndo you walk on?', 'Four',
'Two', 'You\'re not a horse_2',
'Four', 'Really?',
'Really?', 'No_2',
'Really?', 'Yes_2',
'No_2', 'Can you read\nand write?',
'Yes_2', 'Can you read\nand write?',
'Can you read\nand write?', 'Yes_3',
'Can you read\nand write?', 'No_3',
'Yes_3', 'You\'re not a horse_3',
'No_3', 'You\'re reading this,\naren\'t you?',
'You\'re reading this,\naren\'t you?', 'Yes_4',
'Yes_4', 'You\'re not a horse_4'
)
创建图形和布局
graph <- graph_from_data_frame(dat, directed = TRUE)
coords <- graph %>%
layout_as_tree() %>%
as_tibble(.name_repair = ~c('x', 'y'))
output <- coords %>%
mutate(step = vertex_attr(graph, 'name'),label = str_remove(step, '\\_.+'),
x = -2.5 * x,y = 5 * y,type = case_when(str_detect(label, '\\?') ~ "Question",
str_detect(step, 'You\'re not a horse') ~ 'Outcome',T ~ 'Answer'))
制作盒子
box_width <- 1.2
box_height <- 1.25
boxes <- output %>%mutate(xmin = x - box_width,xmax = x + box_width,ymin = case_when(
str_detect(step, '(legs|reading|write)') ~ y - 1.5 * box_height,T ~ y - box_height),
ymax = case_when(str_detect(step, '(legs|reading|write)') ~ y + 1.5 * box_height,T ~ y + box_height))
制作边文件
edges <- dat %>%
mutate(id = row_number()) %>%
pivot_longer(cols = c("from", "to"),names_to = "s_e",values_to = "step") %>%left_join(boxes, by = "step") %>%
select(-c(label, type, y, xmin, xmax)) %>%
mutate(y = ifelse(s_e == "from", ymin, ymax)) %>%
select(-c(ymin, ymax)) %>%
mutate(x = case_when(s_e == 'to' & id %in% c(5, 14) ~ x - box_width,T ~ x))
base_colors <- thematic::okabe_ito(2)
数据可视化
ggplot() +geom_path(data = edges,
aes(x,y, group = id),arrow = arrow(length = unit(0.25, 'cm'))) +
geom_rect(data = boxes,aes(xmin=xmin,xmax = xmax, ymin = ymin, ymax = ymax, fill = type)) +
geom_text(data = boxes,aes(x = x, y = y,label = label),lineheight = 1) +
theme_void() +
theme(legend.position = 'none',
plot.background = element_rect(fill = 'white', colour = NA)) +
scale_fill_manual(values = c('Question' = base_colors[1],
'Answer' = colorspace::lighten(base_colors[1], 0.5),
'Outcome' = colorspace::lighten(base_colors[2], 0.1))) +
coord_cartesian(xlim = c(-4.5,5))
相关文章
- ggplot2优雅绘制环状华夫图
- ggplot2优雅的绘制森林图
- ggplot2优雅的绘制镶嵌条形图
- ggplot2优雅的绘制多组配对箱线图
- ggplot2优雅的绘制全能型箱线图
- 还在用HttpUtil?SpringBoot 3.0全新HTTP客户端工具来了,用起来够优雅!
- ggplot2优雅绘制时间趋势变化散点图
- 如何优雅的跨 Namespace 同步 Secret 和 ConfigMap?
- 使用开源工具 k8tz 优雅设置 Kubernetes Pod 时区
- Java与MySQL配合,优雅构建数据库魔力(javamysql语句)
- 处理优雅处理Java与Redis中的过期缓存(redisjava过期)
- Docker搭建Redis优雅架构示例(dockerredis)
- Linux进程优雅退出之旅(linux进程结束)
- PHP和MySQL配合使用的优雅代码写作技巧(phpmysql代码)
- 如何优雅地清理 MySQL 注册表?(mysql清理注册表)
- 如何正确使用Linux脚本实现优雅退出?(linux脚本退出)
- 如何优雅地关闭Redis数据库(怎么关闭redis数据库)
- Oracle主键插入优雅操作实现表级唯一性(oracle主键插入)
- 在Oracle中如何优雅地查询数据(oracle中查询数据)
- 文件如何在Oracle中优雅地关闭Trace文件(oracle中关闭trc)
- 优雅使用Redis连接池的方式(redis连接池的方式)
- 优雅地使用Redis过期机制(redis过期方法)