Applying a weight matrix to an existing graph - r

Suppose I have a very simple undirected graph G with n=4 nodes:
G = graph_from_literal(1--2, 1:2--3, 1--4)
and a nxn weight matrix W such as:
1 2 3 4
1 0.0 0.5 0.9 1.3
2 0.5 0.0 1.0 0.0
3 0.9 1.0 0.0 0.0
4 1.3 0.0 0.0 0.0
Question: What is the fastest way of applying weights in W to the edges of G?
I could use the graph_from_adjacency_matrix function like the following:
G1 = graph_from_adjacency_matrix(ECV, mode="undirected", weighted=TRUE, diag=FALSE)
and then map the weightattribute of G1 to edges in G.
But it is a very expensive (and not elegant) solution when G is a very big graph.
How can this be done?

Something like the following should work:
library(igraph)
G = graph_from_literal(1--2, 1:2--3, 1--4)
# The weighted adjacency matrix of the new graph should be obtained simply
# by element-wise multiplication of W with the binary adjacency of G
G=graph_from_adjacency_matrix(as.matrix(W) *
as.matrix(get.adjacency(G, type="both")), mode="undirected", weighted=TRUE)
plot(G, edge.label=E(G)$weight)
[Edit]
As per the quick fixes discussed in the comments, if the weight matrix contains zeros and we don't want deletion of the corresponding edges, we can set the edge values to a small number:
W[ which(W == 0) ] = .Machine$double.xmin.
Now, in order to show the value of the weight in the graph plot correctly, before plotting the graph we can update the edge weights, without affecting the adjacency matrix, as follows:
E(graph)[ which(E(graph)$weight == .Machine$double.xmin) ]$weight = 0.0

Related

How to measure differences in spatial densities or organisation of individuals on a plane in R?

I have two distinct datasets which look like this:
identity x-pos y-pos
1: Z 0.5 0.7
2: B 0.1 0.0
3: C 4.6 2.5
4: D 5.6 5.0
5: A 0.2 1.0
6: P 0.4 2.0
Here, the each object with a unique identity is positioned on a 2d plane and the coordinates are denoted by x-pos y-pos (in micrometers)
What I want to be able to do is to measure if objects have differences in spatial positioning/ organisation in the two datasets? This could be differences in clustering, for instance more clusters in one dataset than the other.
or it could be if the radius of each cluster in a dataset is higher than the other.
or there are more objects within a defined radius in one dataset than the other?
Is there a simple way/ r package to execute this in?
Thanks!

constructing complete subgraph in igraph

I'm pretty new to R. I wanna know that given a list of node ID's of a graph is there any fast way to connect all the edges between that vertices and create a clique in that graph?
p.s: I'm looking for a really fast method because I'm working on a very large graph.
Thanks in advance.
Given a vector of vertices idx, we may use combn to create a vector of edges to be added:
g1 <- erdos.renyi.game(20, 1 / 20)
idx <- 3:8
g2 <- g1 + edges(c(combn(idx, 2)))
maximal.cliques(g2)
# ...
# [[14]]
# + 6/20 vertices, from 137d7ad:
# [1] 6 3 8 7 5 4

How to plot three coordinates in function of a fourth (3 positional coordinates in function of time) in R?

I would like to plot a 4 dimensional graph in R. I have three coordinates of position and a fourth variable (time). In this plot I would like to show these 3 coordinates in function of time. I only have one observation for each coordinate in each time. Somebody know if this is possible in R? I only found solutions for 3D plots.
For example:
coord_1<-c(0.5,0.3,0.9)
coord_2<-c(0.2,0.1,0.6)
coord_3<-c(0.7,0.4,0.8)
time_seg<-c(0.1,0.5,1)
data_plot<-data.frame(coord_1,coord_2,coord_3, time_seg)
data_plot
coord_1 coord_2 coord_3 time_seg
1 0.5 0.2 0.7 0.1
2 0.3 0.1 0.4 0.5
3 0.9 0.6 0.8 1.0

R plot matrixes and connect points of each line of second matrix

I have 2 matrices in R. One is called
j= matrix(c(1:8,1:8), nrow=2,ncol=8)
and the second:
B= matrix (c(Dav_Bou_k_med$r,Dav_Bou$r),nrow=2,ncol=8)
both Dav_Bou_k_med$r and Dav_Bou$r are matrices of nrow=1 and and ncol=8 so they are like this:
[1] 1.668 2.000 1.5 1.7 1.7 1.9 1.9 2.5
etc.
I used this plot:
plot(j,B)
but what I get is the relevant points for every 1:8 of the first matrix (j) (2 points for every 1:8, because I have two rows in B). What I want is to connect these points for every row in the B matrix in the plot. So, each of these points in the B matrix will be connected for each row (of B) and ideally with different colors. Is there any easy way to achieve that?
It's a little difficult to interpret exactly what you are looking for, but I imagine it's something like this?
j= matrix(c(1:8,1:8), nrow=2,ncol=8, byrow=TRUE)
fake_data <- sample(seq(1,3,0.2), 8, replace=TRUE)
more_fake_data <- sample(seq(1,3,0.2), 8, replace=TRUE)
B= matrix (c(fake_data, more_fake_data),nrow=2,ncol=8, byrow=TRUE)
plot(j, B)
lines(j[1,],B[1,])
lines(j[2,],B[2,], col="green")

Clustering by pair of 3 points for a given x position?

If I have a set of points that have different y positions (A,B,C) each with the same x coordinate. Is it possible to cluster this set of 3 points together and not individually?
I'd like to see the occurrence of this set of 3 points together in a given sample and see what set (A,B,C) is most frequent.
I've seen most of the clustering algorithm can cluster points for a given position (x,y) but not a set of several points for a given x coordinate.
For instance, if i have the following
X A B C
1 0.7 0.1 0.2
2 0.3 0.4 0.1
3 0.4 0.5 0.1
4 0.7 0.1 0.2
5 0.7 0.1 0.2
6 0.2 0.1 0.5
The positions x :1, 4 and 5 should be clustered together because they have the same set (A,B,C) = (0.7,0.1,0.2).
Is there any algorithm or tool (R) that is already doing that, clustering by pair of several points, finding the most occurrent pair with a graphical visualization?
Any help would be much appreciated.
If you're looking to tabulate the instances, then something along the lines of:
tab <- table(sprintf("%s:%s:%s", df1$A, df1$B, df1$C))
which.max(tab)
sort(tab, decreasing=TRUE)
will give you the most frequent combination (you can use strsplit to separate out the individual components if you need to go on and use them programmatically.
If you're looking to cluster, in the sense of find similar distances, then you can just use
dis <- dist(as.matrix(df1[[c("A","B","C")]])
clust <- hclust(dis)
and dis will tell you all the pairwise distances (find the zero's to get the identicals), and clust will give you a tree based on similarity across A:c
If this isn't answering the question, you probably need to clarify. You say things like same x coordinate in the text, but none of your rows have the same X value. And it's fairly unconventional to switch interchangeably between y coordinate / position / (A,B,C) .
It's hard to suggest a visualisation without knowing what feature you want to emphasize. Possibly a multi-dimensional scaling graph, where each node represents all x with the same (A,B,C) triplet, and then neighbours are other X's with closest (A', B', C') values?

Resources