R/drawGraph.R

#' Draw a social network graph
#' 
#' Creates a two-dimensional plot of a social-network graph.
#' 
#' @param dnet A docNetwork object
#' 
#' @param sub Logical. If true, only a subset of the graph will be printed.
#' 
#' @param communities Logical. If TRUE (default), the function will use the
#'                    communities data in \code{docNetwork@communities} slot
#'                    and assign different colors to each community. If FALSE,
#'                    the color will be determine by the \code{color} parameter.
#'                    
#' @param design Options for the design of the graph layout. All options inherited
#'               from the \code{igraph} package. Default is \code{layout.auto}, but
#'               other common options include \code{layout.fructerman.reingold} or
#'               \code{layout.circle}. Note that these layout options are actually
#'               R objects in the igraph environment, and so should not be placed
#'               in quotation marks when used here.
#' 
#' @param color  A string that defines the color of the nodes in the graph, if
#'               you want all nodes to be a single color. Default is dark red.
#' 
#' @param labelSize  A numeric value that sets the size of the labels for the 
#'                   nodes. To remove the labels altogether (often necessary
#'                   for larger graphs), just set the size to 0. 
#' 
#' @param nodeSize   A numeric value that sets the size of the nodes.
#' 
#' @section How to use it:
#' \code{drawGraph} offers only a few options regarding layout. It's designed
#' to generate easy-to-read results quickly. More advanced plotting
#' options are available directly through \code{igraph}.
#' 
#' @examples 
#' drawGraph(dnet)
#' drawGraph(dnet, labelSize = 0)
#' 
#' @export
drawGraph = function(dnet, communities = T, design = layout.auto, color= "darkred", labelSize=.5, nodeSize=3) {
  g = dnet@graph
  comms = dnet@communities
  if (communities == T) {
    palette = rainbow(max(membership(comms)))
    V(g)$color = palette[membership(comms)]
  } else {
    V(g)$color = color
  }
  V(g)$label.cex = labelSize
  V(g)$size = nodeSize
  if (labelSize == 0) {
    V(g)$label = ""
  }
  plot(g, layout = design)
}
michaelgavin/htn documentation built on May 22, 2019, 9:50 p.m.