我有一个数据框
mydf <- data.frame(ID = c(1,2,3,4,5), MatchedID = c(3,4,2,5,1), Weight = c(12,45,5,19,9))
我希望绘制一个网络图,显示ID和matchedID之间的关系以及权重作为该关系的强度。 用标签表示这个的最佳方法是什么?我喜欢https://briatte.github.io/ggnet/
的那些答案 0 :(得分:1)
您可以尝试:
library(igraph)
g <- graph_from_data_frame(mydf, directed=TRUE)
g <- set_edge_attr(g, "weight", value = mydf$Weight)
plot(g, edge.width = E(g)$weight/5, edge.label=E(g)$weight)
或者使用ggplot2
library(GGally)
library(sna)
library(network)
library(tidyverse)
mydf %>%
spread(MatchedID, Weight, fill = 0) %>%
select(-ID) %>%
network(names.eval = "weights", ignore.eval = FALSE) %>%
ggnet2(label = TRUE, edge.label = "weights")