-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathsd1.R
221 lines (162 loc) · 7.92 KB
/
sd1.R
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
#################################################
##### SUPPLEMENTARY DATA 1 FROM MELLO ET AL. 2019
#################################################
##### Ecological Synthesis Lab (SintECO): https://marcomellolab.wordpress.com
##### Authors: Marco A. R. Mello, Gabriel M. Felix, Rafael B. P. Pinheiro, Renata L. Muylaert, Cullen Geiselman, Sharlene E. Santana, Marco Tschapka, Nastaran Lotfi, Francisco A. Rodrigues & Richard D. Stevens
##### E-mail: [email protected]
##### How to cite: Mello et al. 2019. Assembly rules of a continent-wide multilayer network. Published as a preprint in bioRxiv in 2018, accepted by Nature Ecology and Evolution in 2019. Supplementary Data 1.
##### Updated on August 5th, 2019 (English version).
##### Run in R version 3.6.0 (2019-04-26) -- "Planting of a Tree"
##### Disclaimer: You may use this script freely for non-comercial purposes at your own risk. We assume no responsibility or liability for the use of this software, convey no license or title under any patent, copyright, or mask work right to the product. We reserve the right to make changes in the software without notification. We also make no representation or warranty that such application will be suitable for the specified use without further testing or modification. If this script helps you produce any academic work (paper, book, chapter, dissertation etc.), please acknowledge the authors and cite the source.
##############################################
##### Set the working directory
##############################################
setwd(dirname(rstudioapi::getActiveDocumentContext()$path))
##############################################
##### Load the packages
##############################################
library("igraph")
library("bipartite")
library("reshape2")
library("gdata")
library("Matrix")
library("paco")
library("ggplot2")
library("tcltk")
library("corrgram")
library("glm2")
library("lme4")
library("gridExtra")
library("dplyr")
##############################################
##### Create multilayer network for igraph
##############################################
#Create nodes and edges
nodes <- read.csv("nodesc1.csv", header=T, as.is=T)
links <- read.csv("linksc1.csv", header=T, as.is=T)
#Inspect objects
class(nodes)
head(nodes)
class(links)
head(links)
#Identify and label dual links
multi_index=which(duplicated(links[,1:2], fromLast = T))
links[c(multi_index),][,4]="dual"
to_remove=which(duplicated(links[,1:2]))
links=links[-to_remove,]
links = arrange(links,type)
unique(links$type)
#Create multilayer network
multilayer <- graph_from_data_frame(d=links, vertices=nodes, directed=F)
#Add information on two-mode strucutre
V(multilayer)$type <- V(multilayer)$name %in% links[,1]
V(multilayer)$type = nodes[,2]
#Inspect multilayer network
class(multilayer)
multilayer
E(multilayer)$weight
E(multilayer)$type
V(multilayer)$name
V(multilayer)$taxon
#Import module membership from DIRT_LPA+
#You should run this analysis using the package biparite for R.
#Then you need to extract module membership and save it as
#tab-delimited TXT file.
modules=read.table("partitions.txt", h=T)
head(modules)
length(modules$nodes)
length(V(multilayer)$name)
#######################################################################################
###### Draw the graphs
#######################################################################################
#Set the same layout for all graphs
l <- layout_nicely(multilayer)
#Set node shapes for all vertices
V(multilayer)$shape = V(multilayer)$taxon
V(multilayer)$shape = gsub("Bats","square",V(multilayer)$shape)
V(multilayer)$shape = gsub("Plants","circle",V(multilayer)$shape)
#######################################################################################
###### Draw the graphs
#######################################################################################
##### Draw the graph with node colors by taxon #####
V(multilayer)$color = V(multilayer)$taxon
V(multilayer)$color = gsub("Bats","#D3802B",V(multilayer)$color)
V(multilayer)$color = gsub("Plants","#2C8437",V(multilayer)$color)
E(multilayer)$color = E(multilayer)$type
E(multilayer)$color = gsub("dual","#A151A1",E(multilayer)$color)
E(multilayer)$color = gsub("frugivory","#B3DCF9",E(multilayer)$color)
E(multilayer)$color = gsub("nectarivory","#FFD26B",E(multilayer)$color)
E(multilayer)$arrow.mode = 0
E(multilayer)$width = E(multilayer)$type
E(multilayer)$width = gsub("dual", 6, E(multilayer)$width)
E(multilayer)$width = gsub("frugivory", 1, E(multilayer)$width)
E(multilayer)$width = gsub("nectarivory", 1, E(multilayer)$width)
png(filename= "multilayer.bip.png", res= 300, height= 3000, width= 4900)
par(mfrow=c(1,1),mar=c(1,1,3,17))
plot(multilayer,
vertex.color = V(multilayer)$color,
vertex.frame.color= V(multilayer)$color,
vertex.shape = V(multilayer)$shape,
vertex.size=3,
vertex.label=V(multilayer)$name,
vertex.label.color="white",
vertex.label.cex=.1,
edge.color = E(multilayer)$color,
edge.curved=0.3,
layout=l)
legend(x = 1.3, y = 0.9, title="Taxon",
legend = c("Bats", "Plants"), pch = c(15,16),
text.col = "gray20", title.col = "black",
box.lwd = 0, cex = 2, col=c("#D3802B", "#2C8437"))
legend(x = 1.3,y = 0.1, title="Layers (interaction types)",
legend = c("frugivory", "nectarivory", "dual"),
fill = c("#B3DCF9", "#FFD26B", "#A151A1"), border = "white",
text.col = "gray20", title.col = "black",
box.lwd = 0, cex = 2, bty="n")
title(main = "Bat-plant multilayer network", cex.main=2)
par(mfrow=c(1,1))
dev.off()
##### Draw the graph with node colors by module #####
#First, you need to analyze the modularity of the network using the package bipartite, and then extract infromation on module membership, and then saving the membership list as a tab-delimited TXT file, whcih should be imported before as the object "modules".
vertex_name_order = data.frame(nodes=V(multilayer)$name)
modules_order = merge(vertex_name_order, modules, by="nodes", sort = F)
#colrs = rainbow(12, alpha=1) #rainbow palette with 12 colors
colrs = c("#A6CEE3", "#1F78B4", "#B2DF8A", "#33A02C", "#FB9A99", "#B41572", "#FDBF6F", "#FF7F00", "#CAB2D6", "#6A3D9A", "#FFFF99", "#B15928") #colorblind-friendly palette with 12 colors.
V(multilayer)$color <- colrs[modules_order$modules]
E(multilayer)$color = E(multilayer)$type
E(multilayer)$color = gsub("dual","#A151A1",E(multilayer)$color)
E(multilayer)$color = gsub("frugivory","#B3DCF9", E(multilayer)$color)
E(multilayer)$color = gsub("nectarivory","#FFD26B", E(multilayer)$color)
E(multilayer)$arrow.mode = 0
E(multilayer)$width = E(multilayer)$type
E(multilayer)$width = gsub("dual", 6, E(multilayer)$width)
E(multilayer)$width = gsub("frugivory", 1, E(multilayer)$width)
E(multilayer)$width = gsub("nectarivory", 1, E(multilayer)$width)
png(filename= "multilayer.mod.png", res= 300, height= 3000, width= 4900)
par(mfrow=c(1,1),mar=c(1,1,3,17))
plot(multilayer,
vertex.color = V(multilayer)$color,
vertex.frame.color= V(multilayer)$color,
vertex.shape = V(multilayer)$shape,
vertex.size=3,
vertex.label=V(multilayer)$name,
vertex.label.color="white",
vertex.label.cex=.2,
edge.color = E(multilayer)$color,
edge.curved=0.3,
layout=l)
legend(x = 1.3,y = 0.9, title="Taxon",
legend = c("Bats", "Plants"), pch = c(15,16),
text.col = "gray20", title.col = "black",
box.lwd = 0, cex = 2, col=c("grey", "grey"))
legend(x = 1.3,y = 0.1, title="Layers (interaction types)",
legend = c("frugivory", "nectarivory", "dual"),
fill = c("#B3DCF9", "#FFD26B", "#A151A1"), border = "white",
text.col = "gray20", title.col = "black", box.lwd = 0, cex = 2)
legend(x = 1.3,y = -0.6, title="Modules",
legend = c("node colors = modules"),
fill = c("grey"), border = "white",
text.col = "gray20", title.col = "black", box.lwd = 0, cex = 2)
title(main = "Bat-plant multilayer network", cex.main=2)
par(mfrow=c(1,1))
dev.off()