【问题标题】:R: Change color igraph based on condition if element in list of elementsR:如果元素列表中的元素,则根据条件更改颜色igraph
【发布时间】:2020-01-30 20:41:49
【问题描述】:

我有一个 iGraph 对象。我想将某些子节点的颜色更改为浅蓝色,而其他所有子节点的颜色都是橙色。

以下代码为所有节点返回 false,因此所有节点都返回为橙色。这是为什么呢?

V(sub_net)
> 20/20 vertices, named, from 348f7f4:
> [1] 115 325 423 446 482 485 487 491 492 511 512 517 518 519 521 523 541 561 621 712

typeof(V(sub_net)[1])
> [1] "integer"

V(sub_net)$color <- ifelse(V(sub_net) %in% c(511,   541, 518, 519),"lightblue", "orange")

V(sub_net)$color
> [1] "orange" "orange" "orange" "orange" "orange" "orange" "orange" "orange" "orange" "orange"
>[11] "orange" "orange" "orange" "orange" "orange" "orange" "orange" "orange" "orange" "orange"

我的代码显示有 20 个整数类型的节点,并尝试将 511、541、518 和 519 重新分配为浅蓝色(否则为橙色)。但是当我绘制图表时,所有节点都是橙色的。

我相信我的 ifelse 声明是罪魁祸首,但我不明白这个问题。

添加更多关于我如何获取数据的上下文:

infile<-"MergerNet_Jan21_2016_forR.csv"
## Load package
library(igraph)
el = read.csv(infile, header = TRUE, sep = ",")
g_acq = graph.data.frame(el, directed = TRUE, vertices= NULL)

### List of all the years represented in the set
el[,"year"]
df <- data.frame(el)
class(df$weight)
# ---
#[1] "integer"
# ---

class(df$source)
# ---
# [1] "factor"
# ---

class(el)
# ---
# [1] "data.frame"
# ---

# Edges
ecount(g_acq)
## Vertices
vcount(g_acq)

#Is it a simple graph? No!
## Check whether Self_loops exist, as do multiple edges
is.simple(g_acq)
# ---
#[1] FALSE
# ---
E(g_acq)$weight 
g_acq_simpl<-simplify(g_acq)
### The above should default to the option below, to sum the existing edge weights ### when combining them
##g_acq_simpl<-simplify(g_acq,edge.attr.comb="sum" )

E(g_acq_simpl)$weight 
# Will use the inverse of log weight for shortest path calculations
inv_weight<-1/log(E(g_acq_simpl)$weight  + 1)
num_weight<-E(g_acq_simpl)$weight 
length(inv_weight)

# Remove disconnected components to create a strongly connected component. We will use this component to calculate closeness and shortest path distances.
g_acq_scc <-g_acq_simpl - vertices('814', '925', '928')
inv_weight_scc<-1/log(E(g_acq_scc)$weight  + 1)

plot(g_acq_scc)
sub_net<-induced.subgraph(g_acq_simpl, v=c('511', '541',
                                           '518', '519', '517', '325', '423', '446', '512', '523',
                                           '561', '621', '115', '482', '485', '487', '491', '492',
                                           '521', '712' ))

注意,我已经更改了我的代码以尝试整数和字符串参数,但以下返回 false,令我困惑:

V(sub_net) %in% c(511, 541, 518, 518)
>  [1] FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE [20] FALSE

is.element(el = V(sub_net),set=c(511, 541, 518, 518))
>  [1] FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE [20] FALSE

V(sub_net) %in% c('511', '541', '518', '518')
>  [1] FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE [20] FALSE

is.element(el = V(sub_net),set=c('511', '541', '518', '518'))
>  [1] FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE [20] FALSE

【问题讨论】:

  • 您的代码不应出错。你介意分享数据和图表来复制你的语法吗?
  • 我现在已经完成了,在第二个代码块中可见
  • 谢谢,但至少没有数据的 sn-p "MergerNet_Jan21_2016_forR.csv" 我无法重现您的图表。请查看stackoverflow.com/questions/5963269/…,了解如何共享示例数据以重现您的问题。

标签: r if-statement igraph


【解决方案1】:

尝试在plot 中插入您的ifelse 语句,在vertex.color 参数中,如下所示:

plot(sub_net, vertex.color= ifelse(V(sub_net) %in% c(511, 541, 518, 519),"lightblue", "orange"))

编辑: 来自here

V(sub_net) 应更改为 V(sub_net)$name 以返回正确的输出

【讨论】:

  • 我也试过了;条件语句似乎没有触发任何错误;它只是为所有元素返回 false。真正的大脑谜题。
猜你喜欢
  • 2017-05-20
  • 1970-01-01
  • 1970-01-01
  • 2020-09-19
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多