簡體   English   中英

使用igraph繪制網絡時,如何根據特定的actor屬性更改頂點顏色?

[英]How can I change the vertex color when plotting a network using igraph depending on specific actor attribute?

我正在使用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”丟失,沒有默認值。

如何設置顏色?

這里有兩個問題。 首先, colors功能按名稱列出了所有可用的顏色。 我認為您只是想獲取帶有“ black”和“ white”顏色的列表。這比您嘗試的要簡單。您只需要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的定義colrscolrs <- c("white", "black")

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM