我想绘制我的图表。我提供了一个样本数据集。我用simplemented来去除循环,但它完全影响了数据的结构。当我在不使用简化的情况下绘制数据时,我有正确的顶点和边着色,但有循环。
我使用简化来删除循环和它之后的颜色,这是错误的,因为它的每个节点和边缘都应该具有与我在代码中定义的颜色相同的颜色。
有谁知道如何删除情节中的循环而不影响数据的结构?
图 1:正确着色但有循环
图 2:使用简化后的错误着色
编码:
X user.screen_name child parent in_reply_to_screen_name vaccine_type label
1 0 TweeetLorraine 1.392218e+18 1.392218e+18 1 AstraZeneca 0
2 1 phldenault 1.393259e+18 1.392218e+18 TweeetLorraine AstraZeneca 2
41 40 ElizabethDuncan 1.392297e+18 1.392218e+18 TweeetLorraine AstraZeneca 2
42 41 7Rose75 1.392294e+18 1.392218e+18 TweeetLorraine AstraZeneca 1
43 42 wh0careswh0 1.392336e+18 1.392294e+18 7Rose75 AstraZeneca 0
44 43 T_ProudVeteran 1.392330e+18 1.392294e+18 7Rose75 AstraZeneca 2
45 44 TweeetLorraine 1.392294e+18 1.392294e+18 7Rose75 AstraZeneca 2
46 45 Norlaine 1.392288e+18 1.392218e+18 TweeetLorraine AstraZeneca 2
47 46 elham95264575 1.393212e+18 1.392288e+18 Norlaine AstraZeneca 1
48 47 soyfreemike 1.392387e+18 1.392288e+18 Norlaine AstraZeneca 0
49 48 KMTCarr 1.392288e+18 1.392218e+18 TweeetLorraine AstraZeneca 2
50 49 angela_petta 1.392283e+18 1.392218e+18 TweeetLorraine AstraZeneca 2
51 50 lhoneyimhome 1.392272e+18 1.392218e+18 TweeetLorraine AstraZeneca 2
net1 <- graph_from_data_frame(df %>% select("child","parent"))
rel = get.adjacency(graph, sparse = FALSE)
graph = simplify(net1, remove.loops=TRUE)
graph
summary(graph)
vertex_attr(graph, "label") <- df$label
#Set edge attribute:
edge_attr(graph, "label") <- df$label
E(graph)$color[E(graph)$label == 2] <- '#B3DE69' #green
E(graph)$color[E(graph)$label == 1] <- '#80B1D3' #yellow
E(graph)$color[E(graph)$label == 0] <- '#FB8072'#purple
V(graph)$color[V(graph)$label == 2] <- '#B3DE69'
V(graph)$color[V(graph)$label == 1] <- '#80B1D3'
V(graph)$color[V(graph)$label == 0] <- '#FB8072'
g<-c('#B3DE69','#80B1D3','#FB8072')
plot(graph,layout=layout.fruchterman.reingold,
vertex.frame.color=NA,vertex.label.color="black",
edge.label = NA,
vertex.size=3, usecurve=TRUE,
edge.lwd=0.02,
vertex.dist=10,vertex.label.dist=2,vertex.label.cex=0.9,
pad=0.9,alpha=80,
edge.arrow.size=.1)
legend("bottomleft",legend= c("Positive","Neutral","Negative"),
col=g,pch=19,pt.cex=1.5,bty="n",
title="Label category")
title(main="Visualization ", cex.main=1)