I know the degree of my global graph, but now I need to find the degrees of nodes within a subgraph. So, John has 4 friends in his school, but three friends in his class. How do I instruct igraph to count those three friends in his class, but not the rest in his school?
My global graph
library(igraph)
school <- read.table(text="
A B C D E F G
A 0 1 0 1 0 1 1
B 1 0 1 1 0 1 0
C 0 0 0 0 0 0 1
D 1 1 0 0 1 0 0
E 0 0 0 1 0 1 1
F 0 1 0 0 1 0 1
G 1 0 1 0 1 1 0", header=TRUE)
mat <- as.matrix(school)
g <- graph.adjacency(mat, mode="undirected", add.rownames = T)
My affiliation matrix for classes P, Q, and R
x <- read.table(text="
P Q R
A 1 1 0
B 0 0 1
C 0 0 0
D 1 0 1
E 1 1 0
F 0 1 0
G 1 1 1", header=TRUE)
inc <- as.matrix(x)
ginc <- graph.incidence(inc)
My subgraph for class P
class_nodes <- names(which(inc[,"P"] == 1))
class_adj <- mat[class_nodes, class_nodes]
class_graph <- graph.adjacency(class_adj, mode = "undirected")
I need to calculate the degree of nodes in subgraph "class_graph", but counting only their ties within the subgraph, not the global graph.
You can find all the nodes in class P with (we specifically extract the names so we can look them up in a different graph object).
V(ginc)[.nei("P")]$name
Then you can extract just that subset of connections from the main graph with
subg <- induced.subgraph(g, V(ginc)[.nei("P")]$name)
and you can calculate the degree of those nodes with
degree(subg)
# A D E G
# 2 2 2 2
Related
I'd like to plot two selected nodes and all their edges, not only the ones that connect these two nodes directly. For example:
library(igraph)
o <- read.table(text="
A B C D E F G
A 0 1 0 1 0 1 1
B 1 0 1 1 0 1 0
C 0 0 0 0 0 0 1
D 1 1 0 0 1 0 0
E 0 0 0 1 0 1 1
F 0 1 0 0 1 0 1
G 1 0 1 0 1 1 0", header=TRUE)
mat <- as.matrix(o)
g <- graph.adjacency(mat, mode="undirected", weighted = T, add.rownames = T)
I'm able to choose two nodes of g using the codes below, but the plot includes only the edges that connect them directly. I want them all.
g2 <- induced_subgraph(g, c("A","E"))
plot(g2)
How do I plot the two vertices, and all of their edges? Also, how do I choose path distance for each vertex?
Thanks!
library(igraph)
o <- read.table(text="
A B C D E F G
A 0 1 0 1 0 1 1
B 1 0 1 1 0 1 0
C 0 0 0 0 0 0 1
D 1 1 0 0 1 0 0
E 0 0 0 1 0 1 1
F 0 1 0 0 1 0 1
G 1 0 1 0 1 1 0", header=TRUE)
mat <- as.matrix(o)
g <- graph.adjacency(mat, mode="undirected", weighted = T, add.rownames = T)
plot(g)
# get 1st connections of A and E (their friends)
vertices_input = c("A","E") # specify vertices of interest
ids = as.numeric(E(g)[from(vertices_input)]) # get the ids of the edges from g that correspond to those vertices
g2 = subgraph.edges(g, ids) # keep only those edges from g as a sub-graph g2
plot(g2)
# get up to 2nd connections of A and E (friends of friends)
nms = V(g2)$name # get names of vertices of your sub-graph g2 (main vertices and 1st connections)
ids = as.numeric(E(g)[from(nms)]) # get the ids of the edges from g that correspond to main vertices and 1st connections
g3 = subgraph.edges(g, ids) # keep only those edges from g as a sub-graph g3
plot(g3)
In a plot, I need to colour two specific communities. Take the following data frame:
A B C D E F G
A 0 1 0 1 0 1 0
B 1 0 1 1 0 1 0
C 0 1 0 0 0 0 0
D 1 1 0 0 1 1 0
E 0 0 0 1 0 1 0
F 1 1 0 1 1 0 1
G 0 0 0 0 0 1 0
ob <- read.csv("...ties.csv",sep = ",", header = TRUE, row.names = 1)
m <- as.matrix(ob)
g <- graph.adjacency(m, mode="undirected", weighted = T, add.rownames = T)
First, I detect the communities (com) of my graph g using edge.betweenness:
com <- edge.betweenness.community(g)
V(g)$memb <- com$membership
This operation produces a number of communities, com[[1]],com[[2]], etc. I plot the resulting graph -- each community one colour -- with the following code:
plot(g, vertex.color=membership(com))
Now, how do I colour only two chosen communities, say com[[1]] and com[[2]], keeping the rest of the nodes homogeneous?
I had to tweak your adjacency matrix so that more than 1 community showed up.
library(igraph)
ob <- read.table(text="
A B C D E F G
A 0 1 0 1 0 1 0
B 1 0 1 1 0 1 0
C 0 0 0 0 0 0 0
D 1 1 0 0 1 0 0
E 0 0 0 1 0 1 1
F 0 1 0 0 1 0 1
G 0 0 0 0 1 1 0", header=TRUE)
m <- as.matrix(ob)
g <- graph.adjacency(m, mode="undirected", weighted = T, add.rownames = T)
com <- edge.betweenness.community(g)
V(g)$memb <- com$membership
cols <- membership(com)
cols[cols!=3] <- 1
plot(g, vertex.color=cols)
I detect communities in my adjacency matrix. Parallely, I create an affiliation matrix using the vertices of the same matrix. How do I measure the weight of the communities in each of the columns of the affiliation matrix?
Take the following adjacency matrix:
A B C D E F G
A 0 1 0 1 0 1 0
B 1 0 1 1 0 1 0
C 0 1 0 0 0 0 0
D 1 1 0 0 1 1 0
E 0 0 0 1 0 1 0
F 1 1 0 1 1 0 1
G 0 0 0 0 0 1 0
I identify the communities:
com <- edge.betweenness.community(g)
V(g)$memb <- com$membership
Now take the following affiliation matrix:
P R Q
A 1 1 0
B 1 0 1
C 1 1 0
D 0 1 0
E 1 0 1
F 0 0 1
G 1 1 0
How do I count the number of vertices corresponding to community [[1]] which are affiliated to the "P" in the affiliation matrix?
You can do sum(m[com[[1]],"P"]>0), given that m holds your affiliation matrix. Or lapply(com, function(x) colSums(m[x, ])) for all communities.
I need to create a subgraph from an adjacency matrix selecting by affiliation data. How do I match an adjacency and an affiliation matrix?
Take the following adjacency matrix:
A B C D E F G
A 0 1 0 1 0 1 0
B 1 0 1 1 0 1 0
C 0 1 0 0 0 0 0
D 1 1 0 0 1 1 0
E 0 0 0 1 0 1 0
F 1 1 0 1 1 0 1
G 0 0 0 0 0 1 0
And the following affiliation matrix:
P R Q
A 1 1 0
B 1 0 1
C 1 1 0
D 0 1 0
E 1 0 1
F 0 0 1
G 1 1 0
How do I create a subgraph from the adjacency matrix only with the nodes corresponding to P in the affiliation matrix?
If your goal is to:
filter out nodes from your adjacency matrix where the corresponding P is 1 in the affiliation matrix
convert filtered adjacency matrix to an igraph object
then you can accomplish that with the following:
# the names(which()) isn't needed for the subset of adj
p_nodes <- names(which(aff[,"P"] == 1))
p_adj <- adj[p_nodes, p_nodes]
p_graph <- igraph::graph.adjacency(p_graph)
I have asked a similar question before but this is slightly different compared to my previous question.
I have a matrix
a b c d e
a 0 1 1 1 0
b 1 0 1 1 1
I am trying to convert this to a square matrix like this
a b c d e
a 0 1 1 1 0
b 1 0 1 1 1
c 1 1 0 0 0
d 1 1 0 0 0
e 0 1 0 0 0
Any advise on how to do this in r will be helpful. Thanks in advance.
What do you think about this solution?
res <- (merge(m, t(m)[(nrow(m)+1):ncol(m),], all = TRUE, by = 0:2))[,-1]
rownames(res) <- colnames(res)
res[is.na(res)] <- 0