0

我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"))

这导致了这个结果: 在此处输入图像描述

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

有人可以帮我弄这个吗?

非常感谢。

4

3 回答 3

1

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

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

网络图中有许多未连接节点的原因是因为您提供的节点名称不是完整的整数序列。例如,如果您仅在名为1和的节点之间提供一条边10network()则将假定存在八个名为 的未连接节点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"))
于 2013-08-23T18:01:17.080 回答
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"))
于 2013-08-23T17:24:38.617 回答
0

您也可以直接使用网络包构建绘图,我认为 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'))

在此处输入图像描述

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

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

在此处输入图像描述

于 2013-12-14T01:23:02.023 回答