% Generated by roxygen2: do not edit by hand
% Please edit documentation in R/enrich.R
\name{EnrichMap}
\alias{EnrichMap}
\alias{EnrichNetwork}
\title{Enrichment Map and Enrichment Network}
\usage{
EnrichMap(
  data,
  in_form = c("auto", "clusterProfiler", "clusterprofiler", "enrichr"),
  split_by = NULL,
  split_by_sep = "_",
  top_term = 10,
  metric = "p.adjust",
  layout = "fr",
  minchar = 2,
  cluster = "fast_greedy",
  show_keyword = FALSE,
  nlabel = 4,
  character_width = 50,
  mark = "ellipse",
  label = c("term", "feature"),
  labelsize = 5,
  expand = c(0.4, 0.4),
  theme = "theme_this",
  theme_args = list(),
  palette = "Paired",
  palcolor = NULL,
  palreverse = FALSE,
  alpha = 1,
  aspect.ratio = 1,
  legend.position = "right",
  legend.direction = "vertical",
  title = NULL,
  subtitle = NULL,
  xlab = NULL,
  ylab = NULL,
  seed = 8525,
  combine = TRUE,
  nrow = NULL,
  ncol = NULL,
  byrow = TRUE,
  axes = NULL,
  axis_titles = axes,
  guides = NULL,
  design = NULL,
  ...
)

EnrichNetwork(
  data,
  in_form = c("auto", "clusterProfiler", "clusterprofiler", "enrichr"),
  split_by = NULL,
  split_by_sep = "_",
  top_term = 10,
  metric = "p.adjust",
  character_width = 50,
  layout = "fr",
  layoutadjust = TRUE,
  adjscale = 60,
  adjiter = 100,
  blendmode = "blend",
  labelsize = 5,
  theme = "theme_this",
  theme_args = list(),
  palette = "Paired",
  palcolor = NULL,
  palreverse = FALSE,
  alpha = 1,
  aspect.ratio = 1,
  legend.position = "right",
  legend.direction = "vertical",
  title = NULL,
  subtitle = NULL,
  xlab = NULL,
  ylab = NULL,
  seed = 8525,
  combine = TRUE,
  nrow = NULL,
  ncol = NULL,
  byrow = TRUE,
  axes = NULL,
  axis_titles = axes,
  guides = NULL,
  design = NULL,
  ...
)
}
\arguments{
\item{data}{A data frame containing enrichment results in clusterProfiler
format (see \code{\link{EnrichMap}} for the expected columns).
If you have enrichment results from multiple databases, you can combine
them into one data frame and add a column (e.g. \code{Database}) to
indicate the source.  Use \code{split_by = "Database"} to plot them
side by side.}

\item{in_form}{A character string specifying the input format.
When \code{"auto"} (default), the function infers the format from the
column names.  Other options are \code{"clusterProfiler"},
\code{"clusterprofiler"}, and \code{"enrichr"}.}

\item{split_by}{The column(s) to split data by and plot separately.
When \code{combine = TRUE}, the combined plot's data (\code{p$data})
contains the rows of every split, tagged with the \code{split_by}
column. For ggraph-based plots (e.g. \code{\link{Network}},
\code{\link{ClustreePlot}}), the node layout of every split is
exposed with the split column, and the original link rows (also
tagged with the \code{split_by} column) are available as the
\code{"edges"} attribute of the data.}

\item{split_by_sep}{The separator for multiple split_by columns. See \code{split_by}}

\item{top_term}{An integer specifying the maximum number of terms to
include.  Terms are ranked by \code{metric} (ascending).  Default
\code{100}.}

\item{metric}{A character string specifying the significance metric
used for top-term selection and node scoring: \code{"p.adjust"}
(default) or \code{"pvalue"}.  The value is transformed as
\eqn{-\log_{10}(metric)}.}

\item{layout}{A character string naming the igraph layout algorithm.
Built-in shortcuts: \code{"circle"}, \code{"tree"}, \code{"grid"}.
Otherwise, the suffix passed to \code{layout_with_<layout>} in igraph
(e.g. \code{"fr"} for Fruchterman-Reingold, \code{"kk"} for
Kamada-Kawai).  Default \code{"fr"}.}

\item{minchar}{An integer specifying the minimum character length for
words to be included as keywords when \code{show_keyword = TRUE}.
Default \code{2}.}

\item{cluster}{A character string naming the igraph community detection
algorithm.  The suffix passed to \code{cluster_<cluster>} in igraph
(e.g. \code{"fast_greedy"}, \code{"walktrap"},
\code{"edge_betweenness"}, \code{"infomap"}).  Default
\code{"fast_greedy"}.}

\item{show_keyword}{A logical value.  When \code{TRUE}, the
\code{Description} text is tokenized and the most significant words
per cluster are shown as keywords.  When \code{FALSE} (default), the
original term descriptions are used as labels.}

\item{nlabel}{An integer specifying the number of keywords or term
descriptions to show per cluster in the legend labels.  Default
\code{4}.}

\item{character_width}{An integer specifying the maximum width (in
characters) at which keyword labels are wrapped via
\code{strwrap(width = character_width)}.  Default \code{50}.}

\item{mark}{A character string naming the ggforce hull function.
One of \code{"ellipse"} (default), \code{"rect"}, \code{"circle"}, or
\code{"text"} -- passed as the suffix to \code{geom_mark_<mark>}.}

\item{label}{A character string specifying what information to display
in the legend labels.  Either \code{"term"} (default; shows top term
descriptions/keywords per cluster) or \code{"feature"} (shows top
gene symbols per cluster).}

\item{labelsize}{A numeric value specifying the font size of the
cluster labels drawn by the ggforce mark layer.  Default \code{5}.}

\item{expand}{The values to expand the x and y axes. It is like CSS padding.
When a single value is provided, it is used for both axes on both sides.
When two values are provided, the first value is used for the top/bottom side and the second value is used for the left/right side.
When three values are provided, the first value is used for the top side, the second value is used for the left/right side, and the third value is used for the bottom side.
When four values are provided, the values are used for the top, right, bottom, and left sides, respectively.
You can also use a named vector to specify the values for each side.
When the axis is discrete, the values will be applied as 'add' to the 'expansion' function.
When the axis is continuous, the values will be applied as 'mult' to the 'expansion' function.
See also \url{https://ggplot2.tidyverse.org/reference/expansion.html}}

\item{theme}{A character string or a theme class (i.e. ggplot2::theme_classic) specifying the theme to use.
Default is "theme_this".}

\item{theme_args}{A list of arguments to pass to the theme function.}

\item{palette}{A character string specifying the palette to use.
A named list or vector can be used to specify the palettes for different \code{split_by} values.}

\item{palcolor}{A character string specifying the color to use in the palette.
A named list can be used to specify the colors for different \code{split_by} values.
If some values are missing, the values from the palette will be used (palcolor will be NULL for those values).}

\item{palreverse}{A logical value indicating whether to reverse the palette. Default is FALSE.}

\item{alpha}{A numeric value specifying the transparency of the plot.}

\item{aspect.ratio}{A numeric value specifying the aspect ratio of the plot.}

\item{legend.position}{A character string specifying the position of the legend.
if \code{waiver()}, for single groups, the legend will be "none", otherwise "right".}

\item{legend.direction}{A character string specifying the direction of the legend.}

\item{title}{A character string specifying the title of the plot.
A function can be used to generate the title based on the default title.
This is useful when split_by is used and the title needs to be dynamic.}

\item{subtitle}{A character string specifying the subtitle of the plot.}

\item{xlab}{A character string specifying the x-axis label.}

\item{ylab}{A character string specifying the y-axis label.}

\item{seed}{The random seed to use. Default is 8525.}

\item{combine}{Whether to combine the plots into one when facet is FALSE. Default is TRUE.}

\item{nrow}{A numeric value specifying the number of rows in the facet.}

\item{ncol}{A numeric value specifying the number of columns in the facet.}

\item{byrow}{A logical value indicating whether to fill the plots by row.}

\item{axes}{A string specifying how axes should be treated. Passed to \code{\link[patchwork:wrap_plots]{patchwork::wrap_plots()}}.
Only relevant when \code{split_by} is used and \code{combine} is TRUE.
Options are:
\itemize{
\item 'keep' will retain all axes in individual plots.
\item 'collect' will remove duplicated axes when placed in the same run of rows or columns of the layout.
\item 'collect_x' and 'collect_y' will remove duplicated x-axes in the columns or duplicated y-axes in the rows respectively.
}}

\item{axis_titles}{A string specifying how axis titltes should be treated. Passed to \code{\link[patchwork:wrap_plots]{patchwork::wrap_plots()}}.
Only relevant when \code{split_by} is used and \code{combine} is TRUE.
Options are:
\itemize{
\item 'keep' will retain all axis titles in individual plots.
\item 'collect' will remove duplicated titles in one direction and merge titles in the opposite direction.
\item 'collect_x' and 'collect_y' control this for x-axis titles and y-axis titles respectively.
}}

\item{guides}{A string specifying how guides should be treated in the layout. Passed to \code{\link[patchwork:wrap_plots]{patchwork::wrap_plots()}}.
Only relevant when \code{split_by} is used and \code{combine} is TRUE.
Options are:
\itemize{
\item 'collect' will collect guides below to the given nesting level, removing duplicates.
\item 'keep' will stop collection at this level and let guides be placed alongside their plot.
\item 'auto' will allow guides to be collected if a upper level tries, but place them alongside the plot if not.
}}

\item{design}{Specification of the location of areas in the layout, passed to \code{\link[patchwork:wrap_plots]{patchwork::wrap_plots()}}.
Only relevant when \code{split_by} is used and \code{combine} is TRUE. When specified, \code{nrow}, \code{ncol}, and \code{byrow} are ignored.
See \code{\link[patchwork:wrap_plots]{patchwork::wrap_plots()}} for more details.}

\item{...}{Additional arguments.}

\item{layoutadjust}{A logical value.  When \code{TRUE} (default),
applies \code{\link{adjust_network_layout}()} after the initial layout
to reduce node overlap based on label width and a repulsion simulation.}

\item{adjscale}{A numeric value controlling the scale of the layout
adjustment.  Passed as the \code{scale} argument to
\code{\link{adjust_network_layout}()}.  Default \code{60}.}

\item{adjiter}{A numeric value controlling the number of iterations for
the layout adjustment.  Passed as the \code{iter} argument to
\code{\link{adjust_network_layout}()}.  Default \code{100}.}

\item{blendmode}{A character string specifying how gene colours are
computed from the colours of the terms they belong to.  One of
\code{"blend"} (default), \code{"average"}, \code{"multiply"}, or
\code{"screen"}.  Passed to \code{blend_colors()}.}
}
\value{
A \code{ggplot} object (single plot), a \code{patchwork} /
\code{wrap_plots} object (when \code{split_by} is provided and
\code{combine = TRUE}), or a list of \code{ggplot} objects (when
\code{split_by} is provided and \code{combine = FALSE}).

A \code{ggplot} object (single plot), a \code{patchwork} /
\code{wrap_plots} object (when \code{split_by} is provided and
\code{combine = TRUE}), or a list of \code{ggplot} objects (when
\code{split_by} is provided and \code{combine = FALSE}).
}
\description{
\code{EnrichMap} draws an enrichment map -- a gene-set similarity network
where each node is an enriched term, node size encodes the number of
associated genes, node fill colour encodes cluster membership (detected
via igraph community detection), and edge thickness encodes the number
of overlapping genes between term pairs.  The plot uses a force-directed
layout to arrange terms, and ggforce hull annotations group terms into
clusters.  Keyword or term-description labels appear in the legend.

\code{EnrichNetwork} draws an enrichment network -- a term-gene bipartite
graph where term nodes are shown as numbered circles and gene nodes as
labelled rectangles.  Gene node colours are blended from the colours of
all terms they belong to.  A force-directed layout positions the nodes,
with optional overlap adjustment for better readability.

Both functions accept enrichment results from clusterProfiler or Enrichr
(the latter is auto-detected and preprocessed via
\code{\link{prepare_enrichr_result}()}).
}
\section{split_by Workflow (EnrichMap)}{


When \code{split_by} is provided, \code{EnrichMap()} executes the
following pipeline:
\enumerate{
\item \strong{Argument validation} --
\code{\link{validate_common_args}()} checks the seed.
\item \strong{Input format detection} -- \code{match.arg()} resolves
\code{in_form}; \code{"auto"} mode infers the format from
column names.
\item \strong{Enrichr preprocessing} -- when format is \code{"enrichr"},
calls \code{\link{prepare_enrichr_result}()} to rename columns
and infer GeneRatio/BgRatio.
\item \strong{Split column resolution} --
\code{\link{check_columns}()} validates \code{split_by}
(force_factor, allow_multi, concat_multi).
\item \strong{Data splitting} -- splits \code{data} by
\code{split_by} levels, preserving factor level order.
\item \strong{Per-split palette/colour} --
\code{\link{check_palette}()} and
\code{\link{check_palcolor}()} resolve per-split palette and
colour overrides.
\item \strong{Per-split legend} -- \code{\link{check_legend}()}
resolves \code{legend.position} and \code{legend.direction}
per split.
\item \strong{Per-split title} -- when \code{title} is a function,
it receives the default title (the split level name); otherwise
\code{title \%||\% split_level} is used.
\item \strong{Dispatch} -- each split subset is passed to
\code{\link{EnrichMapAtomic}} with its resolved parameters.
\item \strong{Combination} -- \code{\link{combine_plots}()} assembles
the list of plots via \code{patchwork::wrap_plots}, honouring
\code{nrow}/\code{ncol}/\code{byrow}/\code{axes}/
\code{axis_titles}/\code{guides}/\code{design}.
}
}

\section{split_by Workflow (EnrichNetwork)}{


When \code{split_by} is provided, \code{EnrichNetwork()} executes the
same pipeline as \code{EnrichMap()} above, but dispatches each split
subset to \code{\link{EnrichNetworkAtomic}}.
}

\examples{
\donttest{
data(enrich_example)
EnrichMap(enrich_example)
EnrichMap(enrich_example, label = "feature")
EnrichMap(enrich_example, show_keyword = TRUE, label = "term")
EnrichMap(enrich_example, show_keyword = TRUE, label = "feature")

data(enrich_multidb_example)
EnrichMap(enrich_multidb_example, split_by = "Database")
EnrichMap(enrich_multidb_example, split_by = "Database",
          palette = list(DB1 = "Paired", DB2 = "Set1"))
}
\donttest{
EnrichNetwork(enrich_example, top_term = 5)
}
}
