igraph ggraph r 中的颜色节点
color nodes in igraph ggraph r
我想根据原始数据框中的属性为图形的节点着色。但我想我还没有把那个审美变量“带入”到图表中。
此处的示例有效:
library(dplyr)
library(igraph)
library(ggraph)
data <-
tibble(
from = c("a", "a", "a", "b", "b", "c"),
to = c(1, 2, 3, 1, 4, 2),
type = c("X", "Y", "Y", "X", "Y", "X")
)
graph <-
graph_from_data_frame(data)
ggraph(graph,
layout = "fr") +
geom_node_point() +
geom_edge_link()
我想要 geom_node_point(aes(color = type))
之类的东西,但没有在图表中找到类型?
这里的问题是您将 type
列添加为 edge-attribute,而 geom_node_point
需要 vertex-attribute(参见 ?graph_from_data_frame
:附加列被视为边缘属性。)。
另一个问题是 type
对于任一节点列都不一致(例如 a
与类型 X
以及 Y
相关联,节点 [=18= 也是如此]).
要解决第一个问题,您可以将额外的顶点信息添加到 graph_from_data_frame
函数的 vertices
参数中。
解决这两个问题的最简单方法是在创建图表后添加类型属性:
data <-
tibble(
from = c("a", "a", "a", "b", "b", "c"),
to = c(1, 2, 3, 1, 4, 2)
)
graph <- graph_from_data_frame(data)
V(graph)$type <- bipartite.mapping(graph)$type
bipartite.mapping
函数将 TRUE
或 FALSE
一致地添加到不同类型的每个顶点。
我想根据原始数据框中的属性为图形的节点着色。但我想我还没有把那个审美变量“带入”到图表中。
此处的示例有效:
library(dplyr)
library(igraph)
library(ggraph)
data <-
tibble(
from = c("a", "a", "a", "b", "b", "c"),
to = c(1, 2, 3, 1, 4, 2),
type = c("X", "Y", "Y", "X", "Y", "X")
)
graph <-
graph_from_data_frame(data)
ggraph(graph,
layout = "fr") +
geom_node_point() +
geom_edge_link()
我想要 geom_node_point(aes(color = type))
之类的东西,但没有在图表中找到类型?
这里的问题是您将 type
列添加为 edge-attribute,而 geom_node_point
需要 vertex-attribute(参见 ?graph_from_data_frame
:附加列被视为边缘属性。)。
另一个问题是 type
对于任一节点列都不一致(例如 a
与类型 X
以及 Y
相关联,节点 [=18= 也是如此]).
要解决第一个问题,您可以将额外的顶点信息添加到 graph_from_data_frame
函数的 vertices
参数中。
解决这两个问题的最简单方法是在创建图表后添加类型属性:
data <-
tibble(
from = c("a", "a", "a", "b", "b", "c"),
to = c(1, 2, 3, 1, 4, 2)
)
graph <- graph_from_data_frame(data)
V(graph)$type <- bipartite.mapping(graph)$type
bipartite.mapping
函数将 TRUE
或 FALSE
一致地添加到不同类型的每个顶点。