从 ggraph 图中的动态布局中删除孤立的节点
Remove isolated nodes from dynamic layout in ggraph plot
我有几个 wave 的友谊网络数据集。我想绘制海浪中持续存在的友谊,并使每个图的节点保持在相同的坐标中。我能够使用 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)))
这给了我这个情节:
这就是我想要的,除了图 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
是去除后对应的坐标。然后,你会得到
我有几个 wave 的友谊网络数据集。我想绘制海浪中持续存在的友谊,并使每个图的节点保持在相同的坐标中。我能够使用 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)))
这给了我这个情节:
这就是我想要的,除了图 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
是去除后对应的坐标。然后,你会得到