r - 在 R 中的 igraph 中,是否可以在顶点对象周围创建虚线?

标签 r igraph

在 R 中的 igraph 中,我目前有一个图,如下所示:

enter image description here

这是由代码制成的:

g <- make_undirected_graph(edges = c(1, 3, 2, 1, 2, 4, 3, 4, 4, 5), n = 5)

我想在顶点上添加虚线,它们是圆圈。有一个edge.label选项,但没有 vertex.label选项。有没有另一种方法可以做到这一点?谢谢。

最佳答案

您可以定义自己的形状:https://igraph.org/r/doc/shapes.htmlhttps://r.789695.n4.nabble.com/Drawing-a-dotted-circle-td4655331.html 处给出了一个带有虚线边框的点的示例。 .在 https://lists.gnu.org/archive/html/igraph-help/2013-03/msg00030.html 中给出的创建新形状的完整示例.另请参阅 ?add_shape 中的更多示例.下面的示例调整了来自lists.gnu.org 的代码以组合所有内容。

新建函数 igraph形状

myimg <- function(coords, v=NULL, params) {
  vertex.color <- params("vertex", "color")
  if (length(vertex.color) != 1 && !is.null(v)) {
    vertex.color <- vertex.color[v]
  }
  vertex.size  <- 1/200 * params("vertex", "size")
  if (length(vertex.size) != 1 && !is.null(v)) {
    vertex.size <- vertex.size[v]
  }
  vertex.frame.color <- params("vertex", "frame.color")
  if (length(vertex.frame.color) != 1 && !is.null(v)) {
    vertex.frame.color <- vertex.frame.color[v]
  }
  vertex.frame.width <- params("vertex", "frame.width")
  if (length(vertex.frame.width) != 1 && !is.null(v)) {
    vertex.frame.width <- vertex.frame.width[v]
  }
  ltype <- params("vertex", "ltype")
  if (length(ltype) != 1 && !is.null(v)) {
    ltype <- ltype[v]
  }   

  mapply(coords[,1], coords[,2], vertex.color, vertex.frame.color,
         vertex.size, vertex.frame.width, ltype, 
         FUN=function(x, y, bg, fg, size, lwd, lty) {
           symbols(x=x, y=y, bg=bg, fg=fg, lwd=lwd, lty=lty,
                   circles=size, add=TRUE, inches=FALSE)
         })
  }

然后你做 igraph使用 add_shape 识别形状.您可以使用 parameters 设置默认参数值争论。
library(igraph)

g <- make_undirected_graph(edges = c(1, 3, 2, 1, 2, 4, 3, 4, 4, 5), n = 5)

add_shape("myimg",  plot=myimg, 
          parameters = list(
            vertex.frame.color=1, 
            vertex.frame.width=1,
            vertex.ltype=1))

然后绘图
plot(g,  vertex.shape="myimg", 
         vertex.frame.color=1:5, 
         vertex.frame.width=5, 
         vertex.ltype=1:5,
         vertex.color=6:10,
         vertex.size=seq(50, 80, length=5))

要让所有的边框都加点,只需使用 vertex.ltype="dotted"vertex.ltype=3 .

enter image description here

关于r - 在 R 中的 igraph 中,是否可以在顶点对象周围创建虚线?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/58543917/

相关文章:

regex - 在 R 函数的 list.files 中使用正则表达式

使用 docker 运行 Rstudio-connect

r - PlotCirc 中的标签旋转(DescTools 包)

r - dplyr 折叠时间段

python - 在 Mac El Capitan 上安装 python-igraph 时出现问题

performance - 将JIT与R一起使用可能存在的缺点?

r - 将数据帧转换为 igraph 错误 : Duplicate vertex names

r - 改进 R 中网络图的布局和分辨率

r - plotly 网络: edges are drawn over the vertices (should be the opposite)

networking - 如何沿时间轴组织 i-graph 有向网络?