2
$\begingroup$

I've created a graph from an adjacency matrix using the igraph package but I want edges to appear in the graph only when the value in the adjacency matrix is beyond a particular threshold. Using the present method I am getting all the edges which have non zero weights.

set.seed(42)
iris_matrix <- as.matrix(iris[,1:4])
image(iris_matrix)
g<-cor(iris_matrix)
library(igraph)
R<-graph.adjacency(g, mode=c("undirected"), weighted=TRUE, diag=FALSE, add.colnames=NULL, add.rownames=NULL)
plot(R)
$\endgroup$
1
$\begingroup$

Line 7 is what you need:

set.seed(42)
iris_matrix <- as.matrix(iris[,1:4])
image(iris_matrix)
g<-cor(iris_matrix)
library(igraph)
R<-graph.adjacency(g, mode=c("undirected"), weighted=TRUE, diag=FALSE, add.colnames=NULL, add.rownames=NULL)
R[R > thresh] <- 0 
plot(R)
$\endgroup$
4
  • $\begingroup$ Used your method @kasra manshaei but got the following error : Error in inherits(v, "igraph.vs") : (list) object cannot be coerced to type 'double' $\endgroup$
    – amankedia
    Jan 6 '16 at 13:29
  • $\begingroup$ Check your R matrix after line 6. Whatever it is should be also after line 7 (double, int, ...). then converting it to this type will solve. Unfortunately I do not have access to R otherwise I would have sent you the code. $\endgroup$ Jan 6 '16 at 14:58
  • $\begingroup$ It's a 4*4 matrix @Kasra Manshaei, where do i find the 6th line :P $\endgroup$
    – amankedia
    Jan 6 '16 at 15:05
  • $\begingroup$ :)))) I mean 6th line in the code above dude ... not inside your matrix $\endgroup$ Jan 6 '16 at 17:59

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.