I have a data frame detailing edge weights among N nodes. Is there a package for working with this sort of data?
For example, I would like to plot the following information as a network:
p1 p2 counts
1 a b 100
2 a c 200
3 a d 100
4 b c 80
5 b d 90
6 b e 100
7 c d 100
8 c e 40
9 d e 60
One option is the network package, part of the statnet family of R packages for statistical social network analysis. It handles network data in a sparse way, which is nice for larger data sets.
Below, I do the following:
A = read.table(file="so.txt",header=T)
A
p1 p2 counts
1 a b 100
2 a c 200
3 a d 100
4 b c 80
5 b d 90
6 b e 100
7 c d 100
8 c e 40
9 d e 60
library(network)
net = network(A[,1:2])
# Get summary information about your network
net
Network attributes:
vertices = 5
directed = TRUE
hyper = FALSE
loops = FALSE
multiple = FALSE
bipartite = FALSE
total edges= 9
missing edges= 0
non-missing edges= 9
Vertex attribute names:
vertex.names
adjacency matrix:
a b c d e
a 0 1 1 1 0
b 0 0 1 1 1
c 0 0 0 1 1
d 0 0 0 0 1
e 0 0 0 0 0
set.edge.attribute(net,"weight",A[,3])
gplot(net)
## Another cool feature
s = as.sociomatrix(net,attrname="weight")
plot.sociomatrix(s)
Here's how to make a network plot of the data in igraph:
d <- data.frame(p1=c('a', 'a', 'a', 'b', 'b', 'b', 'c', 'c', 'd'),
p2=c('b', 'c', 'd', 'c', 'd', 'e', 'd', 'e', 'e'),
counts=c(100, 200, 100,80, 90,100, 100,40,60))
library(igraph)
g <- graph.data.frame(d, directed=TRUE)
print(g, e=TRUE, v=TRUE)
tkplot(g, vertex.label=V(g)$name)
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With