Применение порога к весу определенных вершин - PullRequest
0 голосов
/ 15 мая 2018

С учетом кадра данных:

> 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 ИЛИ all_nodes), к которому они принадлежат.Это приводит к удалению вершин из графика, которые принадлежат множеству 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 Ответ

0 голосов
/ 15 мая 2018

Чтобы сохранить все 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

...