作者热门文章
- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我有一个多波的友谊网络数据集。我想绘制在波浪中持续存在的友谊,并使每个图的节点保持相同的坐标。我能够使用 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)))
这给了我这个情节:
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
是去除后的对应坐标。然后,你会得到
关于从 ggraph 图中的动态布局中删除孤立节点,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/66821279/
如果数据很大,我正在使用 orphan 属性在打印时添加分页符它在 chrome 和 IE 中工作但在 FireFox 中不支持. docprint.document.write('
如果我从“Default.aspx”等进行 AJAX PageMethod 或 WebMethod 调用,然后在初始 PageMethod 返回之前快速导航到另一个页面(例如“Settings.asp
我是一名优秀的程序员,十分优秀!