R包:DiagrammeR流程图

news2024/9/21 14:44:53

介绍

DiagrammeR 依赖于图形描述语言 Graphviz,可以通过 R 包 igraph 和 visNetwork 访问。DiagrammeR 通过将有效的图规范以 DOT 语言的形式传递给 grViz() 函数来输出图。

加载R包

采用DiagrammeRR包,它提供了以下函数:

  • 使用create_graph()render_graph()在节点和边的列表上。

  • 创建一个有效的DOT语言图规范,并将此作为字符串传递给函数grViz()

  • 创建一个有效的图规范,并将此作为字符串或 .mmd 文件引用传递给函数 mermaid()。

knitr::opts_chunk$set(message = FALSE, warning = FALSE)

if (!require("pacman")) {
  install.packages("pacman")
}
pacman::p_load(here,
               tidyverse,
               DiagrammeR,
               DiagrammeRsvg,
               rsvg, 
               xml2,
               plotly)

rm(list = ls())
options(stringsAsFactors = F)
options(future.globals.maxSize = 10000 * 1024^2)

create_graph创建图

简单的图可以通过在节点和边的数据框列表上使用 create_graph() 方法来创建。这种方法对于绘制对象之间的简单关系非常方便。

# 定义图边和节点 
# 1 == "Photosynthesis"
# 2 == "Aerobic respiration"  
# 3 == "Solar energy"  

edges <- create_edge_df(from = c(1, 2, 3), 
                        to = c(2, 1, 1), 
                        label = c("Oxygen \n Glucose", "Carbon dioxide \n Water", NA),
                        color = "black") 

nodes <- create_node_df(n = 3,
                        label = c("Photosynthesis", "Aerobic \n respiration", "Solar \n energy"),
                        type = "lower",
                        style = "filled",
                        fillcolor = c("darkseagreen", "mistyrose", "gold"), 
                        fontcolor = "black",
                        shape = "circle",
                        fixedsize = T)

# 创建图
simple_graph <- create_graph(nodes_df = nodes, edges_df = edges)  

# 渲染图
render_graph(simple_graph)

# 导出图
# export_graph(simple_graph,
#              file_name = here("figures", "dv-using_diagrammer-simple_flowchart.svg"),
#              file_type = "svg")

如果您想创建更复杂的流程图,这种方法存在一些限制。

  • 边必须使用到和从的数值向量进行编码。
  • 没有简单的方法来指定节点位置(将太阳能显示为顶部节点是有意义的)。
  • 没有简单的方法来指定边标签的位置。
  • 如果节点标签太长,图形输出会被修剪。

grVis创建流程图

创建流程图

simple_flowchart <- grViz(
  "digraph {
         
    graph[layout = dot, rankdir = LR] # Set node direction
                     
    node[shape = circle, style = filled] # Set global node attributes 
                     
    a[label = 'Photosynthesis', fillcolor = 'darkseagreen']
    b[label = 'Aerobic \nrespiration', fillcolor = 'mistyrose']
    c[label = 'Solar \nenergy', fillcolor = 'gold']

    edge[color = black, fontsize = 8] # Set global edge attributes  
                     
    a -> b[label = 'Oxygen \nGlucose']
    a -> b[style = 'invis']
    a -> b[style = 'invis']
    b -> a[label = 'Carbon dioxide \n Water']
    b -> a[style = 'invis']
    b -> a[style = 'invis']
    c -> a[label = '                       ']
                     
  }"
)

simple_flowchart

# # Export graph as an svg -------------------------------------------------------
# simple_flowchart %>% 
#   export_svg %>%
#   charToRaw %>%
#   rsvg_svg(here("figures", "dv-using_diagrammer-simple_grvis.svg"))

  • 为节点和边设置全局属性,并在需要时使用局部属性手动覆盖个别节点或边。
  • 按照这里推荐的方法,创建额外的不可见边层以增加特定节点之间的箭头曲率。
  • 创建一个带有空字符串的边标签,以增加特定边的长度。

