如何在R中绘制有向无环格子图

时间:2023-02-02 22:56:35

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:

我需要绘制大小为m x n的有向无环格子图,类似于此图片,但在轮廓上没有边,角上没有顶点:

如何在R中绘制有向无环格子图

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?

这可能与graph.lattice函数有关吗?如果是,如何设置这样的顶点标签(即(x,y)格式,而不仅仅是整数)并删除提到的边和顶点?此外,是否可以在不使用tkplot函数的情况下将图形绘制在这样的位置(如图片中)并旋转它?

1 个解决方案

#1


1  

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.
  • 阅读?igraph.plotting获取完整的绘图参数列表。
  • If you don't want the frame on the vertices, set vertex.frame.color to the same value as vertex.color.
  • 如果您不想要顶点上的框架,请将vertex.frame.color设置为与vertex.color相同的值。
  • Use layout.grid, see ?layout.grid.
  • 使用layout.grid,参见?layout.grid。
  • Use vertex.label to set the labels.
  • 使用vertex.label设置标签。
  • 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.
  • 如果要省略某些顶点,则将layout.grid计算的坐标作为顶点属性附加,然后从图中删除顶点。

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))

如何在R中绘制有向无环格子图

#1


1  

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.
  • 阅读?igraph.plotting获取完整的绘图参数列表。
  • If you don't want the frame on the vertices, set vertex.frame.color to the same value as vertex.color.
  • 如果您不想要顶点上的框架,请将vertex.frame.color设置为与vertex.color相同的值。
  • Use layout.grid, see ?layout.grid.
  • 使用layout.grid,参见?layout.grid。
  • Use vertex.label to set the labels.
  • 使用vertex.label设置标签。
  • 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.
  • 如果要省略某些顶点,则将layout.grid计算的坐标作为顶点属性附加,然后从图中删除顶点。

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))

如何在R中绘制有向无环格子图