如何在R中正确绘制模型流程图

时间:2015-07-17 15:01:02

标签: r diagram flow

我想在R或Rstudio中绘制一个隔离模型的流程图。在搜索互联网和本网站后,我能够部分实现以下目标:

http://apprentiss.com/model_diagram/untitled2.png

我的最终目标是实现这一目标: http://apprentiss.com/model_diagram/Untitled.png 所有箭头必须用字母命名。

这是我当前的图表代码:

感谢您的帮助

library(diagram)

M  <- matrix(nrow = 4, ncol = 4, byrow = TRUE, data = 0)
C <- M
A <- M

M[2, 1] <- paste(expression(beta[0]))
M[3, 2] <- paste(expression(alpha))
M[4, 2] <- paste(expression(a[t]))
M[3, 4] <- paste(expression(rho))
M[1, 3] <- paste(expression(phi1))

C[2, 1] <- 0.0
C[3, 2] <- 0.0
C[4, 2] <- 0.0
C[3, 4] <- 0.0
C[1, 3] <- -0.07
A[2, 1] <- A[3, 2] <- A[3, 4] <- A[1, 3]<-A[4, 2]<-2
A[4, 1] <- 2

col   <- M
col[] <- "black"
col[4, 2] <- "blue"
plotmat(M, pos = c(1,1,2), curve = C, name = c("S","C","R","I"),
        box.size=c(0.05,0.05,0.05,0.05), box.prop = 1,
        arr.lwd=A,my=0.0,mx= 0.0, dtext = c(0.6),arr.length= 0.4,shadow.size = 0,
        lwd = 1, box.lwd = 2, box.cex = 1, cex.txt = 1, 
        arr.lcol = col, arr.col = col, box.type = "circle",
        lend=4)


##======

1 个答案:

答案 0 :(得分:1)

关于自此问题以来已经过去的时间 被问到,我预计@Lunik已经解决了一些问题 其他方式。不过,因为我用这个问题作为练习 当我今天想要调查diagram - 包时,我可能会这样 好好发布我设法整理的解决方案。

此解决方案的要点是添加一堆额外的节点 能够指定附加箭头 “空”区。诀窍是“隐藏”这些额外的节点 它们没有显示在图形表示上,即它 基本上是关于为节点选择空名""的问题 我们 不想看,然后选择使它们“隐形”的颜色

为了使绘图尽可能与所需的输出相似,选择了总共13个节点的解决方案,其中只有4个节点可见。

.size <- 13
.visible <- c(2, 3, 5, 7)


##  Initiate the matrices:
.A  <- matrix(nrow = .size,
              ncol = .size)
.arr.lwd <- matrix(0, .size, .size)
.curve <-  matrix(0, .size, .size)
.col <- matrix("black", .size, .size)

##  Define the arguments:
.pos <- c(1, 1, 1, 5, 5)
.box.size <- rep(0.05, length = .size)
##  
.name <- rep(x = "", length = .size)
.name[.visible] <- c("S", "C", "R", "I")
##
.box.col <-  rep(x = "white", length = .size)
.box.lcol <- .box.col
.box.lcol[.visible] <- "black"
##  Arrows up from/down to top visible node:
.arr.lwd[2, 1] <- 2
.curve[2, 1] <- 0.05
.A[2, 1] <- ""
.arr.lwd[1, 2] <- 2
.curve[1, 2] <- 0.05
.A[1, 2] <- ""
##  Arrow down from top visible node:
.arr.lwd[3, 2] <- 2
.A[3, 2] <- "beta[0]"
##  Arrows down from the second visible node (from top):
.arr.lwd[5, 3] <- 2
.A[5, 3] <- "alpha"
.arr.lwd[7, 3] <- 2
.A[7, 3] <- "a[t]"
.col[7, 3] <- "blue"
##  Arrows from the leftmost visible node:
.arr.lwd[4, 5] <- 2
.A[4, 5] <- ""
.arr.lwd[2, 5] <- 2
.curve[2, 5] <- -0.07
.A[2, 5] <-  "phi1"
##  Arrows from the rightmost visible node:
.arr.lwd[5, 7] <- 2
.A[5, 7] <- "rho"
.arr.lwd[8, 7] <- 2
.A[8, 7] <- ""
.arr.lwd[12, 7] <- 2
.A[12, 7] <- ""

##  Adjustment of node `6` to remove "gap" from arrow:
.box.size[6] <- 0
.box.col[6] <- "black"
.box.lcol[6] <- "black"


##  An argument to allow fine-tuning of the arrowhead-positions
##  related to "empty" nodes:
.arr.pos <- matrix(0.5, .size, .size)
.empty_places_top <- rbind(
    c(1, 2),
    c(2, 1))
.empty_places_bottom <- rbind(
    c(4, 5),
    c(8, 7),
    c(12, 7))
.arr.pos[.empty_places_top] <- 0.58
.arr.pos[.empty_places_bottom] <- 0.66


##  Create the desired plot.
plotmat(A = .A,
        pos = .pos,
        curve = .curve,
        name = .name,
        box.size = .box.size, 
        box.col = .box.col,
        box.lcol = .box.lcol,
        box.prop = 1,
        arr.lwd = .arr.lwd,
        my = 0.0,
        mx = 0.0,
        dtext = c(0.6),
        arr.type = "triangle",
        arr.pos = .arr.pos,
        arr.length= 0.4,
        shadow.size = 0,
        lwd = 1,
        box.cex = 1,
        cex.txt = 1, 
        arr.lcol = .col,
        arr.col = .col,
        box.type = "circle",
        lend = 4)
相关问题