/
README.html
465 lines (371 loc) · 25.1 KB
/
README.html
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
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
<!DOCTYPE html
PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html><head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8">
<!--
This HTML was auto-generated from MATLAB code.
To make changes, update the MATLAB code and republish this document.
--><title>Interactive food web diagrams for Ecopath</title><meta name="generator" content="MATLAB 9.1"><link rel="schema.DC" href="http://purl.org/dc/elements/1.1/"><meta name="DC.date" content="2017-02-15"><meta name="DC.source" content="./readmeExtras/README.m"><style type="text/css">
html,body,div,span,applet,object,iframe,h1,h2,h3,h4,h5,h6,p,blockquote,pre,a,abbr,acronym,address,big,cite,code,del,dfn,em,font,img,ins,kbd,q,s,samp,small,strike,strong,sub,sup,tt,var,b,u,i,center,dl,dt,dd,ol,ul,li,fieldset,form,label,legend,table,caption,tbody,tfoot,thead,tr,th,td{margin:0;padding:0;border:0;outline:0;font-size:100%;vertical-align:baseline;background:transparent}body{line-height:1}ol,ul{list-style:none}blockquote,q{quotes:none}blockquote:before,blockquote:after,q:before,q:after{content:'';content:none}:focus{outine:0}ins{text-decoration:none}del{text-decoration:line-through}table{border-collapse:collapse;border-spacing:0}
html { min-height:100%; margin-bottom:1px; }
html body { height:100%; margin:0px; font-family:Arial, Helvetica, sans-serif; font-size:10px; color:#000; line-height:140%; background:#fff none; overflow-y:scroll; }
html body td { vertical-align:top; text-align:left; }
h1 { padding:0px; margin:0px 0px 25px; font-family:Arial, Helvetica, sans-serif; font-size:1.5em; color:#d55000; line-height:100%; font-weight:normal; }
h2 { padding:0px; margin:0px 0px 8px; font-family:Arial, Helvetica, sans-serif; font-size:1.2em; color:#000; font-weight:bold; line-height:140%; border-bottom:1px solid #d6d4d4; display:block; }
h3 { padding:0px; margin:0px 0px 5px; font-family:Arial, Helvetica, sans-serif; font-size:1.1em; color:#000; font-weight:bold; line-height:140%; }
a { color:#005fce; text-decoration:none; }
a:hover { color:#005fce; text-decoration:underline; }
a:visited { color:#004aa0; text-decoration:none; }
p { padding:0px; margin:0px 0px 20px; }
img { padding:0px; margin:0px 0px 20px; border:none; }
p img, pre img, tt img, li img, h1 img, h2 img { margin-bottom:0px; }
ul { padding:0px; margin:0px 0px 20px 23px; list-style:square; }
ul li { padding:0px; margin:0px 0px 7px 0px; }
ul li ul { padding:5px 0px 0px; margin:0px 0px 7px 23px; }
ul li ol li { list-style:decimal; }
ol { padding:0px; margin:0px 0px 20px 0px; list-style:decimal; }
ol li { padding:0px; margin:0px 0px 7px 23px; list-style-type:decimal; }
ol li ol { padding:5px 0px 0px; margin:0px 0px 7px 0px; }
ol li ol li { list-style-type:lower-alpha; }
ol li ul { padding-top:7px; }
ol li ul li { list-style:square; }
.content { font-size:1.2em; line-height:140%; padding: 20px; }
pre, code { font-size:12px; }
tt { font-size: 1.2em; }
pre { margin:0px 0px 20px; }
pre.codeinput { padding:10px; border:1px solid #d3d3d3; background:#f7f7f7; }
pre.codeoutput { padding:10px 11px; margin:0px 0px 20px; color:#4c4c4c; }
pre.error { color:red; }
@media print { pre.codeinput, pre.codeoutput { word-wrap:break-word; width:100%; } }
span.keyword { color:#0000FF }
span.comment { color:#228B22 }
span.string { color:#A020F0 }
span.untermstring { color:#B20000 }
span.syscmd { color:#B28C00 }
.footer { width:auto; padding:10px 0px; margin:25px 0px 0px; border-top:1px dotted #878787; font-size:0.8em; line-height:140%; font-style:italic; color:#878787; text-align:left; float:none; }
.footer p { margin:0px; }
.footer a { color:#878787; }
.footer a:hover { color:#878787; text-decoration:underline; }
.footer a:visited { color:#878787; }
table th { padding:7px 5px; text-align:left; vertical-align:middle; border: 1px solid #d6d4d4; font-weight:bold; }
table td { padding:7px 5px; text-align:left; vertical-align:top; border:1px solid #d6d4d4; }
</style></head><body><div class="content"><h1>Interactive food web diagrams for Ecopath</h1><!--introduction--><p>Author: Kelly Kearney</p><p>This repository includes the Matlab code to perform the trophic group calculations, JSON file formatting, and divided edge bundling calculations necessary to plot food webs with the <a href="https://github.com/kakearney/d3-foodweb">d3-foodweb</a> D3 plugin.</p><p>The d3-foodweb plugin, and this accompanying code, is currently a work in progress. It is being developed alongside the writing of a manuscript describing the underlying algorithms. Documentation and examples will improve as this manuscript progresses.</p><p>This code was written to work with food web models designed for <a href="http://ecopath.org/">Ecopath</a>, a software suite commonly used in fisheries ecosystem modeling. To import an Ecopath model from either Ecopath with Ecosim, Rpath, or EwE-F into Matlab, you will need the <a href="https://github.com/kakearney/ecopath_matlab-pkg">ecopath_matlab</a> toolbox.</p><!--/introduction--><h2>Contents</h2><div><ul><li><a href="#1">Getting started</a></li><li><a href="#2">Syntax</a></li><li><a href="#3">Examples</a></li><li><a href="#14">Contributions</a></li></ul></div><h2 id="1">Getting started</h2><p><b>Prerequisites</b></p><p>This function requires Matlab R2014b or later.</p><p><b>Downloading and installation</b></p><p>This code can be downloaded from <a href="https://github.com/kakearney/foodwebgraph-pkg/">Github</a>.</p><p><b>Matlab Search Path</b></p><p>The following folders need to be added to your Matlab Search path (via <tt>addpath</tt>, <tt>pathtool</tt>, etc.):</p><pre class="language-matlab">foodwebgraph-pkg/InterPointDistanceMatrix
foodwebgraph-pkg/aggregate
foodwebgraph-pkg/catuneven
foodwebgraph-pkg/divedgebundle
foodwebgraph-pkg/foodwebgraph
foodwebgraph-pkg/interparc
foodwebgraph-pkg/jsonlab-1.0
foodwebgraph-pkg/mergestruct
foodwebgraph-pkg/minmax
foodwebgraph-pkg/singlepatch
foodwebgraph-pkg/vlookup
</pre><h2 id="2">Syntax</h2><p>This package includes a number of Matlab functions. The primary ones of interest are:</p><div><ul><li><tt>trophicgroup.m</tt>: Cluster food web nodes based on trophic similarity</li><li><tt>fwgraph2json.m</tt>: Export graph to JSON format for use with d3-foodweb</li><li><tt>foodweblayout.m</tt>: Interactive food web layout tool</li><li><tt>plotfoodweb.m</tt>: Plot food web in Matlab</li></ul></div><p>Please see the help headers (viewable via the <tt>help</tt> command from the Matlab Command Window) for each function for a description of calling syntax, input variables, and output variables.</p><h2 id="3">Examples</h2><p>This example will demonstrate the entire food web graph process, using the Generic_37 Ecopath model (this model is included with the <a href="http://ecopath.org/">Ecopath with Ecosim)</a> software, and represents a generic pelagic fisheries ecosystem).</p><p><b>Step 1:</b> Read an Ecopath model into Matlab. This step requires the <a href="https://github.com/kakearney/ecopath_matlab-pkg">ecopath_matlab</a> code to run.</p><pre class="codeinput">epfile = <span class="string">'examples/Generic_37.EwEmdb'</span>;
w = warning(<span class="string">'off'</span>); <span class="comment">% turning off warnings</span>
EM = mdb2ecopathmodel(epfile); <span class="comment">% since this spits out a lot...</span>
warning(w); <span class="comment">% ...and back on</span>
</pre><p><b>Step 2:</b> Isolate trophic groups in the food web</p><p>The simulated annealing algorithm does a pretty good job optimizing the trophic groups, but its inherent randomness means it doesn't always find the global optimum, especially when dealing with food webs, like this one, whose functional groups are already grouped at a high level (as opposed to webs with lots of individual species groups).</p><p>You can run the algorithm multiple times in order to try to ensure a "best" grouping. In this example, I'll simply seed the random number generator to ensure replicability; this is only for example purposes, and a one-time optimized result is sufficient.</p><pre class="codeinput">rng(8);
[tg, Stmp, G] = trophicgroup(EM, <span class="string">'method'</span>, <span class="string">'simulatedannealing'</span>, <span class="keyword">...</span>
<span class="string">'verbose'</span>, false);
</pre><p><b>Step 3:</b> Sort trophic groups.</p><p>The layout tool used in the next step assigns an initial vertical (y-axis) position to each node based on its trophic level. The horizontal dimension doesn't have an equivalent restriction, so for initialization the nodes are simply placed left to right according to their trophic group number. The <tt>trophicgroup</tt> algorithm assigns numbers to the groups it finds randomly, so this means the initial horizontal position of each group of nodes is random. If we instead want initial placement to reflect some characteristic of the food web, we can sort these numbers based on that characteristic.</p><pre class="codeinput"><span class="comment">% Sort based on fraction of input originating from detritus</span>
detfrac = sourcefraction(EM.graph(<span class="string">'oos'</span>, false, <span class="string">'det'</span>, false), {<span class="string">'Detritus'</span>});
dfracavg = accumarray(tg, detfrac, [max(tg) 1], @mean);
[~,isrt] = sort(dfracavg);
[~, tgsorted] = ismember(tg, isrt);
</pre><p><b>Step 4:</b> Save graph to JSON format</p><p>This step isn't necessary if you're planning to do all your plotting from within Matlab, using the foodweblayout function. However, if you plan to do anything with this food web outside of Matlab (e.g. add a plot to a webpage), you will want to save graph to a JSON file. At this stage in the process, the data will include all properties necessary to run d3.foodweblayout.</p><pre class="codeinput">fwgraph2json(G, tgsorted, fullfile(<span class="string">'examples'</span>, <span class="string">'gen37.json'</span>));
</pre><p><b>Step 5:</b> Run the layout tool</p><p>Now it's time to set the positions for the nodes. The <tt>foodweblayout</tt> function provides a wrapper around d3.foodweblayout, launching an web page in the Matlab browser that lets you play around with the various parameters and make manual adjustments to node position. When you're done, click the checkbox in the lower left; this will close the browser and the function will retrieve the information from the visualization (node location, node radius, text label location) and add it to the Node table in the returned graph object.</p><pre class="language-matlab">[Gpos, Ax, P] = foodweblayout(G, tgsorted);
</pre><p>Here are the parameters I chose for this food web:</p><pre class="codeinput">P
</pre><pre class="codeoutput">
P =
struct with fields:
marl: 50
marr: 10
mart: 10
marb: 60
totwidth: 600
totheight: 600
nodepad: 10
strengthflxlink: 0.001
strengthgrplink: 0.1
strengthtrophic: 1.2
strengthxcenter: 0.03
tlmin: 1
tlmax: 5
rmin: 1
rmax: 50
bmin: NaN
bmax: NaN
</pre><p>At this point, we can plot the food web using straight line edges. We can do this in Matlab, after passing the graph through the pixel-to-trophic level converter:</p><pre class="codeinput">[GposTL, AxTL] = pixels2trophic(Gpos, Ax, [P.tlmin P.tlmax]);
<span class="comment">% Plot</span>
h = plotfoodweb(GposTL, AxTL, <span class="string">'p'</span>, 1/3, <span class="string">'w'</span>, 50, <span class="string">'rloop'</span>, 40, <span class="keyword">...</span>
<span class="string">'edgecolor'</span>, <span class="string">'byValue'</span>, <span class="string">'initial'</span>, true, <span class="string">'gmax'</span>, 500);
<span class="comment">% Color edges by log of weight</span>
h.edg.CData = log10(h.edg.CData);
set(h.ax, <span class="string">'clim'</span>, [-5 4]);
colormap(flipud(parula));
<span class="comment">% Add a colorbar for reference</span>
h.cb = colorbar(<span class="string">'north'</span>);
setpos(h.cb, <span class="string">'# 0.93 0.25 0.02'</span>);
title(h.cb, <span class="string">'log_{10} Biomass flux (t ww/km^2/yr)'</span>);
set(h.cb, <span class="string">'fontsize'</span>, 8);
<span class="comment">% Outline nodes in white, for clarity</span>
set(h.nd, <span class="string">'edgecolor'</span>, <span class="string">'w'</span>);
</pre><img vspace="5" hspace="5" src="./readmeExtras/README_01.png" alt=""> <p>We could also save the graph data to a JSON file, and create an interactive web plot using d3.foodwebstatic.</p><pre class="codeinput">fwgraph2json(Gpos, tgsorted, fullfile(<span class="string">'examples'</span>, <span class="string">'gen37_postlayout.json'</span>));
</pre><p><b>Step 6:</b> Edge bundling</p><p>The edge bundling algorithm calculates new bundled pathways for each edge. This process highlights the major pathways of biomass through the food web, and can often make a cluttered spaghetti diagram much more informative.</p><p>For most food webs, you'll want to apply some sort of scaling to the edge weight values. I find that a logarithmic transform works well for most food webs. Keep in mind that the final edge weights returned by the function must remain positive.</p><pre class="codeinput"><span class="comment">% Scale edges</span>
wlim = minmax(log10(Gpos.Edges.Weight), <span class="string">'expand'</span>, 0.01);
wtfun = @(x) log10(x) - wlim(1);
<span class="comment">% Bundle edges</span>
Gbdl = debundle(Gpos, <span class="string">'edgefun'</span>, wtfun, <span class="string">'l'</span>, 100);
</pre><pre class="codeoutput">Preprocessing...
100% [=================================================>] 176/ 176
Elapsed time is 0.837228 seconds.
Bundling...
Pass 1
100% [=================================================>] Pass 1: 29 of 29
Elapsed time is 0.533837 seconds.
Pass 2
100% [=================================================>] Pass 2: 29 of 29
Elapsed time is 1.523696 seconds.
Pass 3
100% [=================================================>] Pass 3: 29 of 29
Elapsed time is 3.629603 seconds.
Pass 4
100% [=================================================>] Pass 4: 29 of 29
Elapsed time is 7.167741 seconds.
Pass 5
100% [=================================================>] Pass 5: 29 of 29
Elapsed time is 15.045901 seconds.
Postprocessing...
Done
</pre><p>We can save a new version of the JSON file, now containing edge path coordinates.</p><pre class="codeinput">fwgraph2json(Gbdl, tg, fullfile(<span class="string">'examples'</span>, <span class="string">'gen37_postbundle'</span>));
</pre><p>We can also plot as we did in the previous example, now using those path coordinates for the edges.</p><pre class="codeinput"><span class="comment">% Pixel to trophic conversion</span>
GbdlTL = pixels2trophic(Gbdl, Ax, [P.tlmin P.tlmax]);
<span class="comment">% Plot</span>
h = plotfoodweb(GbdlTL, AxTL, <span class="string">'p'</span>, 1/3, <span class="string">'w'</span>, 50, <span class="string">'rloop'</span>, 40, <span class="keyword">...</span>
<span class="string">'edgecolor'</span>, <span class="string">'byValue'</span>, <span class="string">'gmax'</span>, 500, <span class="string">'cthresh'</span>, 0.5);
<span class="comment">% Color edges by log of weight</span>
h.edg.CData = log10(h.edg.CData);
set(h.ax, <span class="string">'clim'</span>, [-5 4]);
colormap(flipud(parula));
<span class="comment">% Add a colorbar for reference</span>
h.cb = colorbar(<span class="string">'north'</span>);
setpos(h.cb, <span class="string">'# 0.93 0.25 0.02'</span>);
title(h.cb, <span class="string">'log_{10} Biomass flux (t ww/km^2/yr)'</span>);
set(h.cb, <span class="string">'fontsize'</span>, 8);
<span class="comment">% Outline nodes in white, for clarity</span>
set(h.nd, <span class="string">'edgecolor'</span>, <span class="string">'w'</span>);
</pre><img vspace="5" hspace="5" src="./readmeExtras/README_02.png" alt=""> <h2 id="14">Contributions</h2><p>Community contributions to this package are welcome!</p><p>To report bugs, please submit <a href="https://github.com/kakearney/foodwebgraph-pkg/issues">an issue</a> on GitHub and include:</p><div><ul><li>your operating system</li><li>your version of Matlab and all relevant toolboxes (type <tt>ver</tt> at the Matlab command line to get this info)</li><li>code/data to reproduce the error or buggy behavior, and the full text of any error messages received</li></ul></div><p>Please also feel free to submit enhancement requests, or to send pull requests (via GitHub) for bug fixes or new features.</p><p>I do monitor the MatlabCentral FileExchange entry for any issues raised in the comments, but would prefer to track issues on GitHub.</p><p class="footer"><br><a href="http://www.mathworks.com/products/matlab/">Published with MATLAB® R2016b</a><br></p></div><!--
##### SOURCE BEGIN #####
%% Interactive food web diagrams for Ecopath
% Author: Kelly Kearney
%
% This repository includes the Matlab code to perform the trophic
% group calculations, JSON file formatting, and divided edge bundling
% calculations necessary to plot food webs with the
% <https://github.com/kakearney/d3-foodweb d3-foodweb> D3 plugin.
%
% The d3-foodweb plugin, and this accompanying code, is currently a work
% in progress. It is being developed alongside the writing of a
% manuscript describing the underlying algorithms. Documentation and
% examples will improve as this manuscript progresses.
%
% This code was written to work with food web models designed for
% <http://ecopath.org/ Ecopath>, a software suite commonly used in
% fisheries ecosystem modeling. To import an Ecopath model from either
% Ecopath with Ecosim, Rpath, or EwE-F into Matlab, you will need the
% <https://github.com/kakearney/ecopath_matlab-pkg ecopath_matlab> toolbox.
%% Getting started
%
% *Prerequisites*
%
% This function requires Matlab R2014b or later.
%
% *Downloading and installation*
%
% This code can be downloaded from <https://github.com/kakearney/foodwebgraph-pkg/ Github>.
%
% *Matlab Search Path*
%
% The following folders need to be added to your Matlab Search path (via
% |addpath|, |pathtool|, etc.):
%
% foodwebgraph-pkg/InterPointDistanceMatrix
% foodwebgraph-pkg/aggregate
% foodwebgraph-pkg/catuneven
% foodwebgraph-pkg/divedgebundle
% foodwebgraph-pkg/foodwebgraph
% foodwebgraph-pkg/interparc
% foodwebgraph-pkg/jsonlab-1.0
% foodwebgraph-pkg/mergestruct
% foodwebgraph-pkg/minmax
% foodwebgraph-pkg/singlepatch
% foodwebgraph-pkg/vlookup
%% Syntax
%
% This package includes a number of Matlab functions. The primary ones of
% interest are:
%
% * |trophicgroup.m|: Cluster food web nodes based on trophic similarity
% * |fwgraph2json.m|: Export graph to JSON format for use with d3-foodweb
% * |foodweblayout.m|: Interactive food web layout tool
% * |plotfoodweb.m|: Plot food web in Matlab
%
% Please see the help headers (viewable via the |help| command from the Matlab
% Command Window) for each function for a description of calling syntax,
% input variables, and output variables.
%% Examples
%
% This example will demonstrate the entire food web graph process, using
% the Generic_37 Ecopath model (this model is included with the
% <http://ecopath.org/ Ecopath with Ecosim)> software, and represents a
% generic pelagic fisheries ecosystem).
%
% *Step 1:* Read an Ecopath model into Matlab. This step requires the
% <https://github.com/kakearney/ecopath_matlab-pkg ecopath_matlab> code to
% run.
epfile = 'examples/Generic_37.EwEmdb';
w = warning('off'); % turning off warnings
EM = mdb2ecopathmodel(epfile); % since this spits out a lot...
warning(w); % ...and back on
%%
% *Step 2:* Isolate trophic groups in the food web
%
% The simulated annealing algorithm does a pretty good job optimizing the
% trophic groups, but its inherent randomness means it doesn't always find
% the global optimum, especially when dealing with food webs, like this
% one, whose functional groups are already grouped at a high level (as
% opposed to webs with lots of individual species groups).
%
% You can run the algorithm multiple times in order to try to ensure a
% "best" grouping. In this example, I'll simply seed the random number
% generator to ensure replicability; this is only for example
% purposes, and a one-time optimized result is sufficient.
rng(8);
[tg, Stmp, G] = trophicgroup(EM, 'method', 'simulatedannealing', ...
'verbose', false);
%%
% *Step 3:* Sort trophic groups.
%
% The layout tool used in the next step
% assigns an initial vertical (y-axis) position to each node based on its
% trophic level. The horizontal dimension doesn't have an equivalent
% restriction, so for initialization the nodes are simply placed left to
% right according to their trophic group number. The |trophicgroup|
% algorithm assigns numbers to the groups it finds randomly, so this means
% the initial horizontal position of each group of nodes is random. If we
% instead want initial placement to reflect some characteristic of the food
% web, we can sort these numbers based on that characteristic.
% Sort based on fraction of input originating from detritus
detfrac = sourcefraction(EM.graph('oos', false, 'det', false), {'Detritus'});
dfracavg = accumarray(tg, detfrac, [max(tg) 1], @mean);
[~,isrt] = sort(dfracavg);
[~, tgsorted] = ismember(tg, isrt);
%%
% *Step 4:* Save graph to JSON format
%
% This step isn't necessary if you're planning to do all your plotting from
% within Matlab, using the foodweblayout function. However, if you plan to
% do anything with this food web outside of Matlab (e.g. add a plot to a
% webpage), you will want to save graph to a JSON file. At this stage in
% the process, the data will include all properties necessary to run
% d3.foodweblayout.
fwgraph2json(G, tgsorted, fullfile('examples', 'gen37.json'));
%%
% *Step 5:* Run the layout tool
%
% Now it's time to set the positions for the nodes. The |foodweblayout| function
% provides a wrapper around d3.foodweblayout, launching an web page in the
% Matlab browser that lets you play around with the various parameters and
% make manual adjustments to node position. When you're done, click the
% checkbox in the lower left; this will close the browser and the function
% will retrieve the information from the visualization (node location, node
% radius, text label location) and add it to the Node table in the
% returned graph object.
%
% [Gpos, Ax, P] = foodweblayout(G, tgsorted);
%%
% Here are the parameters I chose for this food web:
P
%%
% At this point, we can plot the food web using straight line edges. We
% can do this in Matlab, after passing the graph through the
% pixel-to-trophic level converter:
[GposTL, AxTL] = pixels2trophic(Gpos, Ax, [P.tlmin P.tlmax]);
% Plot
h = plotfoodweb(GposTL, AxTL, 'p', 1/3, 'w', 50, 'rloop', 40, ...
'edgecolor', 'byValue', 'initial', true, 'gmax', 500);
% Color edges by log of weight
h.edg.CData = log10(h.edg.CData);
set(h.ax, 'clim', [-5 4]);
colormap(flipud(parula));
% Add a colorbar for reference
h.cb = colorbar('north');
setpos(h.cb, '# 0.93 0.25 0.02');
title(h.cb, 'log_{10} Biomass flux (t ww/km^2/yr)');
set(h.cb, 'fontsize', 8);
% Outline nodes in white, for clarity
set(h.nd, 'edgecolor', 'w');
%%
% We could also save the graph data to a JSON file, and create an
% interactive web plot using d3.foodwebstatic.
fwgraph2json(Gpos, tgsorted, fullfile('examples', 'gen37_postlayout.json'));
%%
% *Step 6:* Edge bundling
%
% The edge bundling algorithm calculates new bundled pathways for each
% edge. This process highlights the major pathways of biomass through the
% food web, and can often make a cluttered spaghetti diagram much more
% informative.
%
% For most food webs, you'll want to apply some sort of scaling to the edge
% weight values. I find that a logarithmic transform works well for most
% food webs. Keep in mind that the final edge weights returned by the
% function must remain positive.
% Scale edges
wlim = minmax(log10(Gpos.Edges.Weight), 'expand', 0.01);
wtfun = @(x) log10(x) - wlim(1);
% Bundle edges
Gbdl = debundle(Gpos, 'edgefun', wtfun, 'l', 100);
%%
% We can save a new version of the JSON file, now containing edge path
% coordinates.
fwgraph2json(Gbdl, tg, fullfile('examples', 'gen37_postbundle'));
%%
% We can also plot as we did in the previous example, now using those path
% coordinates for the edges.
% Pixel to trophic conversion
GbdlTL = pixels2trophic(Gbdl, Ax, [P.tlmin P.tlmax]);
% Plot
h = plotfoodweb(GbdlTL, AxTL, 'p', 1/3, 'w', 50, 'rloop', 40, ...
'edgecolor', 'byValue', 'gmax', 500, 'cthresh', 0.5);
% Color edges by log of weight
h.edg.CData = log10(h.edg.CData);
set(h.ax, 'clim', [-5 4]);
colormap(flipud(parula));
% Add a colorbar for reference
h.cb = colorbar('north');
setpos(h.cb, '# 0.93 0.25 0.02');
title(h.cb, 'log_{10} Biomass flux (t ww/km^2/yr)');
set(h.cb, 'fontsize', 8);
% Outline nodes in white, for clarity
set(h.nd, 'edgecolor', 'w');
%% Contributions
%
% Community contributions to this package are welcome!
%
% To report bugs, please submit
% <https://github.com/kakearney/foodwebgraph-pkg/issues an issue> on GitHub and
% include:
%
% * your operating system
% * your version of Matlab and all relevant toolboxes (type |ver| at the Matlab command line to get this info)
% * code/data to reproduce the error or buggy behavior, and the full text of any error messages received
%
% Please also feel free to submit enhancement requests, or to send pull
% requests (via GitHub) for bug fixes or new features.
%
% I do monitor the MatlabCentral FileExchange entry for any issues raised
% in the comments, but would prefer to track issues on GitHub.
%
##### SOURCE END #####
--></body></html>