0

I have a network created with igraph and then viewed with ggraph. The edges have different weights based on the data as do the nodes.

I want to place the network on a map according to the nodes gps coordinates, but I can't figure out how to transfer the igraph or ggraph onto a map/shapefile and how to retain the weights.

There are two datasets, one for edges and one for the vertices. Each vertex is a site. I want to keep the network weights and overlay it onto a map of the study site, but I can't figure out how to keep those weights in ggplot. I have seen that people make tables, but I tried and can't get the tables to retain the weight of the nodes and edges.

When I try to turn the network into an object, I get the following error:

Error in if (dim(x)[2] > 2) vals <- x[, -(1:2), drop = FALSE] else vals <- NULL : argument is of length zero

There have been a few posts asking a similar concept. From igraph to ggplot object how to plot a network on to a geographic map in R? Error converting igraph to network object in R

Appreciate any advice/direction.

library(igraph) library(ggraph) library(ggforce) library(ggmap) library(dplyr)

`

> edges.df
   to from sum_code
1   B    B       24
2   B    C        1
3   B    D        3
4   B    G        9
5   B    H        2
6   B    J        3
7   C    C        4
8   C    G        6
9   C    J        1
10  D    D       16
11  D    F       27
12  D    G        1
13  D    H        1
14  E    F        3
15  F    F       20
16  F    G        2
17  F    H        2
18  G    G      230
19  G    H       22
20  G    I        4
21  H    H       22
22  J    J      109

> vertices.df
   locations        x         y
1          A 46.20323 -9.390967
2          B 46.21088 -9.410898
3          C 46.22592 -9.403538
4          D 46.21906 -9.429763
5          E 46.22664 -9.419323
6          F 46.22702 -9.439434
7          G 46.23780 -9.438571
8          H 46.25050 -9.427436
9          I 46.23859 -9.449669
10         J 46.43900 -9.380025

`

`

g <- graph_from_data_frame(d = edges.df, vertices = vertices.df, directed = FALSE)
g
# get lat long coordinates for the layout
lo <- layout.norm(as.matrix(vertices.df[, c("x","y")]))

#plot
plot.igraph(g, layout=lo, rescale=T, vertex.label= NA)
plot(g)
str(g)

as_data_frame(g, what="edges")
as_data_frame(g, what="vertices")

net<- g

#compute node degrees (and links) and use it to set node size
deg <- degree(net, mode="all")
V(net)$size <- deg*4

# Set edge width based on weight:
E(net)$width <- E(net)$sum_code/6 # 6 is just a very arbitrary number chosen
#change arrow size and edge color:
E(net)$edge.color <- "gray80"


plot(net, vertex.color = "lightgrey") 


#here i lose the weights of the nodes and of the loops
ggraph(net, layout="stress") +
  geom_edge_link(color="lightgrey", aes(width=width)) +
  geom_edge_loop(color="darkgrey",)+ 
  scale_edge_width(aes(size=E(net)$width))+
  geom_node_point(color="light blue",aes(size=V(net)$size))+
  geom_node_text(aes(label = name))+
  theme_void()

#try to turn it into a list using as.network but get error
as.network(net1, directed=FALSE, loops=TRUE, matrix.type="edgelist")

`

I can't figure out how to go from Igraph or ggraph to plotting that same network (with the weights on the nodes and edges) onto a shapefile or map.

Cheryl
  • 1
  • 1

0 Answers0