如何在R igraph中获得正确的Pie节点?

问题描述 投票:0回答:1

我正在创建一个具有派生节点的网络,但是我遇到了一些问题:

我对馅饼切片的值与生成的图像不匹配

示例:

#generate random slice for my pie charts 
valuesq <- lapply(1:23, function(x) sample(0:5,2)) 

[[1]]
[1] 1 0

[[2]]
[1] 5 1

[[3]]
[1] 1 0

[[4]]
[1] 4 5

[[5]]
[1] 5 3

[[6]]
[1] 1 2

[[7]]
[1] 5 2

[[8]]
[1] 0 2

[[9]]
[1] 0 3

[[10]]
[1] 1 5

[[11]]
[1] 5 3

[[12]]
[1] 5 4

[[13]]
[1] 3 1

[[14]]
[1] 0 3

[[15]]
[1] 4 5

[[16]]
[1] 3 1

[[17]]
[1] 4 1

[[18]]
[1] 4 2

[[19]]
[1] 1 2

[[20]]
[1] 2 4

[[21]]
[1] 1 4

[[22]]
[1] 1 2

[[23]]
[1] 4 1


#plot my chart which has 23 nodes 
plot(newig,vertex.shape="pie",vertex.size=20, vertex.pie=valuesq, label.dist=1, label.degree=pi/4)

它产生此图:

<< img src =“ https://image.soinside.com/eyJ1cmwiOiAiaHR0cHM6Ly9pLnN0YWNrLmltZ3VyLmNvbS91ckpDUS5wbmcifQ==” alt =“剧情图像”>

显然这是错误的,例如节点4(IV)是一种纯色,但是定义其切片的矢量是4 5

我不太明白为什么会发生这种情况,任何帮助都很好

[我的第二个问题是如何将馅饼切片与颜色匹配,例如第一列中的所有切片=红色,第二列中的所有切片=黄色?

r networking igraph pie-chart
1个回答
0
投票

最可能的原因是,将标签分配给节点的代码(不包括在内)将标签分配给了错误的节点。请注意,igraph对饼进行切片的方式是,总面积等于在valueq中为该节点提供的所有值的总和,因此对于标记为4的节点,values [[correctnodenumber]]等于一个矢量(任何数字)和零。关于第二个问题,答案是肯定的。您只需要提供一个十六进制值(谷歌单词“ colorpicker”)。希望以下代码和说明有所帮助。

library(igraph)

newig <- graph.star(n=23,mode="undirected")

valuesq <- lapply(1:23, function(x) c(sample(0:5,2)))
#you can assign up to 5 colors(random colors or exact hex values)
#to 5 slices. Exact hex values could be assigned to all nodes:
V(newig)$pie.color=list(c("#E495A5", "#BDAB66"))
#or to a specific node 
V(newig)[10]$pie.color=list(c("#00FF66FF", "#CC00FFFF"))
# let's set a seed number to regenerate the graph so that nodes are 
#positioned at the same place every time you run the code.

set.seed(1234)

plot(newig,
 vertex.shape="pie", 
 vertex.size=25, 
 vertex.pie=valuesq,
 vertex.label.dist=0.8,
 vertex.label.cex=0.6, 
 vertex.label.degree=pi,
 vertex.label=LETTERS[1:23])
© www.soinside.com 2019 - 2024. All rights reserved.