-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathNetwork_MPA_Script.R
More file actions
309 lines (243 loc) · 10.4 KB
/
Network_MPA_Script.R
File metadata and controls
309 lines (243 loc) · 10.4 KB
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
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
####################################################################
##### ####
##### SCRIPT FOR DRAWING AND ANALYZING NETWORKS IN R ####
##### ####
####################################################################
##### Authors: Julian Olaya Restrepo & Marcos Barbeitos
##### E-mail: [email protected]
##### Run in R 3.6.1 (2019-07-05)
rm(list=ls())
library("bipartite")
library("ggplot2")
library("network")
library("png")
library("plyr")
library("reshape2")
library("rstudioapi")
library("permute")
library( "igraph" )
library( "RColorBrewer" )
#############################################
#set working directory to save plots
setwd("~/Banca_evaluadora_Lorena/R_mapas/R_analisis")
#############################################
#####
##### MPA NETWORK
#####
#############################################
library( tidyverse )
##### Read data
nodes.mpa <- read.csv("Nodos_MPAs.csv", header=T, as.is=T)
edges.mpa <- read.csv("EDGES_MPA_revLOrena.csv", header=T, as.is=T)
##### Build graph from dataframe
net_mpa <- graph_from_data_frame( d = edges.mpa, vertices = nodes.mpa, directed = FALSE )
summary(net_mpa)
##### Eliminate nodes that neither answered the questionnare nor were mentioned by other MPAs
vert.isola <- which( degree(net_mpa) == 0)
netMPA <- delete.vertices( net_mpa, vert.isola )
##### Simplify the graph and remove loops
# edge are aggregated by: "sum" (The sum of the attributes is calculated)
#netEquipment <- simplify( netEquipment, remove.multiple = T, remove.loops = T)
# edge.attr.comb=list(intencidade="sum", "ignore"))
# density as one of the proxys for connectivity
edge_density(netMPA,loops = FALSE)
# diameter
diameter(netMPA, directed = F, unconnected = TRUE)
# Weighted Degree
V(netMPA)$weighted_degree = strength(netMPA, v=V(netMPA), mode=c('all'))
# Define colours for nodes based on gobernmental sphere
V(netMPA)$color=V(netMPA)$Country
V(netMPA)$color=gsub("Brazil","green",V(netMPA)$color)
V(netMPA)$color=gsub("Uruguay","deepskyblue",V(netMPA)$color)
V(netMPA)$color=gsub("Argentina","orange",V(netMPA)$color)
# Define colours for edges based on Red
E(netMPA)$color=E(netMPA)$Red
E(netMPA)$color=gsub("Biologico","red",E(netMPA)$color)
E(netMPA)$color=gsub("Gestao","#A6A6A6",E(netMPA)$color)
## Intencity (relevance) used as weight for edge size
w.netMPA <- E(netMPA)$weight / max(E(netMPA)$weight)
## STRENGTH CENTRALITY for node size
MPA.strength <- strength(netMPA,vids = V(netMPA),weights = w.netMPA)
l <- layout_with_fr(netMPA, weights=w.netMPA)
svg("Net_overall.svg")
par(mfrow=c(1,1),las=1,mai=c(0.5,1,.2,0.2))
plot(netMPA,
vertex.size=((5*MPA.strength)/max(MPA.strength))+3,
vertex.label.dist=1,
vertex.label.color="black",
vertex.label.sex=0.4,
edge.curve=0.3,
edge.width=w.netMPA,
edge.arrow.width=0.6,
edge.arrow.size=0.1,
vertex.label=NULL,
layout = l
)
# Close the graphics device
dev.off()
#############################################
#############################################
### HACER GRAFICO POR TYPE BIOLOGICO Y GESTION
#############################################
#############################################
##### selecciono solo los edges del biologico
edges.bco <- edges.mpa[edges.mpa$Red == 'Biologico', ]
##### Build graph from dataframe
net_bco <- graph_from_data_frame( d = edges.bco, vertices = nodes.mpa, directed = FALSE )
summary(net_bco)
##### Eliminate nodes that neither answered the questionnare nor were mentioned by other MPAs
vert.isola <- which( degree(net_bco) == 0)
netBCO <- delete.vertices( net_bco, vert.isola )
##### Simplify the graph and remove loops
# edge are aggregated by: "sum" (The sum of the attributes is calculated)
#netEquipment <- simplify( netEquipment, remove.multiple = T, remove.loops = T)
# edge.attr.comb=list(intencidade="sum", "ignore"))
# density as one of the proxys for connectivity
edge_density(netBCO,loops = FALSE)
# diameter
diameter(netBCO, directed = F, unconnected = TRUE)
# Define colours for nodes based on gobernmental sphere
V(netBCO)$color=V(netBCO)$Country
V(netBCO)$color=gsub("Brazil","green",V(netBCO)$color)
V(netBCO)$color=gsub("Uruguay","deepskyblue",V(netBCO)$color)
V(netBCO)$color=gsub("Argentina","orange",V(netBCO)$color)
## Intencity (relevance) used as weight
w.netBCO <- E(netBCO)$weight / max(E(netBCO)$weight)
## STRENGTH CENTRALITY
netBCO.strength <- strength(netBCO,vids = V(netBCO),weights = w.netBCO)
l.bco <- layout_with_fr(netBCO, weights=w.netBCO)
par(mfrow=c(1,1),las=1,mai=c(0.5,1,.2,0.2))
plot(netBCO,
vertex.size=(5*netBCO.strength)/max(netBCO.strength)+3,
vertex.label.color="black",
vertex.label.sex=0.4,
edge.curve=0.3,
vertex.label.cex=0.9,
edge.width=w.netBCO,
edge.color="black",
edge.arrow.width=0.6,
edge.arrow.size=0.1,
vertex.label=NULL,
layout = l.bco)
#############################################
##### selecciono solo los edges del biologico BALLENA
edges.Ea <- edges.bco[edges.bco$Specie == 'Ea', ]
##### Build graph from dataframe
net_Ea <- graph_from_data_frame( d = edges.Ea, vertices = nodes.mpa, directed = FALSE )
summary(net_Ea)
##### Eliminate nodes that neither answered the questionnare nor were mentioned by other MPAs
vert.isola <- which( degree(net_Ea) == 0)
netEa <- delete.vertices( net_Ea, vert.isola )
##### Simplify the graph and remove loops
# edge are aggregated by: "sum" (The sum of the attributes is calculated)
#netEquipment <- simplify( netEquipment, remove.multiple = T, remove.loops = T)
# edge.attr.comb=list(intencidade="sum", "ignore"))
# density as one of the proxys for connectivity
edge_density(netEa,loops = FALSE)
# diameter
diameter(netEa, directed = F, unconnected = TRUE)
# Define colours for nodes based on gobernmental sphere
V(netEa)$color=V(netEa)$Country
V(netEa)$color=gsub("Brazil","green",V(netEa)$color)
V(netEa)$color=gsub("Uruguay","deepskyblue",V(netEa)$color)
V(netEa)$color=gsub("Argentina","orange",V(netEa)$color)
## Intencity (relevance) used as weight
w.netEa <- E(netEa)$weight / max(E(netEa)$weight)
## STRENGTH CENTRALITY
netEa.strength <- strength(netEa,vids = V(netEa),weights = w.netEa)
l.Ea <- layout_with_fr(netEa, weights=w.netEa)
svg("Net_Ea.svg")
par(mfrow=c(1,1),las=1,mai=c(0.5,1,.2,0.2))
plot(netEa,
vertex.size=degree(netEa)+1,
vertex.label.color="black",
vertex.label.sex=0.4,
edge.curve=0.3,
vertex.label.cex=0.9,
edge.width=w.netEa,
edge.color="black",
edge.arrow.width=0.6,
edge.arrow.size=0.1,
vertex.label=NULL,
layout = l.Ea)
dev.off()
#############################################
##### selecciono solo los edges del biologico LEON MARINO
edges.Of <- edges.bco[edges.bco$Specie == 'Of', ]
##### Build graph from dataframe
net_Of <- graph_from_data_frame( d = edges.Of, vertices = nodes.mpa, directed = FALSE )
summary(net_Of)
##### Eliminate nodes that neither answered the questionnare nor were mentioned by other MPAs
vert.isola <- which( degree(net_Of) == 0)
netOf <- delete.vertices( net_Of, vert.isola )
##### Simplify the graph and remove loops
# edge are aggregated by: "sum" (The sum of the attributes is calculated)
#netEquipment <- simplify( netEquipment, remove.multiple = T, remove.loops = T)
# edge.attr.comb=list(intencidade="sum", "ignore"))
# density as one of the proxys for connectivity
edge_density(netOf,loops = FALSE)
# diameter
diameter(netOf, directed = F, unconnected = TRUE)
# Define colours for nodes based on gobernmental sphere
V(netOf)$color=V(netOf)$Country
V(netOf)$color=gsub("Brazil","green",V(netOf)$color)
V(netOf)$color=gsub("Uruguay","deepskyblue",V(netOf)$color)
V(netOf)$color=gsub("Argentina","orange",V(netOf)$color)
## Intencity (relevance) used as weight
w.netOf <- E(netOf)$weight / max(E(netOf)$weight)
## STRENGTH CENTRALITY
netOf.strength <- strength(netOf,vids = V(netOf),weights = w.netOf)
l.Of <- layout_with_fr(netOf, weights=w.netOf)
par(mfrow=c(1,1),las=1,mai=c(0.5,1,.2,0.2))
plot(netOf,
vertex.size=degree(netOf)+2,
vertex.label.color="black",
vertex.label.sex=0.4,
edge.curve=0.3,
vertex.label.cex=0.9,
edge.width=w.netOf,
edge.color="black",
edge.arrow.width=0.6,
edge.arrow.size=0.1,
vertex.label=NULL,
layout=l.Of)
#############################################
##### selecciono solo los edges del gestao
edges.gst <- edges.mpa[edges.mpa$Red == 'Gestao', ]
##### Build graph from dataframe
net_gst <- graph_from_data_frame( d = edges.gst, vertices = nodes.mpa, directed = FALSE )
summary(net_gst)
##### Eliminate nodes that neither answered the questionnare nor were mentioned by other MPAs
vert.isola <- which( degree(net_gst) == 0)
netGST <- delete.vertices( net_gst, vert.isola )
##### Simplify the graph and remove loops
# edge are aggregated by: "sum" (The sum of the attributes is calculated)
#netEquipment <- simplify( netEquipment, remove.multiple = T, remove.loops = T)
# edge.attr.comb=list(intencidade="sum", "ignore"))
# density as one of the proxys for connectivity
edge_density(netGST,loops = FALSE)
# diameter
diameter(netGST, directed = F, unconnected = TRUE)
# Define colours for nodes based on gobernmental sphere
V(netGST)$color=V(netGST)$Country
V(netGST)$color=gsub("Brazil","green",V(netGST)$color)
V(netGST)$color=gsub("Uruguay","deepskyblue",V(netGST)$color)
V(netGST)$color=gsub("Argentina","orange",V(netGST)$color)
## Intencity (relevance) used as weight
w.netGST <- E(netGST)$weight / max(E(netGST)$weight)
## STRENGTH CENTRALITY
netGST.strength <- strength(netGST,vids = V(netGST),weights = w.netGST)
l.GST <- layout_with_fr(netGST, weights=w.netGST)
par(mfrow=c(1,1),las=1,mai=c(0.5,1,.2,0.2))
plot(netGST,
vertex.size=degree(netGST)/max(degree(netGST))+6,
vertex.label.color="black",
vertex.label.sex=0.4,
edge.curve=0.3,
vertex.label.cex=0.9,
edge.width=w.netGST,
edge.color="black",
edge.arrow.width=0.6,
edge.arrow.size=0.1,
vertex.label=NULL,
layout=l.GST)