reorderIgraphNodes: Re-order igraph nodes

reorderIgraphNodesR Documentation

Re-order igraph nodes

Description

Re-order igraph nodes

Usage

reorderIgraphNodes(
  g,
  sortAttributes = c("pie.color", "pie.color.length", "pie.border", "pie.border.length",
    "coloredrect.color", "coloredrect.border", "color", "frame.color", "label", "name"),
  nodeSortBy = c("x", "-y"),
  orderByAspect = TRUE,
  aspectThreshold = -1.25,
  layout = NULL,
  nodesets = NULL,
  colorV = NULL,
  verbose = FALSE,
  ...
)

reorder_igraph_nodes(
  g,
  sortAttributes = c("pie.color", "pie.color.length", "pie.border", "pie.border.length",
    "coloredrect.color", "coloredrect.border", "color", "frame.color", "label", "name"),
  nodeSortBy = c("x", "-y"),
  orderByAspect = TRUE,
  aspectThreshold = -1.25,
  layout = NULL,
  nodesets = NULL,
  colorV = NULL,
  verbose = FALSE,
  ...
)

Arguments

g

igraph object, typically expected to have a fixed graph layout stored as igraph::graph_attr(g, "layout"), or supplied via layout argument.

sortAttributes

character vector of node attribute names, to be applied in order when sorting nodes.

nodeSortBy

character vector containing "x" and "y" indicating the primary axis used to sort nodes. Note that sort order can be reversed by prepending "-", for example "-x" or "-y".

orderByAspect

logical indicating whether the aspect ratio of each nodeset determines the sort order. When orderByAspect=TRUE it ignores nodeSortBy and uses each nodeset aspect ratio to determine the order. Note that the original values for nodeSortOrder are retained, specifically the directional sign for each axis, such that "x" and "-y" will either become c("x", "-y") or c("-y", "x"). This way, other locales for which bottom-right to top-left ordering is more natural, use nodeSortBy=c("-x", "y") to preserve that order. It will become c("y", "-x") for tall aspect nodesets. See aspectThreshold below.

aspectThreshold

numeric indicating the aspect ratio threshold below which nodes are sorted c("-y", "x") top to bottom. All aspect ratios (x/y) above this threshold are sorted c("x", "-y") left to right. Aspect ratios with x < y are internally converted to negative ratios (-y/x) such that -1.25 is equivalent to x/y=0.8 for example.

layout

numeric matrix of node coordinates, or function used to produce layout coordinates. When layout is NULL, this function tries to use graph attribute igraph::graph_attr(g, "layout"), otherwise the relayout_with_qfr() is called.

nodesets

character with optional subset of nodesets to apply re-ordering. Each value must match names generated by get_cnet_nodeset(), otherwise it will be ignored.

colorV

optional character vector that contains R colors, used to order the colors in attributes such as "pie.color" and "coloredrect.color".

verbose

logical indicating whether to print verbose output.

...

additional arguments are ignored.

Details

This function takes an igraph and a layout in the form of coordinates, or a function used to produce coordinates. It repositions nodes within equivalent positions, ordering nodes by color along either the "x" or "y" direction.

Equivalent node positions are those with the same neighboring nodes. For example if node "A" and node "B" both have neighbors c("D", "E", "F") then nodes "A" and "B" are considered equivalent, and will be reordered by their color.

This function is particularly effective with concept network (Cnet) graphs, where multiple terms may be connnected to the same concept. For MultiEnrichmap, it typically works when multiple genes are connected to the same pathways. When this happens, the genes are sorted to group the colors.

Value

igraph with nodes positioned to order nodes by color. The layout coordinates are stored in the graph attribute "layout", accessible with g$layout or graph_attr(g, "layout"). When there are not multiple nodes sharing the same neighbors, the original igraph object is returned, with the addition of layout coordinates.

See Also

Other jam igraph functions: cnet2df(), cnet2im(), cnetplotJam(), cnetplot_internalJam(), color_edges_by_nodegroups(), color_edges_by_nodes_deprecated(), color_edges_by_nodes(), color_nodes_by_nodegroups(), communities2nodegroups(), drawEllipse(), edge_bundle_bipartite(), edge_bundle_nodegroups(), enrichMapJam(), fixSetLabels(), flip_edges(), get_bipartite_nodeset(), igraph2pieGraph(), jam_igraph(), jam_plot_igraph(), label_communities(), layout_with_qfrf(), layout_with_qfr(), mem2emap(), memIM2cnet(), mem_multienrichplot(), nodegroups2communities(), rectifyPiegraph(), relayout_with_qfr(), removeIgraphBlanks(), removeIgraphSinglets(), rotate_igraph_layout(), spread_igraph_labels(), subgraph_jam(), subsetCnetIgraph(), subset_igraph_components(), sync_igraph_communities(), with_qfr()

Examples