grVis创建复杂流程图

  • 机器学习流程图
# Create a flow chart to describe my ML workflow -------------------------------  
ml_workflow <- grViz(
  "digraph {
         
    graph[layout = dot]
                     
    node[shape = rectangle, style = filled, fillcolor = 'blanchedalmond'] 
                     
    data_source_1[label = 'Data \n warehouse', shape = cylinder, fillcolor = 'azure']
    data_source_2[label = 'Survey.csv', fillcolor = 'aliceblue']
    data_source_3[label = 'External data \n API', shape = cylinder, fillcolor = 'azure']
                  
    process_1[label = 'Data cleaning \n and joining']
    process_2[label = 'Feature selection']
    process_3[label = 'Split train \n and test dataset']  
    process_4[label = 'Cross-validation']  
    process_5[label = 'Test different \n ML algorithms']
    process_6[label = 'Select optimal \n ML model']
                     
    file_1[label = 'Clean data', fillcolor = 'aliceblue']
    file_2[label = 'Training data', fillcolor = 'aliceblue']
    file_3[label = 'Test data', fillcolor = 'aliceblue']
                     
    product_1[label = 'Optimal \n ML model', shape = 'ellipse', fillcolor = 'lightsalmon']
                     
    edge[color = black, fontsize = 12] 
                     
    data_source_1 -> process_1
    data_source_2 -> process_1
    data_source_3 -> process_1  
                     
    process_1 -> file_1
                     
    file_1 -> process_2
    process_2 -> process_3
    process_3 -> file_2
    process_3 -> file_3
                     
    file_2 -> process_4
    process_4 -> process_5
    process_5 -> process_4
                     
    process_5 -> file_3
    file_3 -> process_6
    process_6 -> product_1
                     
    product_1 -> file_2[label = '      Constantly monitor \n for ML model drift',
                        style = 'dashed', penwidth = 2, weight = 2, 
                        color = 'firebrick', fontcolor = 'firebrick']
                     
  }"
)

ml_workflow


# # Export graph as an svg -------------------------------------------------------
# ml_workflow %>% 
#   export_svg %>%
#   charToRaw %>%
#   rsvg_svg(here("figures", "dv-using_diagrammer-ml_workflow.svg"))

在这里插入图片描述

  • 临床试验图
# Create flow chart with flexible parameter inputs -----------------------------  
set.seed(111)

a <- 200 # Total patients
b <- sample(1:60, 1) # Excluded patients
c <- 200 - b # Randomised patients 
d <- ceiling(c/2) # Assigned treatment A
e <- c - d # Assigned treatment B

# Store parameters inside a list
flow_chart_data <- list(a, b, c, d, e) 

# Create grVis() graph
simple_trial <- grViz(
  "digraph {
         
    graph[layout = dot]
                     
    node[shape = rectangle, style = filled, margin = 0.2, fillcolor = 'aliceblue']  
                     
    a[label = '@@1']
    b[label = '@@2', fillcolor = 'mistyrose']
    c[label = '@@3']
    d[label = '@@4']
    e[label = '@@5']

    edge[color = black]   
                
    a -> b[style = 'dashed']
    a -> c[weight = 2] # Weighs this edge more heavily so it is centrally placed 
    c -> d
    c -> e
                     
  }
                     
  [1]: paste0('Patients screened (n = ', flow_chart_data[[1]], ')')
  [2]: paste0('Patients excluded (n = ', flow_chart_data[[2]], ')')
  [3]: paste0('Patients randomised (n = ', flow_chart_data[[3]], ')')
  [4]: paste0('  Patients assigned Treatment A (n = ', flow_chart_data[[4]], ')')
  [5]: paste0('  Patients assigned Treatment B (n = ', flow_chart_data[[5]], ')')

  "
) 

simple_trial

# # Export graph as an svg -------------------------------------------------------
# simple_trial %>% 
#   export_svg %>%
#   charToRaw %>%
#   rsvg_svg(here("figures", "dv-using_diagrammer-flowchart_clinical_single.svg"))

