问题
I'm working with R in some biology-behavioural problems, and I have a transition matrix which I want to plot in a certain way.
I'm using the markovchain
package, which makes easy the visualization.
This is a test-code and it's output.
> a<-array(0.25,dim = c(4,4))
> markov<-new("markovchain",transitionMatrix=a,states=c("a","b","c","d"), name="test")
> markov
test
A 4 - dimensional discrete Markov Chain defined by the following states:
a, b, c, d
The transition matrix (by rows) is defined as follows:
a b c d
a 0.25 0.25 0.25 0.25
b 0.25 0.25 0.25 0.25
c 0.25 0.25 0.25 0.25
d 0.25 0.25 0.25 0.25
> plot(markov)
The thing is, I would like to set the coordinates of the graph nodes to place them in a 2D grid or something like that, and also to set the size of the nodes.
I know that this package works with S4
, but I'm not really familiar with it and don't know if there are any arguments that would be useful to me.
Any help?
回答1:
You could do this:
layout <- matrix(c(0,0,0,1,1,1,1,0), ncol = 2, byrow = TRUE)
# [,1] [,2]
# [1,] 0 0
# [2,] 0 1
# [3,] 1 1
# [4,] 1 0
plot(markov, vertex.size = 25, layout = layout)
The layout
is a matrix of two columns. Each row contains coordinates per node. With vertex.size
, you can adjust the size of the nodes. Note that markovchain
package benefits from the igraph
package to do so.
With these layouts
layout <- matrix(c(4,-2,7,2,8,8,8,-4), ncol = 2, byrow = TRUE)
# [,1] [,2]
# [1,] 4 -2
# [2,] 7 2
# [3,] 8 8
# [4,] 8 -4
plot(markov, vertex.size = 25, layout = layout)
You would have this:
来源:https://stackoverflow.com/questions/43205378/r-markov-chain-package-is-possible-to-set-the-coordinates-and-size-of-the-nodes