if (require(igraph)) {
   c3 <- c("red", "gold", "blue");
   c3l <- list(c3[1], c3[2], c3[3],
      c3[c(1,2)], c3[c(1,3)], c3[c(2,3)],
      c3[c(1,2,3)]);
   set.seed(123);
   pc <- c(c3l[1], sample(rep(c3l, c(6,5,5, 4, 1, 4, 4))))
   x <- lapply(pc, function(i){
      jamba::nameVector(i, paste0("group_", i))
   })
   g2 <- igraph::graph_from_edgelist(directed=FALSE,
      as.matrix(data.frame(
         node1=rep("Pathway", length(x)),
         node2=paste0("Gene", jamba::colNum2excelName(seq_along(x))))));
   V(g2)$pie.color <- x[c(1,seq_along(pc))];
   V(g2)$shape <- "pie";
   V(g2)$pie <- lapply(lengths(V(g2)$pie.color), function(i){
      rep(1, i)
   });
   V(g2)$frame.color <- "grey80";
   V(g2)$pie.border <- NA;
   V(g2)$color <- lapply(V(g2)$pie.color, colorjam::blend_colors)

   g2 <- relayout_with_qfr(g2, repulse=7, do_reorder=FALSE);
   g2b <- nudge_igraph_node(g2, nodes_xy=list(Pathway=c(0, -0.2)));
   g2b <- spread_igraph_labels(g2b, do_reorder=FALSE)
   igraph::V(g2b)$label.family <- "Arial"

   opar <- par("mar"=c(1, 1, 4, 1), xpd=TRUE);
   jam_igraph(g2b,
      main="Unordered",
      label_dist_factor=3,
      label_factor=0.7,
      node_factor=2,
      use_shadowText=TRUE)
   jam_igraph(reorderIgraphNodes(g2b),
      main="reorder_igraph_nodes()",
      label_dist_factor=3,
      label_factor=0.7,
      node_factor=2,
      use_shadowText=TRUE);
   jam_igraph(reorderIgraphNodes(g2b, nodeSortBy=c("-y","x")),
      main="reorderIgraphNodes(nodeSortBy=c(\"-y\",\"x\"))",
      label_dist_factor=3,
      label_factor=0.7,
      node_factor=2,
      use_shadowText=TRUE);

   jam_igraph(
      reorderIgraphNodes(g2b,
         nodeSortBy=c("-y", "x"),
         sortAttributes=c("-pie.color.length", "pie.color", "color", "label", "name")),
      main="reorder_igraph_nodes() by pie.color.length",
      label_dist_factor=3,
      label_factor=0.7,
      node_factor=2,
      use_shadowText=TRUE);
   par(opar);

   g2c <- g2b;
   set.seed(12)
   V(g2c)$frame.color <- sample(c("firebrick3", "#DDDDDD", "dodgerblue3"),
      replace=TRUE, size=igraph::vcount(g2c))
   opar <- par("lwd"=4, mar=c(1, 1, 4, 1), xpd=TRUE);
   jam_igraph(reorderIgraphNodes(g2c,
      nodeSortBy=c("-y", "x")),
      main="reorder_igraph_nodes() including frame.color",
      label_dist_factor=3,
      label_factor=0.7,
      node_factor=2,
      use_shadowText=TRUE);
   par(opar);

   g2d <- reorderIgraphNodes(g2b);
   set.seed(12)
   mn <- (lengths(V(g2d)$pie.color) > 1);
   V(g2d)[!mn]$frame.color <- sample(c("firebrick3", "#DDDDDD", "dodgerblue3"),
      replace=TRUE, size=sum(!mn))
   V(g2d)$pie.border <- rep(list(character(0)), vcount(g2d))
   V(g2d)[mn]$pie.border <- lapply(which(mn), function(i){
      jamba::nameVector(
         sample(c("firebrick3", "#DDDDDD", "dodgerblue3"),
            replace=TRUE, size=lengths(V(g2d)[i]$pie.color)),
         names(V(g2d)[i]$pie.color[[1]]))
   })
   g2e <- reorderIgraphNodes(g2d,
      nodeSortBy=c("-y", "x"));
   opar <- par("lwd"=4, mar=c(1, 1, 4, 1), xpd=TRUE);
   options("inner_pie_border"=TRUE);
   jam_igraph(g2e,
      main="reorder_igraph_nodes() including frame.color",
      label_dist_factor=3,
      label_factor=0.7,
      node_factor=2,
      use_shadowText=TRUE);
   par(opar);

   g2f <- g2e;
   igraph::V(g2f)["GeneV"]$frame.color <- "green";
   igraph::V(g2f)["GeneE"]$frame.color <- "green";
   opar <- par("lwd"=5, mar=c(1, 1, 4, 1), xpd=TRUE);
   options("inner_pie_border"=TRUE);
   jam_igraph(g2f,
      main="reorder_igraph_nodes() including frame.color",
      label_dist_factor=3,
      label_factor=0.7,
      node_factor=2,
      use_shadowText=TRUE);
   par(opar);

}


jmw86069/multienrichjam documentation built on Feb. 7, 2024, 12:58 a.m.