作者:Anisa Dhana 原文链接:https:///how-to-build-a-simple-flowchart-with-r-diagrammer-package/ 自从我学会了Markdown并且在RStudio使用R notebook生成高质量的PDF报告,我希望不再使用Microsoft Office。事实上,通过R Markdown,我可以完成所有事情,除非我需要制作流程图。流程图是一种代表工作流程的图。在医药研究中,流程图用来展现研究人群以及被排除的群体。 经过一些研究,我发现R有一些程序包允许制作示例流程图。我找到的一个相对容易使用的是DiagrammeR。该程序包的优点是在R Markdown句法中使用代码可以生成图。 下载程序包:
library(DiagrammeR)
让我们通过一个简单的例子开始,并且使用相应的代码。 library(DiagrammeR) grViz('digraph flowchart { # node definitions with substituted label text node [fontname = Helvetica, shape = rectangle] tab1 [label = '@@1'] tab2 [label = '@@2'] tab3 [label = '@@3'] tab4 [label = '@@4'] tab5 [label = '@@5']
# edge definitions with the node IDs tab1 -> tab2 -> tab3 -> tab4 -> tab5; }
[1]: 'Questionnaire sent to n=1000 participants' [2]: 'Participants responded to questionnaire n=850' [3]: 'Participants came to clinic for evaluation n=700' [4]: 'Participants eligible for the study n=600' [5]: 'Study sample n=600' ')
node 指向图的样式并包括字体和形状。该例子中,我选择了长方形,但我可以选择方形或菱形。接着,我在图上设置了我需要的节点(比如,tab1,tab2,…,tab5)。然后,我调整次序,最后,我在每个节点输入内容。呈现的图是直的。 另一个例子展现在下方:
grViz('digraph flowchart { # node definitions with substituted label text node [fontname = Helvetica, shape = rectangle] tab1 [label = '@@1'] tab2 [label = '@@2'] tab3 [label = '@@3'] tab4 [label = '@@4'] tab5 [label = '@@5']
# edge definitions with the node IDs tab1 -> tab2; tab2 -> tab3; tab2 -> tab4 -> tab5 }
[1]: 'Questionnaire sent to n=1000 participants' [2]: 'Participants came to clinic for evaluation n=700' [3]: 'Participants non-eligible for the study n=100' [4]: 'Participants eligible for the study n=600' [5]: 'Study sample n=600' ')
前一个例子的区别是node2后面紧跟着其它两个节点。看看代码tab2->tab3 以及tab2->tab4->tab5 。我还可以进行无限种结合,构建更加复杂多样的流程图。 我发现这个程序包的一个缺点是我不能建立PDF的notebook,但对于HTML还是挺好的。如果有人知道如何把它呈现在PDF上,分享在这里,那将是非常棒的。想要学习更多的DiagrammeR,可以点击这个链接。 ——————————————
|