为ggnet中的段设置颜色

时间:2013-08-23 15:27:41

标签: r networking ggplot2

我hvar以下数据集:

structure(c(2L, 6L, 2L, 6L, 7L, 7L, 2L, 7L, 6L, 8L, 8L, 4L, 8L, 
2L, 9L, 8L, 7L, 6L, 9L, 1L, 9L, 4L, 9L, 3L, 2L, 10L, 9L, 10L, 
8L, 10L, 7L, 6L, 10L, 1L, 2L, 12L, 9L, 8L, 12L, 1L, 11L, 10L, 
2L, 44L, 79L, 10L, 8L, 47L, 45L, 51L, 9L, 11L, 74L, 75L, 77L, 
69L, 75L, 77L, 78L, 2L, 44L, 44L, 46L, 46L, 8L, 6L, 1L, 1L, 6L, 
7L, 1L, 4L, 7L, 8L, 8L, 1L, 4L, 8L, 3L, 8L, 8L, 9L, 9L, 9L, 1L, 
9L, 5L, 9L, 3L, 9L, 9L, 9L, 10L, 8L, 10L, 7L, 10L, 10L, 1L, 10L, 
10L, 9L, 12L, 12L, 1L, 12L, 12L, 12L, 12L, 7L, 7L, 44L, 44L, 
44L, 44L, 44L, 44L, 44L, 44L, 44L, 44L, 7L, 7L, 7L, 7L, 44L, 
10L, 9L, 42L, 43L, 46L, 46L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 
1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 
1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, -1L, 1L, 1L, 1L, 1L, 1L, 
1L, 1L, 1L, 1L, 1L, -1L, 1L, 1L, 1L, -1L, -1L, 1L, 1L, 1L, 1L, 
1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, -1L), .Dim = c(66L, 
3L), .Dimnames = list(NULL, c("from", "to", "impact")))

数据集表明,from和to之间的关系是正(1)或负(-1)影响。

我想用ggnet(或ggplot2)绘制一个绘制此图的网络图。

到目前为止,我已完成以下步骤:

library(network)
library(ggplot2)
library(ggnet)
library(grid)

net <- network(df2[,c(1,2)], directed = FALSE)

ggnet(net, mode = 'kamadakawai', size = 6, alpha = .5, label.nodes=F, segment.alpha = 0.5, color = "black") +
  theme(legend.position = "none") +
  theme(plot.margin = unit(c(0.1,0.1,0.1,0.1), "cm"))

这导致了这个结果: enter image description here

我想知道如何根据数据集中的影响对边缘进行着色(1 =绿色和-1 =红色)。我也想知道为什么有这么多未连接的节点......

有人可以帮我这个吗?

非常感谢。

3 个答案:

答案 0 :(得分:1)

首先,我使用的是GGally::ggnet,因为这可以通过CRAN获得。我相信这相当于github上的那个。

library(network)
library(GGally)
library(ggplot2)
# dependencies
library(grid)
library(sna)
library(intergraph)

网络图中有许多未连接节点的原因是因为您提供的节点名称不是一个完整的整数序列。例如,如果仅在名为110的节点之间提供边缘,则network()将假定存在八个名称为2:9的未连接节点。例如,

netwk1 <- network(cbind(1,2), directed = F)
get.vertex.attribute(netwk1, attrname="vertex.names")
netwk2 <- network(cbind(1,10), directed = F)
get.vertex.attribute(netwk2, attrname="vertex.names")

因此,如果将节点名称转换为不间断的序列,则将丢失所有未连接的节点。如下所示:

df2[,1:2]=as.numeric(as.factor(df2[,c(1,2)]))
net <- network(df2[,c(1,2)], directed = F)
ggnet(net, mode = 'kamadakawai', size = 6, alpha = .5, label.nodes=T, segment.alpha = 0.5, color = "black") +
  theme(legend.position = "none") +
  theme(plot.margin = unit(c(0.1,0.1,0.1,0.1), "cm"))

您可以使用segment.color中的ggnet()参数为边缘着色:

edge_color = ifelse(df2[,3]==-1, "red", "green")
ggnet(net, mode = 'kamadakawai', size = 6, alpha = .5, segment.color=edge_color, label.nodes=T, segment.alpha = 0.5, color = "black") +
  theme(legend.position = "none") +
  theme(plot.margin = unit(c(0.1,0.1,0.1,0.1), "cm"))

答案 1 :(得分:0)

您应该包含node.group = type

   ggnet(net, mode = 'kamadakawai', size = 6, alpha = .5, label.nodes=F, segment.alpha = 0.5, color = "black", node.group=type) +
   theme(legend.position = "none")+
   theme(plot.margin = unit(c(0.1,0.1,0.1,0.1), "cm"))

答案 2 :(得分:0)

您也可以使用network包直接构建绘图,我认为ggnet正在使用它。

# Dominik's data structure
edgelist<-structure(c(2L, 6L, 2L, 6L, 7L, 7L, 2L, 7L, 6L, 8L, 8L, 4L, 8L, 
2L, 9L, 8L, 7L, 6L, 9L, 1L, 9L, 4L, 9L, 3L, 2L, 10L, 9L, 10L, 
8L, 10L, 7L, 6L, 10L, 1L, 2L, 12L, 9L, 8L, 12L, 1L, 11L, 10L, 
2L, 44L, 79L, 10L, 8L, 47L, 45L, 51L, 9L, 11L, 74L, 75L, 77L, 
69L, 75L, 77L, 78L, 2L, 44L, 44L, 46L, 46L, 8L, 6L, 1L, 1L, 6L, 
7L, 1L, 4L, 7L, 8L, 8L, 1L, 4L, 8L, 3L, 8L, 8L, 9L, 9L, 9L, 1L, 
9L, 5L, 9L, 3L, 9L, 9L, 9L, 10L, 8L, 10L, 7L, 10L, 10L, 1L, 10L, 
10L, 9L, 12L, 12L, 1L, 12L, 12L, 12L, 12L, 7L, 7L, 44L, 44L, 
44L, 44L, 44L, 44L, 44L, 44L, 44L, 44L, 7L, 7L, 7L, 7L, 44L, 
10L, 9L, 42L, 43L, 46L, 46L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 
1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 
1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, -1L, 1L, 1L, 1L, 1L, 1L, 
1L, 1L, 1L, 1L, 1L, -1L, 1L, 1L, 1L, -1L, -1L, 1L, 1L, 1L, 1L, 
1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, -1L), .Dim = c(66L, 
3L), .Dimnames = list(NULL, c("from", "to", "impact")))

# construct a network object
net<-as.network.matrix(edgelist,matrix.type='edgelist',
                    ignore.eval=FALSE,names.eval='impact')

# plot it, using the impact edge attribute to control edge color
plot(net,edge.col=ifelse(net%e%'impact'==1,'green','red'))

enter image description here

Nate Pope上面关于改变ids范围去除隔离物的答案仍然适用。但是,您可以要求plot.network不要绘制隔离区:

plot(net,edge.col=ifelse(net%e%'impact'==1,'green','red'),displayisolates=FALSE)

enter image description here