3
votes

I am trying to create an igraph graph using the co-ordinates of nodes. I realized that there are no direct ways to do this through the igraph package for R.

There are ways to plot a graph on a 2D space using the layout() function, but I need to create a graph from known co-ordinates of nodes. I'll appreciate any help anyone can provide with this.

Why I'm trying to set up a graph this way? After parametrizing the graph nodes with co-ordinates, I want to connect the nodes using a probability measure that takes the distance between the nodes into account.

Thanks. Hitaysh

    # Initial Co-ordinates of nodes
    n = 1000 # no. of nodes
    nodes.coord <- data.frame(x=runif(n,min=0,max=n),
                              y=runif(n,min=0,max=n))

    # Set up a graph with nodes on above co-ordinates...

P.S. This is my first post on StackOverflow. Any constructive feedback on how to ask questions better, is also welcome.

1
I don't understand what you are trying to do. The graph structure is completely independent of the plotted xy coordinates. If you want to control where the nodes are rendered, you can use the layout= parameter in plot.igraph() (see ?igraph::layout). The xy coordinates tell you nothing about which nodes are connected. Please read how to create a reproducible example and include sample input and desired output. (If you're going to use a runif, also use set.seed() so we can get the same values).MrFlick

1 Answers

2
votes

After reading your question afew more times, i'm guessing that something like this will work

n = 20 # no. of nodes
set.seed(15)
nodes.coord <- data.frame(
    x=runif(n,min=0,max=n),
    y=runif(n,min=0,max=n)
)
gg <- graph.empty(n)
plot(gg, layout=as.matrix(nodes.coord[,c("x","y")]))

enter image description here

But if you are going to connect nodes based on distance, it probably makes sense to find the connections prior to creating the igraph object since you already know the locations.