R 可视乎|优雅的绘制流程图

简介: R 可视乎|优雅的绘制流程图


代码展示

加载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))


477fea0b0aa81f5a9c689093d24da2c9.png

目录
相关文章
|
存储 人工智能 API
DeepSeek——DeepSeek模型部署实战
本文介绍了DeepSeek大模型的本地部署方法、使用方式及API接入。首先,通过下载Ollama平台部署DeepSeek-R1模型,提供7种不同参数版本(1.5b至671b),用户可根据硬件选择合适的模型大小。接着,文章详细描述了如何在终端运行命令启动模型,并通过Chatbox官网下载并接入DeepSeek API,实现本地和云端模型的交互。最后,提及了DeepSeek官网和集成工具如POE的使用,帮助用户更好地利用DeepSeek进行开发和应用。
|
监控 安全 生物认证
网络安全中的身份认证与访问控制技术详解
【6月更文挑战第30天】网络安全聚焦身份认证与访问控制,确保合法用户身份并限制资源访问。身份认证涉及生物和非生物特征,如密码、指纹。访问控制通过DAC、MAC、RBAC策略管理权限。最佳实践包括多因素认证、定期更新凭证、最小权限、职责分离和审计监控。这些措施旨在增强系统安全,防范未授权访问。
2769 2
|
人工智能 自然语言处理 算法
大模型 is all you need?
本文旨在通过对业务案例、prompt技巧、大模型原理等探讨,让大家能够更好地在实际业务中应用大模型。
889 16
【AD速成】半小时入门AltiumDesigner之绘制PCB(五)
【AD速成】半小时入门AltiumDesigner之绘制PCB(五)
|
Java
记一个 Base64 有关的 Bug
了解一下,下一次用 Java 开发 Base64 有关的逻辑的时候注意绕过。
565 2
|
easyexcel Java API
SpringBoot集成EasyExcel 3.x:高效实现Excel数据的优雅导入与导出
SpringBoot集成EasyExcel 3.x:高效实现Excel数据的优雅导入与导出
3345 1
|
SQL 安全 网络安全
网络安全攻防实战:黑客与白帽子的较量
【6月更文挑战第29天】网络安全战场,黑客与白帽子的博弈日益激烈。黑客利用漏洞扫描、DDoS、SQL注入等手段发起攻击,而白帽子则通过防火墙、入侵检测、数据加密等技术防守。双方在技术与智慧的较量中,未来将更多融入AI、区块链等先进技术,提升攻防效率与安全性。面对网络威胁,提升技能与意识至关重要。
|
算法 安全 虚拟化
|
vr&ar 开发工具 图形学
Unity引擎更新收费模式:从收入分成转向游戏安装量,将会有哪些影响呢
Unity引擎更新收费模式:从收入分成转向游戏安装量,将会有哪些影响呢
|
弹性计算 运维 Cloud Native
容器镜像服务ACR
学习摘要
1507 0
容器镜像服务ACR

热门文章

最新文章