我需要一些关于我试图用 ggraph 绘制的网络的帮助。我希望边缘颜色取决于边缘数据框中的权重值。我已经尝试了下面的代码(以及其他代码),但没有结果。有人熟悉这个主题吗?谢谢!
library(ggraph)
library(igraph)
# Data frame nodes
nodes <- data.frame(
id = c(1, 2, 3, 4, 5),
label = c("Especie1", "Especie2", "Especie3", "Especie4", "Especie5")
)
# Data frame edges
edges <- data.frame(
from = c(1, 2, 3, 4, 5, 1, 3),
to = c(2, 3, 4, 5, 1, 4, 5),
weight = c(60, 40, 30, 70, 80, 90, 20)
)
# I create the graph
graph <- graph_from_data_frame(edges, vertices = nodes)
# Graphic with ggraph
ggraph(graph, layout = "stress") +
geom_edge_link(aes(color = ifelse(edges$weight > 50, "Above Threshold", "Below Threshold"))) +
geom_node_text(aes(label = label)) +
scale_color_manual(values = c("Above Threshold" = "red", "Below Threshold" = "yellow")) +
theme_void()
我尝试过的代码为创建的“高于阈值”和“低于阈值”的类别选择两种随机颜色,但没有选择我在“scale_color_manual”中选择的颜色
使用
scale_edge_color_manual()
代替 scale_color_manual()
。
ggraph(graph, layout = "stress") +
geom_edge_link(aes(color = ifelse(edges$weight > 50, "Above Threshold", "Below Threshold"))) +
geom_node_text(aes(label = label)) +
scale_edge_color_manual(values = c("Above Threshold" = "red", "Below Threshold" = "yellow")) +
theme_void()