根据组成员资格数据创建加权图 [英] Create Weighted Graph from Group Membership Data

查看:67
本文介绍了根据组成员资格数据创建加权图的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

假设我在R中有一个数据集,指示组内的个人.这是一个示例:

Suppose I have a dataset in R indicating the individuals within groups. Here is an example:

grp <- c(1, 1, 1, 2, 2, 2, 2, 3, 3, 3, 4, 4, 4, 5, 5)
ind <- c("A", "C", "D", "B", "C", "D", "E", "A", "D", "E", "B", "F", "E", "A", "F")
data.frame(grp, ind)

所以,数据看起来像这样:

So, the data look like this:

   grp ind
1    1   A
2    1   C
3    1   D
4    2   B
5    2   C
6    2   D
7    2   E
8    3   A
9    3   D
10   3   E
11   4   B
12   4   F
13   4   E
14   5   A
15   5   F

因此,组1由个人(A,C,D)组成,组2由个人(B,C,D,E)组成,依此类推.我想创建一个网络图,以显示个人如何相互联系.在一个组中,所有个体都通过边连接.边缘的厚度应反映两个人相互连接的频率.

So, group 1 is composed of individuals (A, C, D), group 2 is composed of individuals (B, C, D, E), and so on. I would like to create a network graph that shows how individuals are connected with each other. Within a group, all individuals are connected by edges. The thickness of the edges should reflect how often two individuals are connected to each other.

使用:

pairs <- do.call(rbind, sapply(split(ind, grp), function(x) t(combn(x,2))))

我可以获得具有所有成对边缘的矩阵,可以使用igraph包进行绘制:

I can obtain a matrix with all pairwise edges, which I can plot with the igraph package:

library(igraph)
plot(graph.edgelist(pairs, directed=FALSE), edge.curved=FALSE)

但是有没有一种方法可以使边缘的厚度与特定配对的发生频率成正比?

But is there a way of making the thickness of the edges proportional to how often a particular pairing occurred?

推荐答案

@hrbrmstr的解决方案构建第二个图以获取边缘权重.您也可以通过在pairs上进行操作来预先完成此操作:

@hrbrmstr's solution builds a second graph to get the edge weights. You could also do this beforehand by operating on pairs:

# Count unique edge pairs
library(plyr)
weighted <- ddply(data.frame(pairs), .(X1, X2), count)

# Plot
library(igraph)
g <- graph.edgelist(as.matrix(weighted[,1:2]), directed=FALSE)
plot(g, edge.curved=FALSE, edge.width=weighted$freq*3)

这篇关于根据组成员资格数据创建加权图的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