在这里插入图片描述

  • 多研究中心临床试验图
# Create flow chart with flexible parameter inputs -----------------------------  
set.seed(111)

a <- 200
b <- sample(1:60, 1)
c <- 200 - b
d <- ceiling(c/2)
e <- c - d

set.seed(222)

f <- 125
g <- sample(1:45, 1)
h <- 125 - b
i <- ceiling(h/2)
j <- h - i

# Store subgroup parameters in separate lists  
hospital_a <- list(a, b, c, d, e) 
hospital_b <- list(f, g, h, i, j)

# Create grVis() graph  
multi_trial <- grViz(
  "digraph {

    graph[layout = 'dot', rankdir = TB]
               
    node[shape = rectangle, style = filled, margin = 0.2, fillcolor = 'azure']
               
    subgraph cluster_a { 
      graph[rankdir = TB, label = 'Hospital A',
            fontsize = 18, shape = rectangle, style = dashed]

      a[label = '@@1']
      b[label = '@@2', fillcolor = 'mistyrose']
      c[label = '@@3']
      d[label = '@@4', fillcolor = 'aliceblue']
      e[label = '@@5', fillcolor = 'aliceblue']
               
    }
               
    subgraph cluster_b { 
      graph[rankdir = TB, label = 'Hospital B',
            fontsize = 18, shape = rectangle, style = dashed]
             
      f[label = '@@6']
      g[label = '@@7', fillcolor = 'mistyrose']
      h[label = '@@8']
      i[label = '@@9', fillcolor = 'aliceblue']
      j[label = '@@10', fillcolor = 'aliceblue']
               
    }
               
    edge[color = black]
               
    a -> b[style = 'dashed']
    a -> c[weight = 2] 
    c -> d
    c -> e
    f -> g[style = 'dashed']
    f -> h[weight = 2] 
    h -> i
    h -> j
               
  }
      
  [1]: paste0('Patients screened (n = ', hospital_a[[1]], ')')
  [2]: paste0('Patients excluded (n = ', hospital_a[[2]], ')')
  [3]: paste0('Patients randomised (n = ', hospital_a[[3]], ')')
  [4]: paste0('  Patients assigned Treatment A (n = ', hospital_a[[4]], ')')
  [5]: paste0('  Patients assigned Treatment B (n = ', hospital_a[[5]], ')')
  [6]: paste0('Patients screened (n = ', hospital_b[[1]], ')')
  [7]: paste0('Patients excluded (n = ', hospital_b[[2]], ')')
  [8]: paste0('Patients randomised (n = ', hospital_b[[3]], ')')
  [9]: paste0('  Patients assigned Treatment A (n = ', hospital_b[[4]], ')')
  [10]: paste0('  Patients assigned Treatment B (n = ', hospital_b[[5]], ')')

  "
)  

multi_trial

# # Export graph as an svg -------------------------------------------------------
# multi_trial %>% 
#   export_svg %>%
#   charToRaw %>%
#   rsvg_svg(here("figures", "dv-using_diagrammer-flowchart_clinical_multiple.svg"))

在这里插入图片描述

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处:http://www.coloradmin.cn/o/1931241.html

如若内容造成侵权/违法违规/事实不符,请联系多彩编程网进行投诉反馈,一经查实,立即删除!

相关文章

亲子母婴行业媒体邀约宣发资源

传媒如春雨&#xff0c;润物细无声&#xff0c;大家好&#xff0c;我是51媒体网胡老师。 亲子母婴行业是一个综合性的产业&#xff0c;涉及多个领域&#xff0c;包括儿童食品&#xff0c;儿童玩具&#xff0c;服装&#xff0c;洗护&#xff0c;月子中心&#xff0c;母婴护理&a…

单臂路由组网实验,单臂路由的定义、适用情况、作用

