匹配igraph中的顶点和边缘颜色 [英] Match vertex and edge color in igraph

查看:406
本文介绍了匹配igraph中的顶点和边缘颜色的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想使用igraph用网络图表示一个大数据集。我只是不明白如何正确选择颜色。是否有可能获得具有与顶点颜色相同颜色的边缘的igraph图?在下面的示例中,我想根据已采样或未采样状态为顶点和边缘着色。另一个问题是所有边缘都没有出现在igraph上,我也不明白为什么



到目前为止,我的代码是:

  d<-data.frame(个体= c(1:10),mother_id = c(0,0,0,0,0,1, 3,7,6,7),Father_id = c(0,0,0,0,0,4,1,6,7,6),generation = c(0,0,0,0,0,1, 1,2,2,2),状态= c(已采样,未采样,未采样,已采样,已采样,已采样,未采样,未采样,已采样,' '))

#只是布局图的某些设置
g<-d $ generation
n<-nrow(d)
pos<-matrix(数据= NA,nrow = n,ncol = 2)
pos [,2]<-max(g)-g
pos [,1]<-order(g,部分= order( d $个数,递减= TRUE))-cumsum(c(0,table(g)))[g + 1]

#绘制igraph
G <-graph_from_data_frame(d )
plot(G,rescale = T,vertex.label = d $ individual,layout = pos,
edge.arrow.mode =-,
vertex.color = d $ status ,
edge.color = d $ status,
asp = 0.35)

我的追求ion与这个问题有点相似,但是我想用igraph包来做。



您可以通过传入两个来解决此问题对象,一个具有边缘及其属性,一个具有节点及其属性。
对我来说不清楚边缘应该在哪里。但是,您的代码应如下所示:

  dd<-read.table(text = 
from键入
1 6 A
3 7 B
7 8 A
6 9 B
7 10 A
4 6 B
1 7 A
6 8 B
7 9 B
6 10 A,头= T)

节点<-data.frame(id = unique(c( dd $ from,dd $ to)))
节点$ type<-sample(LETTERS [1:2],8,replace = T)
节点$ x<-c(8,3 ,5,7,1,2,4,10)#如果布局
节点$ y <-c(1、2、4、5、6、8、5、7)

节点
id类型xy
1 1 B 8 1
2 3 A 3 2
3 7 B 5 4
4 6 A 7 5
5 4 A 1 6
6 8 B 2 8
7 9 A 4 5
8 10 A 10 7

G<-graph_from_data_frame(dd ,顶点=节点)#定向的T或F?

V(G)$ color<-ifelse(V(G)$ type == A,粉红色,天蓝色)
E(G)$ color< ;-ifelse(E(G)$ type == A,粉红色,天蓝色)

edge_attr(G)
vertex_attr(G)
图( G)


I have a large data set that I want to represent with a network graph using igraph. I just don't understand how to get the colors right. Is it possible to get an igraph plot with edge having the same color as vertex color? I my example below, I would like to color vertex and edges according to the status 'sampled' or 'unsampled'. An other problem is that all the edge do not appear on the igraph, and I don't understand why

My code so far is:

d <- data.frame(individual=c(1:10), mother_id = c(0,0,0,0,0,1,3,7,6,7), father_id = c(0,0,0,0,0,4,1,6,7,6) , generation = c(0,0,0,0,0,1,1,2,2,2), status=c("sampled","unsampled","unsampled","sampled",'sampled',"sampled","unsampled","unsampled","sampled",'sampled'))

#Just some settings for layout plot
g <- d$generation
n <- nrow(d)
pos <- matrix(data = NA, nrow = n, ncol = 2)
pos[, 2] <- max(g) - g
pos[, 1] <- order(g, partial = order(d$individual, decreasing = TRUE)) - cumsum(c(0, table(g)))[g + 1]

#Plotting the igraph
G <- graph_from_data_frame(d)
plot(G, rescale = T, vertex.label = d$individual, layout = pos,
edge.arrow.mode = "-",
vertex.color = d$status,
edge.color = d$status,
asp = 0.35)

My question is somewhat similar to this question, but I would like to do it with igraph package. Ggraph node color to match edge color

Thanks for your help

解决方案

if you plot(G) you will see that the graph from data frame object is not what you expect, most likely. That is why you dont see all edges (i.e the column father_id is not used at all).

By default igraph takes the first column as "from" and the second one as "to". That is why you see 1to0, 2to0 and so on.

You can fix this by passing in two objects, one with the edges and their attributes, and one with the nodes and their attributes. It is not so clear to me where the edges should be. However, your code should look something like this:

dd <- read.table(text = "
from to type
1 6 A
3 7 B
7 8 A
6 9 B
7 10 A
4 6 B
1 7 A
6 8 B
7 9 B
6 10 A ", header=T )

nodes <- data.frame(id=unique(c(dd$from, dd$to)) )
nodes$type <- sample(LETTERS[1:2], 8, replace = T  )
nodes$x <- c(8,3,5,7,1,2,4,10) # this if for the layout
nodes$y <- c(1, 2, 4, 5, 6, 8, 5, 7)

    nodes
  id type  x y
1  1    B  8 1
2  3    A  3 2
3  7    B  5 4
4  6    A  7 5
5  4    A  1 6
6  8    B  2 8
7  9    A  4 5
8 10    A 10 7

G <- graph_from_data_frame(dd, vertices = nodes ) # directed T or F?

V(G)$color <- ifelse( V(G)$type == "A", "pink", "skyblue")
E(G)$color <- ifelse( E(G)$type == "A", "pink", "skyblue")

edge_attr(G)
vertex_attr(G)
plot(G)

这篇关于匹配igraph中的顶点和边缘颜色的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