diagram: Functions for visualising simple graphs (networks), plotting...

Description Details Author(s) See Also Examples

Description

Visualises simple graphs (networks) based on a transition matrix, utilities to plot flow diagrams, visualising webs,...

Support for the book "A practical guide to ecological modelling - using R as a simulation platform" by Karline Soetaert and Peter M.J. Herman (2009). Springer.

and for the book "Solving Differential Equations in R" by Karline Soetaert, Jeff R. Cash and Francesca Mazzia (in press). Springer.

Details

Package: diagram
Type: Package
Version: 1.6
Date: 2011-06-01
License: GNU Public License 2 or above

This package is used in R-package ecolMod, which includes many more examples.

Author(s)

Karline Soetaert (Maintainer)

See Also

plotmat, plotweb, coordinates, openplotmat,

arrows:

bentarrow, curvedarrow, segmentarrow, selfarrow, splitarrow, straightarrow, treearrow,

boxes and text:

shadowbox, textdiamond, textellipse, textempty, texthexa, textdiamond, textplain, textrect, textround.

electrical networks:

en.Resistor,en.Capacitator,en.Node, en.Amplifier,en.Signal en.Ground.

Examples

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
## Not run: 
## show examples (see respective help pages for details)
example(plotmat)
example(plotweb)

## run demos
demo("flowchart") # creating flow charts
demo("plotmat")   # plotting diagrams inputted as a matrix
demo("plotweb")   # plotting webs inputted as a matrix

## open the directory with source code of demos
browseURL(paste(system.file(package="diagram"), "/demo", sep=""))

## show package vignette
vignette("diagram")
edit(vignette("diagram"))
browseURL(paste(system.file(package="diagram"), "/doc", sep=""))

## End(Not run)

Example output

Loading required package: shape

plotmt> M <- matrix(nrow = 4, ncol = 4, byrow = TRUE, data = 0)

plotmt> pp <- plotmat(M, pos = c(1, 2, 1), name = c("A", "B", "C", "D"),
plotmt+               lwd = 1, box.lwd = 2, cex.txt = 0.8, 
plotmt+               box.size = 0.1, box.type = "square", box.prop = 0.5,
plotmt+               main = "plotmat")

plotmt> pp
$arr
[1] row col
<0 rows> (or 0-length row.names)

$comp
        x         y
[1,] 0.50 0.8333333
[2,] 0.25 0.5000000
[3,] 0.75 0.5000000
[4,] 0.50 0.1666667

$radii
       x          y
[1,] 0.1 0.05621547
[2,] 0.1 0.05621547
[3,] 0.1 0.05621547
[4,] 0.1 0.05621547

$rect
     xleft      ybot xright      ytop
[1,]  0.40 0.7771179   0.60 0.8895488
[2,]  0.15 0.4437845   0.35 0.5562155
[3,]  0.65 0.4437845   0.85 0.5562155
[4,]  0.40 0.1104512   0.60 0.2228821


plotmt> # when explicitly given, pos should should be inbetween 0, 1
plotmt> pos <- cbind (c(0.2, 0.4, 0.6, 0.8), c(0.8, 0.6, 0.4, 0.2))

plotmt> pp <- plotmat(M, pos = pos, name = c("A", "B", "C", "D"),
plotmt+               lwd = 1, box.lwd = 2, cex.txt = 0.8, 
plotmt+               box.size = 0.1, box.type = "square", box.prop = 0.5,
plotmt+               main = "plotmat")

plotmt> # includes arrows between boxes
plotmt> pm <- par(mfrow = c(2, 2))

plotmt> M  <- matrix(nrow = 4, ncol = 4, byrow = TRUE, data = 0)

plotmt> M[2, 1] <- M[3, 1] <- M[4, 2] <- M[4, 3] <- "f1"

plotmt> col   <- M

plotmt> col[] <- "red"

plotmt> col[2, 1] <- col[3, 1] <- "blue"

plotmt> pp <- plotmat(M, pos = c(1, 2, 1), curve = 0, name = 1:4,
plotmt+               lwd = 1, box.lwd = 2, box.cex = 1:4, cex.txt = 0.8, 
plotmt+               arr.lcol = col, arr.col = col, box.type = "circle",
plotmt+               box.prop = 1.0, main = "plotmat")

plotmt> pp <- plotmat(M, pos = c(1, 2, 1), curve = 0, name = 1:4,
plotmt+               lwd = 1, box.lwd = 2, box.cex = 1:4, cex.txt = 0.8, 
plotmt+               arr.lcol = col, arr.col = col, box.type = "circle",
plotmt+               box.prop = 1.0, arr.len = 0.3,
plotmt+               segment.from = 0.35, segment.to = 0.65)

plotmt> M[1, 2] <- M[1, 3] <- M[2, 4] <- M[3, 4] <- "f2"

plotmt> pp <- plotmat(M, pos = c(1, 2, 1), curve = 0.1, name = 1:4,
plotmt+               lwd = 1, box.lwd = 2, box.cex = 1:4, cex.txt = 0.8, 
plotmt+               arr.lcol = col, arr.col = col, box.type = "none",
plotmt+               box.prop = 1.0, main = "plotmat", arr.len = 0.2,
plotmt+               segment.from = 0.3, segment.to = 0.7)

