Using Graph.Adjacency() in R

using graph.adjacency() in R

The problem seems to be due to the data-type of the matrix elements. graph.adjacency expects elements of type numeric. Not sure if its a bug.

After you do,

m <- as.matrix(dat)

set its mode to numeric by:

mode(m) <- "numeric"

And then do:

net <- graph.adjacency(m, mode = "undirected", weighted = TRUE, diag = FALSE)
> E(net)$weight
[1] 8 1 10 1 15 1 1 5 7 1

How to create a graph from an adjacency matrix by also specifying nodes coordinates in igraph?

[As @user20650 mentioned above], you can specify the coordinates of the vertices latter, using vertex_attr():

library('igraph')

adjm <- matrix(
c(0, rep(c(1, 0, 0, 0, 0), times = 3)), 4, , F, list(letters[1:4], letters[1:4])
)

g01 <- graph_from_adjacency_matrix(adjm, 'undirected') -> g02

vertex_attr(g02, name = 'x') <- c(0, 0, 1, 1)
vertex_attr(g02, name = 'y') <- c(0, 1, 1, 0)

par(mfrow = 1:2)

plot(g01)
plot(g02)

vertex_attr

Create adjacency matrix from a path given as a vector of nodes in base R

You can use the powerful but little-known trick of matrix-based indexing:

index_mat <- rbind(
c(1, 2),
c(2, 3),
c(3, 1)
)

mat <- matrix(FALSE, 3, 3)
mat[index_mat] <- TRUE
mat
[,1] [,2] [,3]
[1,] FALSE TRUE FALSE
[2,] FALSE FALSE TRUE
[3,] TRUE FALSE FALSE

So do this:

path_to_D <- function (path, p) {
indices <- cbind(path[-length(path)], path[-1])
D <- matrix(0, p, p)
D[indices] <- 1
D
}

D <- path_to_D(hypothesis_path, 25)
which(D == 1, arr.ind=TRUE)

row col
[1,] 6 1
[2,] 7 6
[3,] 17 7

igraph creating a weighted adjacency matrix

The weights are there, weight (e/n) means that there is an edge attribute called weight, and it is numeric. See ?print.igraph. But they are not plotted by default, you need to add them as edge.label.

plot(ig, edge.label=round(E(ig)$weight, 3))

graph plot screenshot

For plotting, make sure you read ?igraph.plotting.

R iGraph: How to get weighted adjacency matrix from a graph?

It appears there are actually quite a few ways to do this.
Perhaps obvious, a first way to do it is to look carefully at the documentation of as_adjacency_matrix() and using the attr option:

as_adjacency_matrix(g,attr = "weight",sparse = T)

6 x 6 sparse Matrix of class "dgCMatrix"
a b c d f g
a . 1 3 2 . .
b 1 . . . . .
c 3 . . . 2 .
d 2 . . . 6 .
f . . 2 6 . 4
g . . . . 4 .

But one can also type

get.adjacency(g,attr = "weight",sparse = T)

Or just

g[]

6 x 6 sparse Matrix of class "dgCMatrix"
a b c d f g
a . 1 3 2 . .
b 1 . . . . .
c 3 . . . 2 .
d 2 . . . 6 .
f . . 2 6 . 4
g . . . . 4 .

Even if I'm not sure of the scope of validity of this last option.

What is the most efficient method to plot large Adjacency Matrices (not Graphs)?

My first bet was to look at the image method from the Matrix package. An example could be something like:

#####
# simulate a random matrix
n <- 1000000L # number of nodes
set.seed(1)
rng_i <- sample.int(n, size = 100L * n, replace = TRUE)
rng_j <- sample.int(n, size = 100L * n, replace = TRUE)
i <- c(1:n, rng_i, rng_j)
j <- c(1:n, rng_j, rng_i)
x <- runif(n * 100L)
x <- c(rep(1, n), x, x)

keep <- j <= i & c(rep(TRUE, n), tail(i, -n) != tail(j, -n))
j <- j[keep]
i <- i[keep]
x <- x[keep]

# use the image method from Matrix
library(Matrix)
mat <- sparseMatrix(i = i, j = j, x = x, symmetric = TRUE)
image(mat)

This takes a while but turns out to be based on levelplot from lattice. The output is a big blank plot.

Thinking a bit about, if you have say a 102 x 102 mm picture (~ 4 x 4 inch) and a 1m x 1m matrix, then there is ~ .0001 x .0001 mm per entry of the matrix if we disregard the axis etc. Not knowing much about human perception or images, I am guessing that you would need a very large number of pixels per inch in order to plot these and I am not sure if this will be perceivable unless there are larger clusters of adjacent non-zero entries.

On the other hand, if you change n <- 10000L then you get:

Sample Image

fairly fast. The above also gives an indication as to how hard it will be perceive boxes that are 1/100 x 1/100 smaller. I am guessing one would have to look for the number of adjacent non-zero nodes in a larger neighborhood but I am not aware of a package that will do that.



Related Topics



Leave a reply



Submit