Create a graph from data frame with a layout base on attribute

256 views Asked by At

I create a graph from a data frame. And I would like the vertices to be arranged and moved apart according to the hamming value that is contained in data.rw$Hamming.

I would like some help

data.rw <- structure(list(g1 = c(1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 2, 
2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 4, 
4, 4, 4, 4, 4, 4, 4, 4, 5, 5, 5, 5, 5, 5, 5, 5, 6, 6, 6, 6, 6, 
6, 6, 7, 7, 7, 7, 7, 7, 8, 8, 8, 8, 8, 9, 9, 9, 9, 10, 10, 10, 
11, 11, 12), g2 = c(2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 3, 
4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 4, 5, 6, 7, 8, 9, 10, 11, 12, 
13, 5, 6, 7, 8, 9, 10, 11, 12, 13, 6, 7, 8, 9, 10, 11, 12, 13, 
7, 8, 9, 10, 11, 12, 13, 8, 9, 10, 11, 12, 13, 9, 10, 11, 12, 
13, 10, 11, 12, 13, 11, 12, 13, 12, 13, 13), Hamming = c(116, 
86, 101, 92, 84, 78, 83, 102, 87, 100, 96, 97, 90, 111, 98, 90, 
92, 87, 114, 95, 108, 104, 109, 85, 74, 68, 60, 67, 84, 71, 84, 
78, 79, 83, 85, 79, 78, 101, 90, 101, 91, 92, 72, 66, 67, 92, 
77, 90, 82, 83, 62, 59, 88, 71, 86, 78, 81, 59, 78, 63, 74, 68, 
73, 83, 60, 77, 75, 72, 89, 100, 94, 97, 79, 75, 82, 90, 93, 
91)), row.names = c(NA, -78L), class = "data.frame")

set.seed(1234)
vertice.df <- unique(c(data.rw$name1,data.rw$name2))
g <- graph_from_data_frame(d = data.rw, vertices = vertice.df, directed = F)

plot(g)

the graph

1

There are 1 answers

1
Ben Nutzer On BEST ANSWER

I recommend a distance-based layout for this task, multidimensional scaling comes to mind:

m <- get.adjacency(g, attr = "Hamming", sparse = F) 
# optionally: m <- dist(m)
l <- layout_with_mds(g, dist = m, dim = 2) 

First extract the weighted adjacency matrix from the graph and feed it into the layout function (dist = m). This returns a 2-dimensional matrix l (dim = 2) that you can use as layout for the position of the nodes.

plot(g, layout = l)

Have a look at ?cmdscale if you are interested in MDS and specifically the eig parameter to later assess the goodness-of-fit. Chances are that two dimensions are not enough to adequately reflect the distances between nodes. But that's for you to decide.