R markov chain package, is possible to set the coordinates and size of the nodes?

你。 提交于 2020-01-14 14:38:27

问题


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

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!