一、定义 单臂路由是指通过在路由器的一个接口上配置许多子接口,从而实现原来相互隔离的不同VLAN之间的互通。 子接口:把路由器上的实际的物理接口划分为多个逻辑上的接口,这些被划分的逻辑接口就是子接口。 二、适用情况 用在没有三层交换机,却要实现不同VLAN之间的互…

怎样恢复删除的视频?简单几步迅速上手!

在短视频时代&#xff0c;视频成为我们生活中不可缺少的一部分。它记录了我们生活中的点点滴滴&#xff0c;承载着许多美好的回忆。如果一不小心删除了重要的视频&#xff0c;那将是一个巨大的缺憾。那我们该怎么恢复删除的视频呢&#xff1f;在电脑删掉的视频还能恢复吗&#…

Java基础笔记(面试题)

一、Tomcat中为什么要使用自定义类加载器 Tomcat中可以放多个Java项目的jar文件&#xff0c;如果每个jar文件中都有一个User的类&#xff0c;那么User类在没有自定义类加载器的情况下是只能加载一次&#xff1b;想要加载多次&#xff0c;只能自定义类加载器 二、JDK、JRE、JVM…

如何实现本地文件存储

文章目录 1. 知识回顾2. 示例代码 我们在上一章回中介绍了"如何实现文件存储"相关的内容&#xff0c;本章回中将继续介绍与此相关的内容.闲话休提&#xff0c;让我们一起Talk Flutter吧。 1. 知识回顾 我们上一章回中介绍了实现文件存储的三个步骤&#xff0c;不过限…

vue调试工具没有Pinia模块解决办法

修改前&#xff0c;调试工具里面没有Pinia模块 在项目的入口文件mian.js中这样修改 import /assets/main.scss import pinia from /stores/index import { createApp } from vue import App from ./App.vue import router from ./router import elFormRenderer from el-form…

详解 BGE-M3 与 Splade 模型

详解 BGE-M3 与 Splade 模型 本文将探索两种先进的 Embedding 模型——BGE-M3 和 Splade&#xff0c;深入解析它们的设计理念和工作原理。 01. 快速回顾Embedding向量的概念 Embedding 向量或者向量表示&#xff0c;是指在高维向量空间中以数值描述表示对象、概念或实体&am…

使用Python和wxPython创建动态HTML日历生成器

在这个数字化时代,日历仍然是我们日常生活中不可或缺的工具。今天,我们将探讨如何使用Python创建一个动态HTML日历生成器。这个项目不仅实用,还能帮助我们深入理解Python编程、GUI开发和网页生成的相关知识。 项目概述 我们的目标是创建一个应用程序,允许用户选择特定的年份和…

第二证券:电影暑期档持续升温 农机自动驾驶驶入快车道

农机自动驾驶打开驶入快车道 得益于农机补贴、土地流通、高标准农田制造等方针引导&#xff0c;叠加技术突围和用户降本增效的内生需求&#xff0c;我国正处于农业2.0向农业3.0的过渡阶段。其间农机自动驾驶系统是结束农业3.0&#xff08;即自动化&#xff09;的要害并迎来快速…

【中项第三版】系统集成项目管理工程师 | 第 5 章 软件工程② | 5.4 - 5.8

前言 第 5 章对应的内容选择题和案例分析都会进行考查&#xff0c;这一章节属于技术的内容&#xff0c;学习要以教材为准。 目录 5.4 软件实现 5.4.1 软件配置管理 5.4.2 软件编码 5.4.3 软件测试 5.5 部署交付 5.5.1 软件部署 5.5.2 软件交付 5.5.3 持续交付 5.5.4…

我日常是如何使用LLM工具的:你的LLM工具没用起来,可能是因为方法不对。

引言 我对 Prompt 认知经历了 2 个阶段&#xff1a; 第一阶段&#xff1a;去年 3 月-11 月&#xff0c;我认为 Prompt 最终会灭亡。 第二阶段&#xff1a;去年 12 月至今&#xff0c;我有两个理解&#xff1a; 在主流 LLM 工具(比如 ChatGPT&#xff0c;文心一言等大模型厂商…

