我正在使用R中的statnet套件来估计具有ERGM的网络。我想使用igraph软件包来可视化网络,该软件包的大小按程度中心,而节点的颜色按领导位置。解释一下:这是一个共享的领导网络,我想直观地看到一个人是否有正式的领导职位(LSPosition = 1)(以黑色显示)或没有(领导地位(LSPosition = 0),以白色显示)。
这是我到目前为止的代码(graphExample是我的网络,Data_Axample是一个actor属性数据集):
library(igraph)
degreeExample <- centralization.degree(graphExample)$res
V(graphExample)$size <- degreeExample
V(graphExample)$LSPosition <- Data_Example$LSPosition
colrs <- colors(c("black","white"))
V(graphExample)$color <- colrs[V(graphExample)$LSPosition]
E(graphExample)$arrow.size <- 0.5
plot(graphExample, vertex.label = NA, vertex.label.family = "Arial")
legend("bottomleft", c("Employee in no leading position",
"Employee in a leading position"),
pch = 21, col = "#777777", pt.bg = colrs, pt.cex = 2, cex = 0.8, bty = "n", ncol = 1)
问题是,对于colrs <- colors(c("black","white"))
,我收到以下错误消息:
Error in if (distinct) c[!duplicated(t(col2rgb(c)))] else c :
argument is not interpretable as logical
In addition: Warning message: In if (distinct) c[!duplicated(t(col2rgb(c)))] else c :
the condition has length > 1 and only the first element will be used
我还尝试了以下方法:
V(graphExample)$color <- ifelse(V(graphExample)$LSPosition==1, "black", ifelse(V(graphExample)$LSPosition==0, "white"))
但是我得到了错误:
ifelse(V(graphSLO_V1)$ PositionO == 0,“ white”)中的错误: 缺少参数“ no”,没有默认值。
如何设置颜色?
答案 0 :(得分:1)
这里有两个问题。首先,colors
函数按名称列出所有可用的颜色。我认为您只是想获取带有“黑色”和“白色”颜色的列表。这比您尝试的要简单。您只需要colrs <- c("black","white")
,但是根据您的描述,V(graphExample)$LSPosition
将具有0或1的值。列表colrs
应该以1或2进行索引。要做的简单事情是使用
V(graphExample)$color <- colrs[V(graphExample)$LSPosition + 1]
但是,这会使(LSPosition = 0)为黑色(您想要白色),而(LSPosition = 1)为白色(您想要黑色)。因此,我将使用上面带有+1的行,但将colrs
的定义更改为colrs <- c("white", "black")