require(igraph)
require(graphstats)
require(gridExtra)
require(ggplot2)
# define this function
load.data <- function(dat)
{
  e <- new.env()
  name <- data(list=dat, envir = e)[1]
  e[[name[[1]]]]
}

make_plots <- function(datname, title, src.name, tgt.name, attr.code, src.attr.name,
                       tgt.attr.name, wt, edge.xfm) {
  # Adjacency Matrix
  g <- load.data(datname)
  adj.plot <- gs.plot.plot_adjacency(g, title=title, edge.attr = wt, src.label=src.name,
                                     tgt.label=tgt.name, edge.xfm=edge.xfm)

  # Degree Sequence
  gr.degseq.in <- strength(g, mode="in")
  gr.degseq.out <- strength(g, mode="out")
  if (isTRUE(all.equal(as.numeric(gr.degseq.in), as.numeric(gr.degseq.out)))) {
    gr.deg.dat <- data.frame(vertex=names(V(g)), degree=gr.degseq.in, Direction="undirected")
  } else {
    gr.deg.dat <- rbind(data.frame(vertex=names(V(g)), degree=gr.degseq.in, Direction="in"),
                        data.frame(vertex=names(V(g)), degree=gr.degseq.out, Direction="out"))
  }
  deg.plot <- ggplot(gr.deg.dat, aes(x=degree, group=Direction, fill=Direction)) +
    geom_density(alpha=0.2) +
    xlab("Vertex Degree") +
    ylab("Probability") +
    ggtitle(title) +
    theme_bw()

  # SBM
  gr.sbm <- gs.sbm.fit(g, community.attribute=attr.code)
  sbm.plot <- gs.plot.plot_adjacency(gr.sbm, title=title, src.label=src.attr.name,
                                     tgt.label=tgt.attr.name, vertex.label=TRUE, edge.attr="weight")
  return(list(adj.plot=adj.plot, deg.plot=deg.plot, sbm.plot=sbm.plot))
}

Fly, Left Hemisphere

fly.left <- make_plots("fly.left", "Fly, Left Hemisphere", "Source Neuron", "Target Neuron",
                       "type", "Source Neuron Type", "Target Neuron Type", NULL)

Fly, Right Hemisphere

fly.right <- make_plots("fly.right", "Fly, Right Hemisphere", "Source Neuron", "Target Neuron",
                       "type", "Source Neuron Type", "Target Neuron Type", NULL)

Comparison

Adjacency Matrix

grid.arrange(fly.left$adj.plot, fly.right$adj.plot, ncol=2)

Degree Sequence

grid.arrange(fly.left$deg.plot, fly.right$deg.plot, ncol=2)

SBM

grid.arrange(fly.left$sbm.plot, fly.right$sbm.plot, ncol=2)


neurodata/graphstats documentation built on May 14, 2019, 5:19 p.m.