Search code examples
rdiagram

How change the position of boxes within a row plotmat diagram in R (diagram package)?


I made the following template for a mediation diagram in the diagram package (plotmat function). Now all the boxes are aligned with each other in a rectangle, however, I would like to change the position of the "c" box to make the alignment of the boxes look like the (super quick) sketch I added. I read through the documentation of the package but couldn't really find an argument that does this. Does anyone know if this is possible and if yes how it's done?

library(diagram)
data <- c(0, 0, "'4'",0,
          "'1'",0,  0, 0, 
          0,0,0,0,
          "'2'","'3'", "'5'" ,0)
M<- matrix (nrow=4, ncol=4, byrow = TRUE, data=data)
plot<- plotmat (M, pos=c(2,2), 
                name= c("a", "b", "c", "d"), 
                box.type = "rect", box.size = 0.15, box.prop=0.5,  curve=0, shadow.size = 0)

a very quick doodle of the desired graph as it's hard to explain


Solution

  • This should get you there:

    library(diagram)
    
    data <- c(0, 0, 0, 0,
              4, 0, 0, 0, 
              0, 1, 0, 0,
              5, 2, 3, 0)
    
    M <- matrix (nrow=4, ncol=4, byrow = TRUE, data = data)
    
    pos <- cbind (c(0.2, 0.45, 0.8, 0.8), c(0.2, 0.6, 0.6, 0.2))
    
    plot <- plotmat(M, pos = pos, name = c("c", "a", "b", "d"),
                  lwd = 1, box.lwd = 2, cex.txt = 0.8,
                  box.size = 0.1, box.type = "rect", box.prop = 0.5, 
                  curve = 0, shadow.size = 0)
    

    Created on 2023-06-20 with reprex v2.0.2