代码之家  ›  专栏  ›  技术社区  ›  J. Doe

对特定顶点的权重应用阈值

  •  0
  • J. Doe  · 技术社区  · 6 年前

    给定数据帧:

    > dput(rel.matrix)
    structure(list(from = c("A", "A", "A", "A", "B", "B", "B", "B", 
    "C", "C", "C", "C", "C", "C", "D", "D", "D", "D", "E", "E", "E", 
    "E", "F", "F", "F", "F", "F", "F", "G", "G", "G", "G", "H", "H", 
    "H", "H", "I", "I", "I", "I", "J", "J", "J", "J", "K", "K"), 
        to = c("B", "C", "D", "E", "A", "C", "D", "E", "A", "B", 
        "D", "E", "F", "K", "A", "B", "C", "E", "A", "B", "C", "D", 
        "C", "G", "H", "I", "J", "K", "F", "H", "I", "J", "F", "G", 
        "I", "J", "F", "G", "H", "J", "F", "G", "H", "I", "C", "F"
        ), weight = c(0.09137056, 0.2677665, 0.09137056, 0.09137056, 
        0.09137056, 0.09517766, 0.02284264, 0.02284264, 0.2677665, 
        0.09517766, 0.09517766, 0.09517766, 0.3730964, 0.1675127, 
        0.09137056, 0.02284264, 0.09517766, 0.02284264, 0.09137056, 
        0.02284264, 0.09517766, 0.02284264, 0.3730964, 0.09517766, 
        0.09517766, 0.2677665, 0.09517766, 0.1675127, 0.09517766, 
        0.02284264, 0.09137056, 0.02284264, 0.09517766, 0.02284264, 
        0.09137056, 0.02284264, 0.2677665, 0.09137056, 0.09137056, 
        0.09137056, 0.09517766, 0.02284264, 0.02284264, 0.09137056, 
        0.1675127, 0.1675127)), row.names = c(NA, -46L), class = "data.frame")
    

    和两组节点

    > dput(soi)
    c("A", "I", "G")
    > dput(all_nodes)
    c("A", "B", "C", "D", "E", "F", "G", "H", "I", "J", "K")
    

    我有以下函数,在权重的节点上应用一个阈值,在 soi all_nodes 变量,最后绘制出图表。

    现在的情况是,正如您所看到的,阈值应用于所有节点的权重,而不考虑设置( soi公司 所有\u节点 )它们属于。其结果是从绘图中删除属于 soi公司 这是我不想要的。

    实际上,我想画出所有属于 soi公司 使用设置和筛选 threshold 只有那些不属于 soi公司 哪些是 setdiff(all_nodes,soi)

    下面是函数。

    graph_rel_network <- function ( rel.matrix, nodes.names, soi.names, threshold )
    {
      # setup vertices meta data which contain soi and not soi
      meta <- cbind( nodes.names, issoi = nodes.names %in% soi.names )
    
      # read in a graph: the weights are in the edge attributes
      g <- igraph::graph_from_data_frame(rel.matrix, directed = TRUE, vertices =  meta)
      g <- as.undirected(g, mode = "collapse")
      # rescale weights
      E(g)$weight2 <- 9 * E(g)$weight / max(E(g)$weight)
    
      # remove edges acoarding to the threshold
      g_sub <- delete.edges(g, E(g)[weight2 <= threshold ])
    
      # remove vertices with 0 degree
      g_sub <- delete.vertices(simplify(g_sub), degree(g_sub)==0)
    
      # color vertices that belongs to soi
      V(g_sub)$color <- ifelse(V(g_sub)$issoi == TRUE, "gold", "tomato")
    
      # plot the graph
      plot.igraph( g_sub,
                   edge.width=E(g_sub)$weight2,
                   vertex.label.dist=0,
                   vertex.frame.color="gray",
                   vertex.label.color="black")
    
      legend(x=-1.5, y=-1.1,
             c("Nodes of interest","Most Relevant nodes"),
             pch=21,
             pt.bg= c('gold','tomato'),
             pt.cex=2,
             cex=1,
             bty="n",
             ncol=1)
    
    }
    

    例如,您可以执行 graph_rel_network( rel.matrix, all_nodes, soi, 3)

    在最后一个绘图节点中, G 当它属于时不会出现 soi公司

    有没有办法不过滤 soi公司 节点?

    1 回复  |  直到 6 年前
        1
  •  1
  •   emilliman5    6 年前

    保留所有 soi 节点只需向 delete.vertices 电话:

    graph_rel_network <- function (rel.matrix, nodes.names, soi.names, threshold )
    {
      # setup vertices meta data which contain soi and not soi
      ###changed meta to a data.frame so that issoi is stored as logical instead of character
      meta <- data.frame(nodes.names, issoi = nodes.names %in% soi.names )  
      # read in a graph: the weights are in the edge attributes
      g <- igraph::graph_from_data_frame(rel.matrix, directed = TRUE, vertices =  meta)
      g <- as.undirected(g, mode = "collapse")
    
      # rescale weights
      E(g)$weight2 <- 9 * E(g)$weight / max(E(g)$weight)
    
      ###Create a subnetwork of soi nodes only
      g_soi  <- induced_subgraph(g, vids = V(g)[V(g)$name %in% soi.names])
      # remove edges acoarding to the threshold
      g_sub <- delete.edges(g, E(g)[E(g)$weight2 <= threshold])
    
      # remove vertices with 0 degree
      g_sub <- delete.vertices(g_sub, (degree(g_sub)==0 & !V(g)$issoi))
    
      ###Merge the tresholded allnode network with the soi network
      g_merge <- union(g_sub, g_soi)
    
      ###Resolve some attribute naming conflicts from the merge
      E(g_merge)$weight2 <- colMeans(rbind(E(g_merge)$weight2_2, E(g_merge)$weight2_1), na.rm = T)
      # color vertices that belongs to soi
      V(g_merge)$color <- ifelse(V(g_merge)$issoi_1, "gold", "tomato")
    
      # plot the graph
      plot.igraph( g_merge,
                   edge.width=E(g_merge)$weight2,
                   vertex.label.dist=0,
                   vertex.frame.color="gray",
                   vertex.label.color="black")
    
      legend(x=-1.5, y=-1.1,
             c("Nodes of interest","Most Relevant nodes"),
             pch=21,
             pt.bg= c('gold','tomato'),
             pt.cex=2,
             cex=1,
             bty="n",
             ncol=1)
    
    }
    

    enter image description here