I want to plot my graph. I provided a sample data set. I used simplify to remove loop, but it completely affect the structure of data. When I plot the data without using simplify, I have the correct colouring of vertex and edges but there is loop.
I used simplify to remove the loop and the colouring after it, is wrong, because each node and edge from it should have the same colour as I defined in the code.
does anyone know how to remove loop in plot to not affect the structure of data?
plot 1 : correct colouring but with loop
plot 2: wrong colouring after using simplify
The code:
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)
Typically it's easier to manipulate data in the data frame than in igraph attributes. I would suggest to prepare everything in the data frame before converting it to a graph. Then
simplify
will do it's job as it's supposed to do, and you can plot or analyse your graph as you wish. To preserve the edge attributes insimplify
ifremove.multiple
isTRUE
, you need to define theedge.attr.comb
parameter. Below I useddplyr::first
, meaning that we pick the first value at combining multiple edges.Edit: using OP data & preserving edge attributes in
simplify