Skip to Content

Trouble with plotting graphs in R [closed]

I have some graphs with latitude and longitude data for each vertex and attributes called "tobichi" and "sea" for each edge (essentially, whether to include the edge in plotting or not, presence of either indicating a no). I'm using igraph to visualize this and am writing my own functions to do so. Here's my code so far:

planar.embedding <- function(graph) {
    edge.incidence <- get.edgelist(graph)
    edges.x <- vector(length = (2 * ecount(graph)))
    edges.y <- vector(length = (2 * ecount(graph)))
    for (i in 1:ecount(graph)) {
        edges.x[2 * i - 1] <- get.vertex.attribute(graph, name="lon", index=edge.incidence[i, 1])
        edges.y[2 * i - 1] <- get.vertex.attribute(graph, name="lat", index=edge.incidence[i, 1])
        edges.x[2 * i] <- get.vertex.attribute(graph, name="lon", index=edge.incidence[i, 2])
        edges.y[2 * i] <- get.vertex.attribute(graph, name="lat", index=edge.incidence[i, 2])
    }
    edg <- get.edge.attribute(graph, name="tobichi", index=E(graph))
    c <- get.edge.attribute(graph, name="sea", index=E(graph))
    cedg <- c | edg
    zedg <- c(2 * which(cedg == TRUE), 2 * which(cedg == TRUE) - 1)
    edges.x <- edges.x[-zedg]
    edges.y <- edges.y[-zedg]
    return(cbind(edges.x, edges.y))
}
 
planar.plot <- function(graph) {
    plot(x=get.vertex.attribute(graph, name="lon", index=V(graph)), y=get.vertex.attribute(graph, name="lat", index=V(graph)))
    lines(x=planar.embedding(graph)[, "edges.x"], y=planar.embedding(graph)[, "edges.y"])
}

Because of how lines() works, edges.x and edges.y are twice as long as the number of edges in the graph, having one entry each for source and sink x and y coordinates. Here edge.incidence[i, 1] gets the source vertex, and edge.incidence[i, 2] gets the sink vertex (of edge i).

The problem is that this works on some graphs but not others. Interestingly enough, when I omit lines 10-15 in planar.embedding everything works fine. What could be going wrong here?