如何根据集群网络中的特定值为顶点着色?
How to color vertices according to specific value in a clustered network?
这里显示了获取以下网络的代码
MyEdges <- read.table(header=TRUE,sep=",",text="
from,to
A,B
C,A
C,B
D,A
E,F
F,G
G,A
G,E
")
MyNodes <- read.table(header=TRUE,sep=",",text="
name,group
A,1
B,1
C,2
D,2
E,3
F,3
G,4
")
MyNetwork <- graph.data.frame(d=MyEdges,vertices=MyNodes,directed=TRUE)
# Make a palette of colors
library(RColorBrewer)
coul <- brewer.pal(9, "Set1")
# Create a vector of color
my_color <- coul[as.numeric(as.factor(V(MyNetwork)$group))]
set.seed(1234)
plot(MyNetwork,
layout = layout.graphopt,
vertex.color = my_color,
)
# Add a legend
legend("bottomleft",
legend=levels(as.factor(V(MyNetwork)$group)),
col = coul ,
bty = "n",
pch= 20 ,
pt.cex = 3,
cex = 1.5,
text.col=coul ,
horiz = FALSE,
inset = c(0.1, 0.1)
)
效果很好。
但是一旦我进行了聚类网络分析并且我想显示组
nodes/vertices 的颜色显示不正确。
# Plotting data
MyNetwork_cluster <- cluster_optimal(MyNetwork) #* Loading the data
set.seed(1234)
plot(MyNetwork_cluster, #* clustered network data
MyNetwork,
layout = layout.graphopt, # better layout options
vertex.color = my_color, # ?????????????????????????????
)
# Add a legend
legend("bottomleft",
legend=levels(as.factor(V(MyNetwork)$group)),
col = coul ,
bty = "n",
pch= 20 ,
pt.cex = 3,
cex = 1.5,
text.col=coul ,
horiz = FALSE,
inset = c(0.1, 0.1)
)
我的问题是“如何保留集群网络顶点的颜色模型?”
顶点颜色的选项称为col
然后:
# Plotting data
MyNetwork_cluster <- cluster_optimal(MyNetwork) #* Loading the data
set.seed(1234)
plot(MyNetwork_cluster, #* clustered network data
MyNetwork,
layout = layout.graphopt, # better layout options
col = my_color, # !!!!!!!!!!!!!!!!
)
# Add a legend
legend("bottomleft",
legend=levels(as.factor(V(MyNetwork)$group)),
col = coul ,
bty = "n",
pch= 20 ,
pt.cex = 3,
cex = 1.5,
text.col=coul ,
horiz = FALSE,
inset = c(0.1, 0.1)
)
这里显示了获取以下网络的代码
MyEdges <- read.table(header=TRUE,sep=",",text="
from,to
A,B
C,A
C,B
D,A
E,F
F,G
G,A
G,E
")
MyNodes <- read.table(header=TRUE,sep=",",text="
name,group
A,1
B,1
C,2
D,2
E,3
F,3
G,4
")
MyNetwork <- graph.data.frame(d=MyEdges,vertices=MyNodes,directed=TRUE)
# Make a palette of colors
library(RColorBrewer)
coul <- brewer.pal(9, "Set1")
# Create a vector of color
my_color <- coul[as.numeric(as.factor(V(MyNetwork)$group))]
set.seed(1234)
plot(MyNetwork,
layout = layout.graphopt,
vertex.color = my_color,
)
# Add a legend
legend("bottomleft",
legend=levels(as.factor(V(MyNetwork)$group)),
col = coul ,
bty = "n",
pch= 20 ,
pt.cex = 3,
cex = 1.5,
text.col=coul ,
horiz = FALSE,
inset = c(0.1, 0.1)
)
效果很好。
但是一旦我进行了聚类网络分析并且我想显示组 nodes/vertices 的颜色显示不正确。
# Plotting data
MyNetwork_cluster <- cluster_optimal(MyNetwork) #* Loading the data
set.seed(1234)
plot(MyNetwork_cluster, #* clustered network data
MyNetwork,
layout = layout.graphopt, # better layout options
vertex.color = my_color, # ?????????????????????????????
)
# Add a legend
legend("bottomleft",
legend=levels(as.factor(V(MyNetwork)$group)),
col = coul ,
bty = "n",
pch= 20 ,
pt.cex = 3,
cex = 1.5,
text.col=coul ,
horiz = FALSE,
inset = c(0.1, 0.1)
)
我的问题是“如何保留集群网络顶点的颜色模型?”
顶点颜色的选项称为col
然后:
# Plotting data
MyNetwork_cluster <- cluster_optimal(MyNetwork) #* Loading the data
set.seed(1234)
plot(MyNetwork_cluster, #* clustered network data
MyNetwork,
layout = layout.graphopt, # better layout options
col = my_color, # !!!!!!!!!!!!!!!!
)
# Add a legend
legend("bottomleft",
legend=levels(as.factor(V(MyNetwork)$group)),
col = coul ,
bty = "n",
pch= 20 ,
pt.cex = 3,
cex = 1.5,
text.col=coul ,
horiz = FALSE,
inset = c(0.1, 0.1)
)