使用 R 的流程图

Flow chart using R

我一整天都在努力创建一个流程图来代表我们纳入研究的一些患者。

我已经在 Power Point 演示文稿中准备了一个粗略的版本,描述了我正在寻找什么样的安排。

现在,我已经准备好所有的盒子,但我需要的是与附图中相同的箭头对齐和连接。我曾尝试复制并粘贴此页面上的指南 https://cran.r-project.org/web/packages/Gmisc/vignettes/Grid-based_flowcharts.html,但现在我被卡住了。由于我对使用 R 的专业知识很少,只是偶尔使用它,因此您可能会看到一些我没有看到的简单内容。

这是我的代码:

  install.packages("Gmisc")
library(Gmisc, quietly = TRUE)
library(glue)
library(htmlTable)
library(grid)
library(magrittr)

org_cohort <- boxGrob(glue("Patients with pancreatic cancer admitted to the University Clinic of Essen",
                           "from 01/2002 to 08/2020",
                           "n = {pop}",
                           pop = txtInt(909),
                           .sep = "\n"))
included <- boxGrob(glue("Patients with genetic sequencing and survival data",
                         "n = {incl}",
                         incl = txtInt(412),
                         .sep = "\n"))
grp_a <- boxGrob(glue("Patients treated with FOLFIRINOX or Gemcitabine / nab-Paclitaxel",
                      "n = {recr}",
                      recr = txtInt(179),
                      .sep = "\n"))

grp_b <- boxGrob(glue("Patients with genetic sequencing data via MAPK-TRON panel",
                      "n = {recr}",
                      recr = txtInt(185),
                      .sep = "\n"))

excluded_1 <- boxGrob(glue("Excluded (n = {tot}):",
                         " - No survival data: {NoSurv}",
                         " - No MAPK-Tron panel and early available abdominal CT-scan: {NoMAPKCT}",
                         tot = 506,
                         NoSurv = 300,
                         NoMAPKCT = 206,
                         .sep = "\n"),
                    just = "left")

excluded_2 <- boxGrob(glue("Excluded (n = {NoFGP}):",
                           " - No FOLFIRINOX or Gemcitabine / nab-Paclitaxel as 1st line: {NoFGP}",
                           NoFGP = 233,
                           .sep = "\n"),
                      just = "left")

excluded_3 <- boxGrob(glue("Excluded (n = {NoMAPK}):",
                           " - No sequencing data available: {NoMAPK}",
                           NoMAPK = 227,
                           .sep = "\n"),
                      just = "left")

grid.newpage()
vert <- spreadVertical(org_cohort = org_cohort,
                       included = included,
                       grps = grp_a)
grps <- alignVertical(reference = vert$grp_a,
                      grp_a, grp_b) %>%
  spreadHorizontal()
vert$grps <- NULL

excluded_1 <- moveBox(excluded_1,
                    x = .8,
                    y = coords(vert$included)$top + distance(vert$included, vert$org_cohort, half = TRUE, center = FALSE))

excluded_2 <- moveBox(excluded_2,
                      x = .8,
                      y = coords(vert$grp_a)$top + distance(vert$included, vert$grp_a, half = TRUE, center = FALSE))

excluded_3 <- moveBox(excluded_3,
                      x = .8,
                      y = coords(vert$grp_b)$bottom + distance(vert$included, vert$grp_b, half = TRUE, center = FALSE))

## already facing problems here: R gives me the following error message: Error in coords(vert$grp_a/b) : 
  #Assertion on 'box' failed: Must inherit from class 'box', but has class 'NULL'..

for (i in 1:(length(vert) - 1)) {
  connectGrob(vert[[i]], vert[[i + 1]], type = "vert") %>%
    print
}
connectGrob(vert$included, grps[[1]], type = "N")
connectGrob(vert$included, grps[[2]], type = "N")

connectGrob(vert$included, excluded_1, type = "L")
connectGrob(vert$grp_a, excluded_2, type = "L")



# Print boxes
vert
grps
excluded_1
excluded_2
excluded_3

老实说:我并没有真正理解“grid.newpage()”之后的代码。因此,我一直在尝试一些事情来理解代码的组成部分实际上做了什么,但每次我认为我理解它是如何工作的时候,另一个挫折就在拐角处等着我。 R 基本上给我一个到处都是箭头和方框的图,但不是我希望它们看起来的样子。那么有时我的盒子甚至太大了还是怎么的?特别是图表末尾的两个子组的框经常被截断...

你知道如何“强制”R 为我复制那种图表(见图)吗?

非常感谢您的帮助,这会让我离最终完成我的论文更近一步...!

提前致谢!!

这里有一些内容可以让您开始使用 DiagrammeR

  • 使用splines = ortho得到90度角和直线。
  • 在节点标签中添加<br/>换行符。
  • 使用空白节点获取排除框的分支。然后使用 rank 让隐藏的空白节点与排除框对齐。

希望对您有所帮助。

library(DiagrammeR)

grViz(
     "digraph my_flowchart { 
      graph[splines = ortho]
      node [fontname = Helvetica, shape = box, width = 4, height = 1]
      
        node1[label = <Pancreatic cancer patients treated at our center<br/>diagnosed 2002-2020<br/>(n = 909)>]
        node2[label = <Patients with MAPK-TRON panel and/or<br/>early available abdominal CT scan (n = 412)>]
        
        blank1[label = '', width = 0.01, height = 0.01]
        excluded1[label = <Exclusion of patients without<br/>(1) MARK-TRON panel and early available abdominal CT scan, and<br/>(2) survival data<br/>(n = 506)>]
        
        node1 -> blank1[dir = none];
        blank1 -> excluded1[minlen = 2];
        blank1 -> node2;
        { rank = same; blank1 excluded1 }
        
        node3[label = <FOLFIRINOX or Gemcitabine/nab-Paclitaxel as first CTx<br/>(n = 179)>]
        node4[label = <Patients with MAPK-TRON panel regardless of CT scans<br/>(n = 185)>]
        
        blank2[label = '', width = 0.01, height = 0.01]
        excluded2[label = <Exclusion of patients that did not receive<br/>FOLFIRINOX or Gemcitabine/nab Paclitaxel as first CTx<br/>(n = 233)>]
        
        blank3[label = '', width = 0.01, height = 0.01]
        excluded3[label = <Exclusion of patients without MAPK-TRON panel<br/>(n = 227)>]
        
        node2 -> blank2[dir = none];
        node2 -> blank3[dir = none];
        
        blank2 -> excluded2[minlen = 2];
        blank3 -> excluded3[minlen = 2];
        
        blank2 -> node3;
        blank3 -> node4;
        
        { rank = same; blank2 excluded2 blank3 excluded3 }
     }"
)