dom4j 操作 xml 之按照顺序插入标签

最近学了一下 dom4j 操作 xml 文件&#xff0c;特此记录一下。 public class Dom4jNullTagFiller {public static void main(String[] args) throws DocumentException {SAXReader reader new SAXReader();//加载 xml 文件Document document reader.read("C:\\Users\\24…

leetcode刷题总结——字符串匹配

KMP&#xff08;字符串匹配算法&#xff09; 主串或目标串&#xff1a;比较长的&#xff0c;我们就是在它里面寻找子串是否存在&#xff1b; 子串或模式串&#xff1a;比较短的。 前缀&#xff1a;字符串A和B&#xff0c;A BS&#xff0c;S非空&#xff0c;则B为A的前缀。 …

C++从入门到精通(第2版) 中文电子版

前言 C&#xff08;c plus plus&#xff09;是一种计算机高级程序设计语言&#xff0c;由C语言扩展升级而产生&#xff0c;最早于1979年由本贾尼斯特劳斯特卢普在AT&T贝尔工作室研发。C既可以进行C语言的过程化程序设计&#xff0c;又可以进行以抽象数据类型为特点的基于对…

四、GD32 MCU 常见外设介绍

系统架构 1.RCU 时钟介绍 众所周知&#xff0c;时钟是MCU能正常运行的基本条件&#xff0c;就好比心跳或脉搏&#xff0c;为所有的工作单元提供时间 基数。时钟控制单元提供了一系列频率的时钟功能&#xff0c;包括多个内部RC振荡器时钟(IRC)、一个外部 高速晶体振荡器时钟(H…

Python os模块的强大功能与应用详解

概要 在Python中,os模块提供了与操作系统进行交互的功能,允许我们执行各种操作系统任务,如文件和目录操作、环境变量访问、进程管理等。os模块是标准库的一部分,无需额外安装。本文将详细介绍os模块的功能,并提供相应的示例代码,帮助全面掌握这一强大工具。 os 模块概述…

支付宝低代码搭建电商小程序,无需编程,可视化操作

大家好&#xff0c;我是小悟 在数字化浪潮的推动下&#xff0c;为了更快速、高效地搭建电商小程序&#xff0c;支付宝低代码平台凭借其独特优势&#xff0c;为商家提供了便捷的解决方案。 支付宝低代码平台犹如一座精心打造的智慧工坊&#xff0c;让电商小程序的搭建变得轻而易…

【 香橙派 AIpro评测】烧系统运行部署LLMS大模型跑开源yolov5物体检测并体验Jupyter Lab AI 应用样例(新手入门)

文章目录 一、引言⭐1.1下载镜像烧系统⭐1.2开发板初始化系统配置远程登陆&#x1f496; 远程ssh&#x1f496;查看ubuntu桌面&#x1f496; 远程向日葵 二、部署LLMS大模型&yolov5物体检测⭐2.1 快速启动LLMS大模型&#x1f496;拉取代码&#x1f496;下载mode数据&#x…

【Outlook】从Outlook新版回归经典版全攻略

引言 在微软宣布计划于2024年底淘汰邮件应用&#xff08;Mail app&#xff09;之后&#xff0c;许多用户发现新版Outlook应用&#xff08;Outlook (new)&#xff09;在他们的Windows 11/10系统上自动启动。如果您更倾向于使用经典版Outlook&#xff08;Outlook (classic)&…

大气热力学(11)——热力学图的应用之二(焚风)

本篇文章源自我在 2021 年暑假自学大气物理相关知识时手写的笔记&#xff0c;现转化为电子版本以作存档。相较于手写笔记&#xff0c;电子版的部分内容有补充和修改。笔记内容大部分为公式的推导过程。 文章目录 11.1 焚风的概念11.2 焚风形成的原理11.3 焚风的示意图 11.1 焚风…