user3001385
user3001385

Reputation: 33

How to plot directed acyclic lattice graph in R

I need to plot directed acyclic lattice graph of size m x n, similar as in this picture, but without edges on the contour and without vertexes on the corners:

enter image description here

Is this possible to do with graph.lattice function? If yes, how to set such vertexes' labels (i.e. (x,y) format, not just an integer number) and remove mentioned edges and vertexes? Moreover, is it possible to plot graph in such position (as in a picture) without using tkplot function and rotating it then?

Upvotes: 2

Views: 1097

Answers (1)

Gabor Csardi
Gabor Csardi

Reputation: 10825

I am not exactly sure what you mean by 'without edges on the contour', but here are some points:

  • Read ?igraph.plotting for the complete list of plotting parameters.
  • If you don't want the frame on the vertices, set vertex.frame.color to the same value as vertex.color.
  • Use layout.grid, see ?layout.grid.
  • Use vertex.label to set the labels.
  • If you want to omit some edges, then delete them, or set their width to zero or their color to background color.
  • If you want to omit some vertices, then attach the coordinates calculated by layout.grid as vertex attributes, and then remove the vertices from the graph.

Something like this could work:

g <- graph.lattice( c(5,5) )
lay <- layout.grid(g)
V(g)$x <- lay[,1]
V(g)$y <- lay[,2]
V(g)$color <- V(g)$frame.color <- "darkolivegreen"
V(g)$label.color <- "lightgrey"
V(g)$label <- paste(V(g)$x+1, V(g)$y+1, sep=",")

To remove the edges, you can select them based on the coordinates of the vertices:

torem <- c(E(g)[ V(g)[x==0] %--% V(g)[x==0] ], 
           E(g)[ V(g)[y==0] %--% V(g)[y==0] ], 
           E(g)[ V(g)[x==4] %--% V(g)[x==4] ], 
           E(g)[ V(g)[y==4] %--% V(g)[y==4] ])
g2 <- delete.edges(g, torem)

And then remove the vertices and plot:

g3 <- delete.vertices(g2, V(g2)[ x %in% c(0,4) & y %in% c(0,4) ])
plot(g3, layout=cbind(V(g3)$x, V(g3)$y))

plot

Upvotes: 1

Related Questions