基于R语言Gmisc包绘制研究患者入组流程图的技术求助
修正Gmisc流程图代码:解决布局错误、框体截断与箭头连接问题
我帮你梳理下代码里的核心问题,一步步调整后就能得到符合需求的患者纳入流程图啦~
首先你遇到的Error in coords(vert$grp_a/b) : Must inherit from class 'box', but has class 'NULL',是因为spreadVertical(org_cohort = org_cohort, included = included, grps = grp_a)这行用法不对——spreadVertical不需要grps这种命名参数,直接传递要垂直排列的box就行,所以原来的vert对象里根本没有grp_a元素,自然会报NULL错误。
下面是修正后的完整代码,同时解决了布局混乱、框体截断的问题:
# 安装并加载所需包(如果未安装) if (!require("Gmisc")) 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"), width = 0.4 ) included <- boxGrob( glue("Patients with genetic sequencing and survival data", "n = {incl}", incl = txtInt(412), .sep = "\n"), width = 0.4 ) grp_a <- boxGrob( glue("Patients treated with FOLFIRINOX or Gemcitabine / nab-Paclitaxel", "n = {recr}", recr = txtInt(179), .sep = "\n"), width = 0.4 ) grp_b <- boxGrob( glue("Patients with genetic sequencing data via MAPK-TRON panel", "n = {recr}", recr = txtInt(185), .sep = "\n"), width = 0.4 ) excluded_1 <- boxGrob( glue("Excluded (n = {tot}):", " - No survival data: {NoSurv}", " - No MAPK-Tron panel and early available abdominal CT-scan: {NoMAPKCT}", tot = txtInt(506), NoSurv = txtInt(300), NoMAPKCT = txtInt(206), .sep = "\n"), just = "left", width = 0.4 ) excluded_2 <- boxGrob( glue("Excluded (n = {NoFGP}):", " - No FOLFIRINOX or Gemcitabine / nab-Paclitaxel as 1st line: {NoFGP}", NoFGP = txtInt(233), .sep = "\n"), just = "left", width = 0.4 ) excluded_3 <- boxGrob( glue("Excluded (n = {NoMAPK}):", " - No sequencing data available: {NoMAPK}", NoMAPK = txtInt(227), .sep = "\n"), just = "left", width = 0.4 ) # 初始化绘图页面 grid.newpage() # 1. 垂直排列顶层两个框:原始队列 -> 纳入队列 vert_top <- spreadVertical(org_cohort, included) # 2. 水平排列两个子组,对齐到纳入队列的下方 grps <- spreadHorizontal(grp_a, grp_b) # 将子组移动到纳入队列的正下方,保持合适垂直间距 grps <- moveBox(grps, y = coords(vert_top$included)$bottom - 0.1) # 3. 放置排除框到对应主框的右侧,对齐中间位置 excluded_1 <- moveBox(excluded_1, x = coords(vert_top$included)$right + 0.1, y = coords(vert_top$included)$center_y) excluded_2 <- moveBox(excluded_2, x = coords(grps$grp_a)$right + 0.1, y = coords(grps$grp_a)$center_y) excluded_3 <- moveBox(excluded_3, x = coords(grps$grp_b)$right + 0.1, y = coords(grps$grp_b)$center_y) # 绘制箭头连接 connectGrob(vert_top$org_cohort, vert_top$included, type = "vert") %>% grid.draw() connectGrob(vert_top$included, grps$grp_a, type = "N") %>% grid.draw() connectGrob(vert_top$included, grps$grp_b, type = "N") %>% grid.draw() connectGrob(vert_top$included, excluded_1, type = "L") %>% grid.draw() connectGrob(grps$grp_a, excluded_2, type = "L") %>% grid.draw() connectGrob(grps$grp_b, excluded_3, type = "L") %>% grid.draw() # 绘制所有框体 vert_top %>% grid.draw() grps %>% grid.draw() excluded_1 %>% grid.draw() excluded_2 %>% grid.draw() excluded_3 %>% grid.draw()
关键修改点说明:
- 解决NULL错误:删除了错误的
grps = grp_a参数,改用spreadVertical直接传递要排列的box,再单独处理两个子组的水平布局,确保每个box都能被正确索引。 - 避免框体截断:给每个
boxGrob设置了width = 0.4,你可以根据文本长度调整这个值(比如0.35或0.45),确保所有内容都能完整显示。 - 清晰的布局逻辑:把布局分成顶层垂直排列、子组水平排列、排除框定位三个独立步骤,更容易理解和微调位置。
- 正确的箭头渲染:用
grid.draw()替代print,确保箭头能在grid绘图环境中正确显示。
内容的提问来源于stack exchange,提问作者Pharmacos
相关产品推荐
相关产品推荐

