The visualization code on this page is featured in "Visualization of communities in networks," Amanda L. Traud, Christina Frost, Peter J. Mucha, and Mason A. Porter, *Chaos* **19**, 041104 (2009).

**Code**

This MATLAB code requires that your adjacency matrix (or matrix that shows the connections between people) is sparse. To create a sparse matrix in MATLAB, take your matrix (suppose it's called "A"), type "sparse(A)". This code also requires that you download the package from this website. If you are using a Mac with 64-bit OS X (version 2009b or later), then instead download the package from this website.

Typing "help" and then the name of each program below gives specific instructions on the inputs and outputs of each program. We also show these here:

Each of the programs below gives xy coordinates as one of the outputs; these can be put into the following program to produce the graphs that are featured in our Nonlinear Science Gallery poster and paper: graphplot2D.m

1. FRKK (Fruchterman-Reingold placement of communities, Kamada-Kawai placement of nodes within each community) FRKK.m

This function uses the Fruchterman-Reingold Algorithm to find the optimal
node placement for a given network with respect to Communities. But then
uses the Kamada-Kawai algorithm to place the nodes within each community.

- Inputs:
- A: the adjacency matrix for the network
- gn: a vector of group numbers, i.e. every node that has the number 1 is in group 1.
- epsilon: the accuracy at which you would like the algorithm to act; .01 tends to work well.
- seed: the number used in the random number generator for placing the communities; any 4-digit number will work.
- factor1: the number to multiply the community coordinates by, 2 tends to work well, but you may want the communities farther apart.
- factor2: the number to multiply the node coordinates by within the community, if there are more than five nodes within a particular community it is helpful to normalize the coordinates and then multiply them by a factor to redistribute them. 4 tends to work in most cases, but you may want to change it based on your data.

- Outputs:
- xynew: the vectors of the xy coordinates for each node in your network, this is one of the inputs for graphplot2d.m
- XY: the vectors of xy coordinates for the communties, this is one of the inputs for drawForceCPie.m
- xyc: the cellarray of xy coordinates for the nodes within each community that are centered at zero.

- Calling this Function (either of the commands below):
- [xynew, XY, xyc]=FRKK(A,gn,epsilon)
- [xynew, XY, xyc]=FRKK(A,gn,epsilon, seed, factor1, factor2)

2. KKFR (Kamada-Kawai placement of communities, Fruchterman-Reingold placement of nodes within communities) KKFR.m

This code follows the algorithm in the article written by Tomihisa Kamada
and Satoru Kawai entitled "An Algorithm For Drawing General Undirected
Graphs" while respecting individual communities. It uses the Fruchterman-Reingold
algorithm to place the nodes within each community.
This code outputs the xy coordinates for the nodes of a given network

- Inputs:
- A: the adjacency matrix for the network
- gn: a list of group numbers for identifying each node by the community in which they they belong. For example, if you have 20 nodes, gn should be a 20 x 1 matrix. If node 5 is in the 2nd community, then at (5,1) the value is 2. You may use any community detection program you see fit.
- epsilon: determines how precise you want to be in your node placement. We suggest epsilon = .01.
- seed: the seed for the random number generator for the initial placement of the communities

- Outputs:
- xynew: the set of coordinates for each node, the first column is the x coordinates and the second column is the y-coordinates.

- Calling this function:
- [xynew]=KKFR(A,gn,epsilon, seed)

3. fruc_reinC (Fruchterman and Reingold placement of communities and then Fruchterman-Reingold placement of nodes within communities) fruc_reinC.m

This function uses the Fruchterman-Reingold algorithm to find the optimal
node placement for a given network with respect to Communities.

- Inputs:
- A: the adjacency matrix for the network
- gn: a vector of group numbers, i.e. every node that has the number 1 is in group 1.
- epsilon: the accuracy at which you would like the algorithm to act; .01 tends to work well.
- seed: the number used in the random number generator for placing the communities, any four digit number will work.
- factor1: the number to multiply the community coordinates by, 2 tends to work well, but you may want the communities farther apart.
- factor2: the number to multiply the node coordinates by within the community, if there are more than five nodes within a particular community it is helpful to normalize the coordinates and then multiply them by a factor to redistribute them. 4 tends to work in most cases, but you may want to change it based on your data.

- Outputs:
- xynew: the vectors of the xy coordinates for each node in your network, this is one of the inputs for graphplot2d.m
- XY: the vectors of xy coordinates for the communties, this is one of the inputs for drawForceCPie.m
- xyc: the cellarray of xy coordinates for the nodes within each community that are centered at zero.

- Calling this Function:
- [xynew, XY, xyc] = fruc_reinC(A, gn, epsilon, seed, factor1, factor2)

4. KamadaC (Kamada-Kawai placement of communities and then Kamada-Kawai placement of nodes within communities) KamadaC.m

This code follows the algorithm in the article written by Tomihisa Kamada
and Satoru Kawai entitled "An Algorithm For Drawing General Undirected
Graphs" while respecting individual communities.
This code outputs the xy coordinates for the nodes of a given network

- Inputs:
- A: the adjacency matrix for the network
- gn: a list of group numbers for identifying each node by the community they belong in. For example, if you have 20 nodes, gn should be a 20 x 1

matrix. If node 5 is in the 2nd community, then at (5,1) the value is 2. You can use any community detection program you see fit.
- epsilon: determines how precise you want to be in your node placement. We suggest epsilon = .01.

- Outputs:
- xynew: is the list of xy coordinates for each node in your network.

- Calling this Function:
- xy = KamadaC(A,maxgroups,.01)

5. Kamada (Kamada-Kawai placement of nodes, ignoring community structure) Kamada.m

This code follows the algorithm in the article written by Tomihisa Kamada
and Satoru Kawai entitled "An Algorithm For Drawing General Undirected
Graphs."

- Inputs:
- A:the adjacency matrix of your network
- epsilon: the accuracy at which the algorithm acts; .01 works well.

- Outputs:
- xynew: the matrix of xy coordinates for each node.

- Calling this Function:
- xynew = Kamada(A,epsilon)

6. fruc_rein (Fruchterman and Reingold placement of nodes, ignoring community structure) fruc_rein.m

This function uses the Fruchterman-Reingold Algorithm to find the optimal
node placement for a given network.

- Inputs:
- A: the adjacency matrix for the network
- eps: is the accuracy at which the algorithm will act; .01 works well in most cases.
- seed: the seed for the random number generator for initial placement of the nodes.

- Outputs:
- xy: the matrix of xy coordinates for each node in your network; the first column gives the x coordinates and the second column gives the y coordinates.

- Calling this Function:
- [xy] = fruc_rein(A,eps, seed)

7. drawForceCPie (Draws pie charts for each community) drawForceCPie.m

- Inputs:
- A: the adjacency matrix
- XY: the community coordinates, the XY output of all of the above programs that take community structure into consideration.
- scores: Categorical data for each node, this is how the colors are chosen.
- gn: group numbers found in community detection

- Outputs:
- Plot of network, no other outputs.

- Calling this Function:
- drawForceCPie(A,XY,scores,gn)

**References**

Tomihisa Kamada, Satoru Kawai.: An Algorithm for Drawing General Undirected Graphs.
Information Processing Letters, 31:7-15, 1988.

Fruchtermann, T.M.J., Reingold, E.M.: Graph drawing by force-directed placement.
Software, Practice and Experience 21:1129-1164, 1991.