我需要创建一个网络图,其中节点和边的颜色将取决于参数。虽然我已经能够渲染图形,但是边缘的颜色没有正确反映,并且图例也没有出现。 Stackoverflow中的类似问题已出现在以下链接中:
Color legend not appearing in ggnet2 plot
以下是可重现的代码。任何帮助都会很棒
library(network)
library(sna)
library(ggplot2)
library(ggnetwork)
library(sna)
library(GGally)
edge_df = data.frame("Start" = c(1,2,4,5), "End" = c(2,3,3,1), "strength" =
c("uncovered","uncovered","covered","uncovered"))
vertex_df = data.frame("Nodes" = c(1,2,3,4,5), "strength" =
c("covered","uncovered","uncovered",
"uncovered","covered"))
net = network(edge_df[,1:2], directed = FALSE)
net %v% "nodes" = ifelse(vertex_df$strength == "covered", "Nodes Covered",
"Nodes Uncovered")
net %v% "color_node" = ifelse(net %v% "nodes" == "Nodes Covered",
"steelblue", "yellow")
net %e% "weight" = ifelse(edge_df$strength == "covered", "Edges Covered",
"Edges Uncovered")
set.edge.attribute(net, "color", ifelse(net %e% "weight" == "Edges Covered",
"green", "red"))
set.seed(123)
ggnet2(net, color = "color_node",
edge.color = "color",
edge.size = 0.5,
node.size = 1,
mode = "fruchtermanreingold",
color.legend = c("timers","weight")) +
theme(panel.background = element_rect(fill = "black"),
text = element_text(color = "white"),
legend.position = "bottom")