#' A graphing function for invariants.
drawGraphWithInvariants <- function(graph, g.layout){
#Did this as a function so I could apply to multiple graphs
#Takes in a graph as described above, with a type attribute labeled "place" or "transition"
places <- V(graph)[V(graph)$type == "place"]$name
transitions <- V(graph)[V(graph)$type == "transition"]$name
## Get adjacency
adjMat <- as.matrix(get.adjacency(graph))
## Incidence matrix is derived by taking reactant - reaction relationship matrix
## and product - reaction relation matrix and subtracting former from latter.
pre <- adjMat[places, transitions]
post <- t(adjMat[transitions, places])
S <- post - pre
# Invariant calculation matrix
pInvariants <- getMinimalInvariants(S)
tInvariants <- getMinimalInvariants(t(S))
# Name them
names(pInvariants) <- unlist(lapply(pInvariants, function(p) paste(p, collapse = "--")))
names(tInvariants) <- unlist(lapply(tInvariants, function(t) paste(t, collapse = "--")))
# Label nodes in the graph
require(reshape, quietly=TRUE)
invariantMap <- rbind(melt(pInvariants), melt(tInvariants))
names(invariantMap) <- c("name", "invariant")
rownames(invariantMap) <- as.character(invariantMap$name)
V(graph)$invariant <- invariantMap[V(graph)$name, "invariant"]
invariantVertex <- lapply(c(pInvariants, tInvariants), function(inv){
V(graph)[V(graph)$name %in% inv]
})
V(graph)$color <- "lightblue"
V(graph)[is.na(V(graph)$invariant)]$color <- "pink"
V(graph)[V(graph)$name == "E1"]$color <- "blue"
V(graph)[V(graph)$name == "PP_K"]$color <- "red"
plot.igraph(graph, layout = g.layout, vertex.size =3, vertex.label.dist=-.37,
edge.arrow.size=.4, mark.groups=invariantVertex)
}
Add the following code to your website.
For more information on customizing the embed code, read Embedding Snippets.