plotmt> pp <- plotmat(M, pos = c(1, 2, 1), curve = 0.1, name = 1:4, 
plotmt+               box.lwd = 2, box.cex = 1:4, cex.txt = 0.8, 
plotmt+               arr.lcol = col, arr.col = col, arr.pos = 0.7,
plotmt+               arr.type = "simple", lwd = 2, box.type = "none",
plotmt+               box.prop = 1.0, main = "plotmat", arr.len = 0.2,
plotmt+               segment.from = 0.3, segment.to = 0.7)

plotmt> par(mfrow=pm)

plotmt> # self arrows
plotmt> diag(M) <- "self"

plotmt> pp <- plotmat(M, pos = c(2, 2), curve = 0, name = LETTERS[1:4],
plotmt+               lwd = 1, box.lwd = 2, cex.txt = 0.8, self.cex = 0.5,
plotmt+               self.shiftx = c(-0.1, 0.1, -0.1, 0.1),
plotmt+               box.type = "diamond", box.prop = 0.5, main = "plotmat")

plotmt> M <- matrix(nrow = 4, ncol = 4, data = 0)

plotmt> M[2, 1]<- 1 ; M[4, 2] <- 2 ; M[3, 4] <- 3; M[1, 3] <- 4

plotmt> pp <- plotmat(M, pos = c(1, 2, 1), curve = 0.2, name = letters[1:4],
plotmt+               lwd = 1, box.lwd = 2, cex.txt = 0.8, arr.type = "triangle",
plotmt+               box.size = 0.1, box.type = "hexa", box.prop = 0.5,
plotmt+               main = "plotmat")

plotmt> arrlwd <- M*2

plotmt> arr.length <- M*0.4

plotmt> cextxt <- M*0.8

plotmt> plotmat(M, pos = c(1, 2, 1), curve = 0.2, name = letters[1:4], lwd = 1,
plotmt+         box.lwd = 2, arr.type = "triangle", box.size = 0.1,
plotmt+         box.type = "hexa", box.prop = 0.5, main = "plotmat",
plotmt+         arr.lwd = arrlwd, arr.length = arr.length, cex.txt = cextxt)

plotmt> M <- matrix(nrow = 4, ncol = 4, byrow = TRUE, data = 0)

plotmt> M     <- as.data.frame(M)

plotmt> M[[2,1]]<- "k[si]"

plotmt> M[[3,1]]<- "k[N]"

plotmt> M[[4,2]]<- "sqrt(frac(2,3))"

plotmt> names <-
plotmt+   c(expression(lambda[12]), "?", 
plotmt+     expression(lambda[13]),expression(lambda[23]))

plotmt> plotmat(M, pos = c(1, 2, 1), name = names, lwd = 1, box.lwd = 2, 
plotmt+         curve = 0, cex.txt = 0.8, box.size = 0.1, box.type = "square",
plotmt+         box.prop = 0.5, main = "plotmat")

plotmt> plotmat(M, name = letters[1:4], curve = 0, box.cex = 1:4, box.lwd = 4:1,
plotmt+         box.size = 0.075, arr.pos = 0.7, 
plotmt+         box.col = c("lightblue", "green", "yellow", "orange"))

plotwb> plotweb(Rigaweb, main = "Gulf of Riga food web",
plotwb+         sub = "mgC/m3/d", val = TRUE)

plotwb> ArrCol <- Rigaweb

plotwb> ArrCol[] <- "black"

plotwb> ArrCol[,"Sedimentation"] <- "green"

plotwb> plotweb(Rigaweb, main = "Gulf of Riga food web",
plotwb+         sub = "mgC/m3/d", val = FALSE, arr.col = ArrCol)

plotwb> plotweb(diag(20), main = "plotweb")


	demo(flowchart)
	---- ~~~~~~~~~

> ## Flowchart examples
> par(ask=TRUE)

> ## MODELLING DIAGRAM
> mar <- par(mar=c(1,1,1,1))

> openplotmat(main="from Soetaert and herman, book in prep",cex.main=1)

> elpos<-coordinates (c(1,1,1,1,1,1,1,1),mx=-0.1)

> segmentarrow(elpos[7,],elpos[2,],arr.pos=0.15,dd=0.3,arr.side=3,endhead=TRUE)

> segmentarrow(elpos[7,],elpos[3,],arr.pos=0.15,dd=0.3,arr.side=3,endhead=TRUE)

> segmentarrow(elpos[7,],elpos[4,],arr.pos=0.15,dd=0.3,arr.side=3,endhead=TRUE)

> pin   <- par ("pin")        # size of plotting region, inches

> xx  <- 0.2

> yy  <- xx*pin[1]/pin[2]*0.15  # used to make circles round

> sx    <- rep(xx,8)

> sx[7] <- 0.05

> sy    <- rep(yy,8)

> sy[6] <-yy*1.5

> sy[7] <- sx[7]*pin[1]/pin[2]

> for (i in c(1:7)) straightarrow (to=elpos[i+1,],from=elpos[i,],lwd=2,arr.pos=0.6,endhead=TRUE)

> lab <- c("Problem","Conceptual model","Mathematical model","Parameterisation",
+          "Mathematical solution","","OK?","Prediction, Analysis")

> for (i in c(1:5,8)) textround(elpos[i,],sx[i],sy[i],lab=lab[i])

> textround(elpos[6,],xx,yy*1.5,lab=c("Calibration,sensitivity","Verification,validation"))

>