gpt4 book ai didi

r - ggraph网络图: specify node coordinates

转载 作者:行者123 更新时间:2023-12-02 03:34:14 25 4
gpt4 key购买 nike

尝试使用ggraph包绘制网络并指定每个节点的坐标。虽然我可以使用 igraph 包来实现这一点,但我无法使用 ggraph 包来实现这一点。

# reproducible example to generate a random graph
library(igraph)
g1 <- erdos.renyi.game(20, 1/2)
plot(g1)

# function to produce coordinates for each node in order of the node
# degree (number of links per node)
coord <- function(g){
n.nod <- length(V(g))
mat.c <- matrix(0, nrow = n.nod, ncol = 2)
deg <- degree(g)
uniq.deg <- unique(deg)
min.d <- min(deg); max.d<- max(deg)
spa <- 10/(max.d - min.d) # how much to increment 0 to 10 space along y-axis

divi.y.1 <- seq(from = min.d, to=max.d, by = 1)
divi.y.2 <- seq(from = 0, to=10, by = spa) # both have same length

ind.x <- c(); ind.x[1] = 0
for(x in 2:n.nod){
ind.x[x] <- ind.x[x-1] + 0.1
if(ind.x[x] >= 10){
ind.x[x] = ind.x[x-1] - 0.1
}
}
d1 <- data.frame(divi.y.1, divi.y.2)

# plotting space of grid is (0,0), (0, 10), (10, 10), (10, 0)
for(i in 1:n.nod){
# y-axis in order
inD <- which(d1$divi.y.1 == deg[i])
mat.c[i, 2] <- d1[inD,2]
}
mat.c[, 1] <- ind.x
return(data.frame(mat.c))
}

这是绘制 igraph 对象的“老式”方法:

# plot igraph object - the old fashion way
x11()
plot(g1, layout = coord(g1), rescale = T, frame=T,
vertex.frame.color = "black", edge.color = "lightsteelblue",
edge.width = 1, vertex.label.cex = 1,
vertex.label.color = "black",
main = "Nodes in order of degree: top have more links, bottom fewer links")

ggraph 文档的链接是 here 。另请参阅 ggraph 的 GitHub 存储库以获取软件包安装说明(需要 >R.3.3 版本)。下面是有效的 ggraph 图(但我没有指定每个节点的坐标):

library(ggraph)
V(g1)$NaMe <- seq(1:20)

x11()
ggraph(g1, 'igraph', algorithm = 'kk') +
geom_edge_link(colour = "black", alpha = 0.8, show.legend = F) +
geom_node_label(aes(label = NaMe)) + ggtitle("ggraph plot: How to allocate coordinate for each node?") +
ggforce::theme_no_axes()

这是我尝试为每个节点制作具有指定坐标的ggraph图。遵循类似的示例和早期在 ggraph() 中的尝试(将节点的坐标传递到绘图上),我尝试了以下操作:

g <- make_ring(10) + make_full_graph(5)
coords <- layout_(g, as_star())
plot(g, layout = coords)
# try to replicate this example:
coords2 <- add_layout_(g1, coord(g1))

还尝试使用this function 。这很困难,因为文档中没有示例。

Lay <- layout_igraph_manual(g1, coord(g1))
Lay <- layout_igraph_igraph(g1, coord(g1))

x11()
ggraph(g1, 'igraph', algorithm = 'kk' ) + add_layout_(Lay) +
# layout_igraph_circlepack() +
geom_edge_link(colour = "black", alpha = 0.8, show.legend = F) +
geom_node_label(aes(label = NaMe)) + ggtitle("ggraph plot: Cannot allocate coordinate for each node") +
ggforce::theme_no_axes()

最佳答案

我不认为你直接调用layout_igraph_manual是作者的意图。如果将布局命名为“手动”,则可以传入手动布局顶点所需的参数。如果我理解正确的话,这让我明白了你所追求的:

coords <- coord(g1)
names(coords) <- c("x","y")
coords <- as.data.frame(coords)

ggraph(g1, layout = "manual", circular = FALSE, node.positions = coords)+
geom_edge_link(colour = "black", alpha = 0.8, show.legend = F) +
geom_node_label(aes(label = NaMe)) + ggtitle("ggraph plot: How to allocate coordinate for each node?") +
ggforce::theme_no_axes()

根据文档,重要的是您的手动布局是一个数据框并包含列 xy。我已经对您的 coord() 函数的输出执行了此操作。您可能只想更新它以返回该表单的数据。

关于r - ggraph网络图: specify node coordinates,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42048671/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com