R/huge.plot.R

Defines functions huge.plot

Documented in huge.plot

#-----------------------------------------------------------------------#
# Package: High-dimensional Undirected Graph Estimation                 #
# huge.plot(): graph visualization                                      #
#-----------------------------------------------------------------------#

#' Graph visualization
#'
#' Implements the graph visualization using adjacency matrix. It can automatic organize 2D embedding layout.
#'
#' The user can change \code{cur.num} to plot several figures and select the best one. The implementation is based on the popular package "igraph".
#'
#' @param G The adjacency matrix corresponding to the graph.
#' @param epsflag If \code{epsflag = TRUE}, save the plot as an eps file in the target directory. The default value is \code{FALSE}.
#' @param graph.name The name of the output eps files. The default value is "default".
#' @param cur.num The number of plots saved as eps files. Only applicale when \code{epsflag = TRUE}. The default value is 1.
#' @param location Target directory. The default value is the current working directory.
#' @seealso \code{\link{huge}} and \code{\link{huge-package}}.
#' @examples
#' ## visualize the hub graph
#' L = huge.generator(graph = "hub")
#' huge.plot(L$theta)
#'
#' ## visualize the band graph
#' L = huge.generator(graph = "band",g=5)
#' huge.plot(L$theta)
#'
#' ## visualize the cluster graph
#' L = huge.generator(graph = "cluster")
#' huge.plot(L$theta)
#'
#' ## plot 5 graphs and save the plots as eps files in the tempdir()
#' huge.plot(L$theta, epsflag = TRUE, cur.num = 5, location = tempdir())
#' @export
huge.plot = function(G, epsflag = FALSE, graph.name = "default", cur.num = 1, location=NULL){
  if(missing(location))  location = tempdir()
  g = graph_from_adjacency_matrix(as.matrix(G!=0), mode="undirected", diag=FALSE)
  layout.grid = layout_with_fr(g)

  if(epsflag == TRUE){
    caller.device = grDevices::dev.cur()
    postscript(file.path(location, paste(paste(graph.name, cur.num, sep=""), "eps", sep=".")), width = 8.0, height = 8.0)
    plot.device = grDevices::dev.cur()
    on.exit({
      devices = grDevices::dev.list()
      if(!is.null(devices) && plot.device %in% devices)
        grDevices::dev.off(plot.device)
      devices = grDevices::dev.list()
      if(caller.device != 1L && !is.null(devices) &&
         caller.device %in% devices)
        grDevices::dev.set(caller.device)
    }, add = TRUE)
  } else {
    old.par = .huge_graphics_state()
    on.exit(.huge_restore_graphics_state(old.par), add = TRUE)
  }
  par(mfrow = c(1,1))
  plot(g, layout=layout.grid, edge.color='gray50',vertex.color="red", vertex.size=2, vertex.label=NA)
}

Try the huge package in your browser

Any scripts or data that you put into this service are public.

huge documentation built on Aug. 4, 2026, 5:09 p.m.