r - 使用 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 }
}"
)
图表
推荐阅读
- javascript - 尝试在 iOS webview 应用程序中使用 SpeechSynthesis API
- reporting-services - SSRS – 如何对四舍五入的金额求和?
- javascript - 如何获取文本已知的特定锚点的链接
- angular - 如何在 Angular 中配置 MSAL?
- cmake - CMake 生成器不在那里吗?
- python - 使用 numpy 读取列数不等的数据文件
- javascript - 功能对话框 2 按钮无法正常工作
- python - 如何从字符串中提取带分隔符的数字?
- regex - 匹配部分路径的正则表达式
- php - 类中 HttpPost 的 PHP 等效项