使用 visIgraph 为顶点子集着色,但不为它们的边着色
Color a subset of vertices but not their edges with visIgraph
如何设置图形节点子集的颜色而不使用 visNetwork::visIgraph
为它们的边着色?
目前,我的函数 vis_graph_prototyping
生成了所需的图,除了绿色的 selected 节点也有其关联的绿色边。我怎样才能让这些边显示为默认颜色,并且只有那些我通过 E(g, P = as.vector(t(linkages))
或 E(g, path = pathway, directed = TRUE)
分别 select 的边显示为红色?
当前输出的图像显示在可重现示例 R 代码段下方。
rnd_dag <- function(p = 25, p_edge = 0.2, weighted = FALSE, seed = 123) {
if (seed) set.seed(seed)
A <- matrix(0, p, p)
A[lower.tri(A)] <- sample(c(0, 1), p*(p-1)/2, replace = TRUE,
prob = c(1 - p_edge, p_edge))
if (weighted) {A[A == 1] <- runif(length(A[A == 1]), min = -1, max = 1)}
return(A)
}
linkages <- matrix(c(9, 1,
12, 1,
11, 2), ncol = 2, byrow = TRUE)
vis_graph_prototyping <- function(A, linkages = NULL, pathway = NULL) {
g <- graph_from_adjacency_matrix(A, mode = "directed", weighted = TRUE)
stopifnot(is.null(linkages) || is.null(pathway))
if (!is.null(linkages)) {
g <- set_vertex_attr(g, name = "color",
index = unique(as.vector(linkages)),
value = "green") %>%
set_edge_attr(name = "color",
index = E(g, P = as.vector(t(linkages)), directed = TRUE),
value = "red")
} else if (!is.null(pathway)) {
g <- set_vertex_attr(g, name = "color", index = pathway, value = "green") %>%
set_edge_attr(name = "color",
index = E(g, path = pathway, directed = TRUE), value = "red")
}
visIgraph(g, layout = "layout_with_sugiyama") %>%
visOptions(highlightNearest = list(enabled = TRUE, hover = TRUE),
nodesIdSelection = TRUE)
}
vis_graph_prototyping(rnd_dag(12), linkages = linkages)
您可以设置颜色,以便您必须设置它们(而不是从节点继承它们)。
创建图形对象时,请先将颜色设置为默认颜色,然后再将边缘设置为红色。您可以找到 default colors here.
我所做的唯一更改是您的功能。看看吧。
vis_graph_prototyping <- function(A, linkages = NULL, pathway = NULL) {
g <- graph_from_adjacency_matrix(A, mode = "directed", weighted = TRUE)
stopifnot(is.null(linkages) || is.null(pathway))
if (!is.null(linkages)) {
# message(print(edges))
# message(print(linkages))
g <- set_vertex_attr(g, name = "color",
index = unique(as.vector(linkages)),
value = "green") %>%
set_edge_attr(name = "color",
index = E(g), # <----- all edges
value = "#97C2FC") %>% # <----- default blue
set_edge_attr(name = "color",
index = E(g, P = as.vector(t(linkages)), directed = TRUE),
value = "red")
} else if (!is.null(pathway)) {
g <- set_vertex_attr(g, name = "color", index = pathway, value = "green") %>%
set_edge_attr(name = "color",
index = E(g, path = pathway, directed = TRUE), value = "red")
message(" from null path ")
message(E(g))
}
visIgraph(g, layout = "layout_with_sugiyama") %>%
visOptions(highlightNearest = list(enabled = TRUE, hover = TRUE),
nodesIdSelection = TRUE)
}
如何设置图形节点子集的颜色而不使用 visNetwork::visIgraph
为它们的边着色?
目前,我的函数 vis_graph_prototyping
生成了所需的图,除了绿色的 selected 节点也有其关联的绿色边。我怎样才能让这些边显示为默认颜色,并且只有那些我通过 E(g, P = as.vector(t(linkages))
或 E(g, path = pathway, directed = TRUE)
分别 select 的边显示为红色?
当前输出的图像显示在可重现示例 R 代码段下方。
rnd_dag <- function(p = 25, p_edge = 0.2, weighted = FALSE, seed = 123) {
if (seed) set.seed(seed)
A <- matrix(0, p, p)
A[lower.tri(A)] <- sample(c(0, 1), p*(p-1)/2, replace = TRUE,
prob = c(1 - p_edge, p_edge))
if (weighted) {A[A == 1] <- runif(length(A[A == 1]), min = -1, max = 1)}
return(A)
}
linkages <- matrix(c(9, 1,
12, 1,
11, 2), ncol = 2, byrow = TRUE)
vis_graph_prototyping <- function(A, linkages = NULL, pathway = NULL) {
g <- graph_from_adjacency_matrix(A, mode = "directed", weighted = TRUE)
stopifnot(is.null(linkages) || is.null(pathway))
if (!is.null(linkages)) {
g <- set_vertex_attr(g, name = "color",
index = unique(as.vector(linkages)),
value = "green") %>%
set_edge_attr(name = "color",
index = E(g, P = as.vector(t(linkages)), directed = TRUE),
value = "red")
} else if (!is.null(pathway)) {
g <- set_vertex_attr(g, name = "color", index = pathway, value = "green") %>%
set_edge_attr(name = "color",
index = E(g, path = pathway, directed = TRUE), value = "red")
}
visIgraph(g, layout = "layout_with_sugiyama") %>%
visOptions(highlightNearest = list(enabled = TRUE, hover = TRUE),
nodesIdSelection = TRUE)
}
vis_graph_prototyping(rnd_dag(12), linkages = linkages)
您可以设置颜色,以便您必须设置它们(而不是从节点继承它们)。
创建图形对象时,请先将颜色设置为默认颜色,然后再将边缘设置为红色。您可以找到 default colors here.
我所做的唯一更改是您的功能。看看吧。
vis_graph_prototyping <- function(A, linkages = NULL, pathway = NULL) {
g <- graph_from_adjacency_matrix(A, mode = "directed", weighted = TRUE)
stopifnot(is.null(linkages) || is.null(pathway))
if (!is.null(linkages)) {
# message(print(edges))
# message(print(linkages))
g <- set_vertex_attr(g, name = "color",
index = unique(as.vector(linkages)),
value = "green") %>%
set_edge_attr(name = "color",
index = E(g), # <----- all edges
value = "#97C2FC") %>% # <----- default blue
set_edge_attr(name = "color",
index = E(g, P = as.vector(t(linkages)), directed = TRUE),
value = "red")
} else if (!is.null(pathway)) {
g <- set_vertex_attr(g, name = "color", index = pathway, value = "green") %>%
set_edge_attr(name = "color",
index = E(g, path = pathway, directed = TRUE), value = "red")
message(" from null path ")
message(E(g))
}
visIgraph(g, layout = "layout_with_sugiyama") %>%
visOptions(highlightNearest = list(enabled = TRUE, hover = TRUE),
nodesIdSelection = TRUE)
}