I am trying to make a chord diagram, where vertex are grouped by a factor (modules) and edges are colored by other factor.
I really like the layout achieved when using the edgebundle()
and I tried to follow all the directions from here:
(He describes a "hacking" method to color the edges, but I can't manages to do that...) So, I cannot find a way to implement these two coloring rules in any easy way.
So I am thinking of now abandon the edgebundle()
and start over using a function like chordDiagram()
- But I really like the layout better in the first function (plus I spend soo much time already on this)
Maybe someone can help me and I can stick with the edgebundle()
.
Here is my steps - It is not pretty, I apologize, I am not very skilled in R, so I do things is stupid ways often.
The consensus_taxa
is a data frame of the edges, where the labels for taxa1 and taxa2 includes a prefix of the module number it belongs to, like this:
taxa1 taxa2 rho
1 3.ITS_Uncl.Mortierella_OTU985144 3.ITS_Uncl.Ascomycota_OTU258690 -0.9725180
2 1.ITS_Uncl.Mortierella_OTU979 1.ITS_Uncl.Mortierella_OTU2523736 0.9629500
3 3.ITS_Uncl.Pleosporales_OTU136349 3.ITS_Uncl.Basidiomycota_OTU258716 0.9615385
And the m_meta
is meta data, such as additional labels and again the module information:
m_meta[1:3,1:7]
comb taxa1 trt rho module
1 1.Bact_Uncl.Proteobacteria_OTU248 Bact_Uncl.Proteobacteria_OTU248 meadow -0.5000000 1
2 1.Bact_Uncl.Bacteroidetes_OTU695 Bact_Uncl.Bacteroidetes_OTU695 meadow -0.8434475 1
3 1.ITS_Uncl.Mortierella_OTU2523736 ITS_Uncl.Mortierella_OTU2523736 meadow 0.9629500 1
ID Org
1 Uncl.Proteobacteria Bact
2 Uncl.Bacteroidetes Bact
3 Uncl.Mortierella ITS
So here is my attempt:
g_my <- graph.data.frame(consensus_taxa, directed=F, vertices=m_meta)
mod_col <- as.factor(as.character(V(g_my)$module))
# generation random colors
cl <- colors(distinct = TRUE)
set.seed(15887) # to set random generator seed
mycols2 <- sample(cl, length(levels(mod_col)))
levels(mod_col) <- as.character(mycols2)
V(g_my)$color <- as.character(mod_col)
# Edge color green when positiv (rho >0) and red when negativ
edge_col <- matrix(nrow=0,ncol=1)
for (i in 1:length(consensus_taxa$rho)){
t <- consensus_taxa$rho[i]
ecol <- if(t>0) {as.character("green")} else {as.character("red")}
edge_col <- rbind(edge_col, ecol)
print(i/length(consensus_taxa$rho)) }
E(g_my)$color <- as.character(edge_col)
v_lab <- as.character(m_meta$ID)
#### Loosing sorting of the vertex in the circle (and in the general the layout is fare from nice)
plot(g_my, layout = layout.circle, vertex.size=degree(g_my)*0.5, vertex.label = v_lab)
#### loosing color codes and labels in this way, but layout is in general nice
edgebundle(g_my)->eb
eb
Any suggestions?
Here is how it looks (there is a size issue also in the first one).
[![edgebundle][1]][1]
[![normal_plot][2]][2]
[![no_labels][3]][3]