gpt4 book ai didi

从 ggraph 图中的动态布局中删除孤立节点

转载 作者:行者123 更新时间:2023-12-04 07:53:34 29 4
gpt4 key购买 nike

我有一个多波的友谊网络数据集。我想绘制在波浪中持续存在的友谊,并使每个图的节点保持相同的坐标。我能够使用 ggraph 和 graphlayouts 的动态布局来使节点在四波中保持在相同的位置,但我想随着时间的推移删除失去联系的节点。这是我到目前为止所做的一个例子:

library(dplyr)
library(magrittr)
library(ggplot2)
library(igraph)
library(tidygraph)
library(ggraph)
library(viridis)
library(gridExtra)

set.seed(1234)

gtest <- erdos.renyi.game(100, 0.03, type = "gnp", directed = TRUE,
loops = FALSE) %>%
set_vertex_attr("label", value = 1:100)

g_nodes <- get.data.frame(gtest, what = "vertices") %>%
mutate(sex = sample(0:1, n(), replace = TRUE),
sex = as.character(sex))

g_edges1 <- get.edgelist(gtest) %>% as.data.frame()
g_edges2 <- sample_n(g_edges1, 130, replace = FALSE)
g_edges3 <- sample_n(g_edges2, 70, replace = FALSE)
g_edges4 <- sample_n(g_edges3, 20, replace = FALSE)


g1 <- graph_from_data_frame(d=g_edges1, vertices = g_nodes, directed = TRUE)
g2 <- graph_from_data_frame(d=g_edges2, vertices = g_nodes, directed = TRUE)
g3 <- graph_from_data_frame(d=g_edges3, vertices = g_nodes, directed = TRUE)
g4 <- graph_from_data_frame(d=g_edges4, vertices = g_nodes, directed = TRUE)

gList <- list(g1, g2, g3, g4)

xy <- graphlayouts::layout_as_dynamic(gList,alpha = 0.2)
pList <- vector("list",length(gList))

for(i in 1:length(gList)){
pList[[i]] <- ggraph(gList[[i]],layout="manual",x=xy[[i]][,1],y=xy[[i]][,2])+
geom_edge_link(color = "black", alpha = 0.7,
arrow = arrow(type = "closed",
angle = 25,
length = unit(1.5, 'mm')),
end_cap = circle(1, 'mm'),
width = 0.5, show.legend = FALSE) +
geom_node_point(aes(color = factor(sex)), size = 3) +
scale_color_hue(l=40) +
theme_graph()+
theme(legend.position = "none")
}
Reduce("+",pList)+
plot_annotation(title="Friendship network",theme = theme(title = element_text(family="Arial Narrow",face = "bold",size=16)))
这给了我这个情节:
enter image description here
这就是我想要的,除了图 2-4 我想删除没有边的节点。我还尝试从单个图形函数中删除隔离,例如:
g1 <- graph_from_data_frame(d=g_edges1, vertices = g_nodes, directed = TRUE) 

g2 <- graph_from_data_frame(d=g_edges2, vertices = g_nodes, directed = TRUE) %>%
delete.vertices(., which(degree(.)==0))

g3 <- graph_from_data_frame(d=g_edges3, vertices = g_nodes, directed = TRUE) %>%
delete.vertices(., which(degree(.)==0))

g4 <- graph_from_data_frame(d=g_edges4, vertices = g_nodes, directed = TRUE) %>%
delete.vertices(., which(degree(.)==0))
但是当我尝试运行绘图时出现此错误:
Error in data.frame(..., check.names = FALSE) : 
arguments imply differing number of rows: 100, 98
关于如何删除情节本身内的隔离的任何想法?

最佳答案

我认为您可以在 for 中执行以下操作环形

for (i in 1:length(gList)) {
idx <- degree(gList[[i]]) == 0
g <- gList[[i]] %>%
delete.vertices(names(V(.))[idx])
XY <- xy[[i]] %>%
subset(!idx)
pList[[i]] <- ggraph(g, layout = "manual", x = XY[, 1], y = XY[, 2]) +
geom_edge_link(
color = "black", alpha = 0.7,
arrow = arrow(
type = "closed",
angle = 25,
length = unit(1.5, "mm")
),
end_cap = circle(1, "mm"),
width = 0.5, show.legend = FALSE
) +
geom_node_point(aes(color = factor(sex)), size = 3) +
scale_color_hue(l = 40) +
theme_graph() +
theme(legend.position = "none")
}
哪里 g是去除孤立顶点后的图, XY是去除后的对应坐标。然后,你会得到
enter image description here

关于从 ggraph 图中的动态布局中删除孤立节点,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/66821279/

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