[英]Plot a simple network graph in R
我有一个数据框
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/
你可以试试:
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")
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.