加载ggnetwork,网络,sna,igraph和ggplot:
library(igraph)
library(network)
library(sna)
library(ggnetwork)
library(ggplot2)
创建一个具有五个顶点和两个边的igraph对象:
test <- structure(list(5, TRUE, c(0, 3), c(4, 1), c(0, 1),
c(1, 0), c(0, 1, 1, 1, 2, 2),
c(0, 0, 1, 1, 1, 2),
list(c(1, 0, 1),
structure(list(),
.Names = character(0)),
list(name = c("3", "6", "7", "2", "1")),
list(weight = c(3.30310760667904, 3.55724789915966))), environment),
class = "igraph")
检查节点和边的数量:
igraph::ecount(test)
[1] 2
igraph::vcount(test)
[1] 5
以下代码有效(它不起作用,请注意geom_nodes()中的颜色值数量错误,应该有五个值,但给出了七个):
ggplot(test,aes(x = x, y = y, xend = xend, yend = yend)) +
geom_edges(arrow = arrow(), color = 'black') +
geom_nodes(size =10, aes(color=1:7/(7))) +
scale_color_gradient(low = "green", high = "red")
如果使用正确数量的geom_nodes(aes(color =))值,则会生成错误:
ggplot(test, aes(x = x, y = y, xend = xend, yend = yend)) +
geom_edges(arrow = arrow(), color ='black') +
geom_nodes(size =10, aes(color=1:5/(5))) +
scale_color_gradient(low = "green", high = "red")
Error: Aesthetics must be either length 1 or the same as the data (7): colour
请注意,geom_nodes似乎需要| color | = |节点| + | edges,这是为什么? 有没有解决的办法? 如果传递了7种颜色,它将如何决定使用5种颜色作为节点颜色?