我正在尝试使用ggnet2函数在R中绘制树突状网络(河流网络),我希望图中的y轴有意义……特别是我希望它表示流域面积。我正在寻找一种方法,该方法将计算x坐标,以便网络显示得很好,没有交叉线。我该怎么办?请参见下面的代码和示例图像。在第一个图像中,将y坐标指定为等于面积,这是我想要的,但是x坐标未经过优化,因此该图看起来很丑。在第二张图片中,Fruchterman-Rhinegold放置看起来不错,但y坐标显然是任意的。我不愿意使用ggnet2,但是我确实希望网络链接具有角度(例如,不像节点之间具有垂直链接的聚类树状图)。谢谢!
[![library(GGally)
library(network)
library(sna)
graphmatrix <- matrix(c(0,1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,1,0,0,0,0,1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,1,0,0,0,0,1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,1,0,0,0,0,0,0,0,0,0,0,1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0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,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,1,0,0,0,0,0,0,0,1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0), nrow=33)
sitenumbers <- c(26,27,30,3,31,6,4,18,32,5,11,21,29,16,8,7,14,25,13,19,23,9,15,12,17,20,28,10,1,2,24,22,33)
areas <- c(4.2,4.2,4.5,2.2,4.8,2.5,2.4,3.6,5.3,2.5,3.1,3.7,4.4,3.4,2.8,2.7,3.3,4.2,3.3,3.6,3.9,3.1,3.4,3.1,3.5,3.7,4.3,3.1,1,1.6,4.1,3.7,5.7)
wsnet <- network(graphmatrix, directed=TRUE)
layout <- gplot.layout.fruchtermanreingold(wsnet, NULL)
wsnet %v% 'sitenumber' = sitenumbers
wsnet %v% 'area' = areas
wsnet %v% 'randomnumber'= sample(sitenumbers)
ggnet2(wsnet, label='sitenumber')
#You can specify y coordinates, but then you need to also specify x coords, so there's tons of line crossing... I want to "optimize" the x coords.
ggnet2(wsnet, label='sitenumber', mode=c('randomnumber','area'))][1]][1]
答案 0 :(得分:0)
这将是问题的ggraph
解决方案。我们将从布置树状图开始,然后告诉ggraph将面积用作y位置。
library(tidygraph)
library(ggraph)
gr <- as_tbl_graph(wsnet)
lay <- create_layout(gr, "dendrogram")
lay$y <- lay$area
ggraph(lay) +
geom_edge_link() +
geom_node_point(size = 10, shape = 21, fill = "white") +
geom_node_text(aes(label = sitenumber))
现在显然这与相交的线条等并不完美,但这是一个很好的起点。您可以手动调整一些位置:
lay$x[lay$sitenumber %in% c(12, 10, 17, 20, 28)] <- lay$x[lay$sitenumber %in% c(12, 10, 17, 20, 28)] + 1
lay$x[lay$sitenumber %in% c(1, 2)] <- lay$x[lay$sitenumber %in% c(1, 2)] - 2
lay$x[lay$sitenumber == 27] <- lay$x[lay$sitenumber == 27] + 2
lay$x[lay$sitenumber == 26] <- lay$x[lay$sitenumber == 26] + 3
ggraph(lay) +
geom_edge_link() +
geom_node_point(size = 10, shape = 21, fill = "white") +
geom_node_text(aes(label = sitenumber))
根据口味调整口味。