微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

邻接矩阵的顶点坐标;应用于随机过渡矩阵表示的作物轮作

如何解决邻接矩阵的顶点坐标;应用于随机过渡矩阵表示的作物轮作

上下文:我正在尝试绘制从过渡矩阵绘制的作物旋转图,描述从一种作物到另一种作物的变化。对于那些想知道的人,已经发布了使用过渡矩阵来表示农作物轮作的方法,但是参考文献并不涉及绘制地块;看到:

Castellazzi,M.S.,Wood,G.A.,Burgess,P.J.,Morris,J.,Conrad, K.F.,佩里,J.N.,2008年。农作物的系统表示 旋转。农业系统97,26-33。 https://doi.org/10.1016/j.agsy.2007.10.006

通过随机矩阵,我的意思是一个表示从一步到Ci到Cj过渡概率的方阵。每行的总和等于一。

这归结为使用随机转移矩阵作为邻接矩阵来构建加权有向图。可以使用diagram::plotmat()igraph::graph_from_data_frame()完成此操作,但我只是无法找到如何对顶点进行排序并使边缘看起来“不错”。

预期剧情:

Expected output

我要使用的矩阵如下:

> transmat
   C1 C2  C3  C4 C5
C1  0  1 0.0 0.0  0
C2  0  0 0.5 0.5  0
C3  0  0 0.0 0.0  1
C4  0  0 0.0 0.0  1
C5  1  0 0.0 0.0  0

> dput(transmat)
structure(list(C1 = c(0,1),C2 = c(1,0),C3 = c(0,0.5,C4 = c(0,C5 = c(0,1,0)),class = "data.frame",row.names = c("C1","C2","C3","C4","C5"))

尝试使用diagram::plotmat()

## With diagram::plotmat ----------------------------------------------------

plot.new()
plotmat(t(transmat),pos = c(1,2,# non-0 count in each line of transmat
        curve = 0.3,absent = 0,# don't connect crops linked by 0
        arr.type = "triangle",arr.pos = 0.6,Box.type = "rect",Box.prop=0.3,Box.lwd=2,shadow.size = 0,cex.txt=0.8,endhead = FALSE)

输出

Output using diagram::plotmat

一个带有igraph::graph_from_data_frame()layout = layout_as_tree()的示例:

预期地块:

Expected plot complex ex

相关矩阵:

> complex_ex
     C1   C2  C3  C4   C5
C1 0.00 1.00 0.0 0.0 0.00
C2 0.00 0.00 0.5 0.5 0.00
C3 0.25 0.25 0.0 0.0 0.50
C4 0.00 0.00 0.0 0.0 1.00
C5 0.75 0.00 0.0 0.0 0.25

> dput(complex_ex)
structure(list(C1 = c(0,0.25,0.75),0.25)),"C5"))

我使用的代码

# Some data transformation to make it work with igraph::graph_from_data_frame()

df_transmat <- as.data.frame(complex_ex)
df_transmat$from <- rownames(df_transmat)
df_transmat <- reshape(df_transmat,idvar = "from",varying = colnames(df_transmat)[1:(ncol(df_transmat)-1)],times = colnames(df_transmat)[1:(ncol(df_transmat)-1)],timevar = "to",v.names = "change",direction = "long")
rownames(df_transmat) <- NULL
rotation <- subset(df_transmat,subset = df_transmat$change != 0)

# The plot 

g1 <- graph_from_data_frame(rotation,directed = TRUE)

plot(g1,layout = layout_as_tree(g1,root = "C1"),edge.arrow.mode = 2,edge.arrow.size = 0.5,edge.curved = 0,edge.width = 0.5,vertex.label.cex = 1,vertex.label.font = 2,vertex.shape = "rectangle",vertex.color = "white",vertex.size = 50,vertex.size2 = 30,vertex.label.dist = 0,vertex.label.color = "black")

输出

example 2 output

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。