% Generated by roxygen2: do not edit by hand
% Please edit documentation in R/chordplot.R
\name{ChordPlot}
\alias{ChordPlot}
\alias{CircosPlot}
\title{Chord / Circos plot}
\usage{
ChordPlot(
  data,
  y = NULL,
  from = NULL,
  from_sep = "_",
  to = NULL,
  to_sep = "_",
  split_by = NULL,
  split_by_sep = "_",
  flip = FALSE,
  links_color = c("from", "to"),
  theme = "theme_this",
  theme_args = list(),
  palette = "Paired",
  palcolor = NULL,
  palreverse = FALSE,
  alpha = 0.5,
  labels_rot = FALSE,
  title = NULL,
  subtitle = NULL,
  seed = 8525,
  keep_na = FALSE,
  keep_empty = FALSE,
  combine = TRUE,
  nrow = NULL,
  ncol = NULL,
  byrow = TRUE,
  axes = NULL,
  axis_titles = axes,
  guides = NULL,
  design = NULL,
  ...
)

CircosPlot(
  data,
  y = NULL,
  from = NULL,
  from_sep = "_",
  to = NULL,
  to_sep = "_",
  split_by = NULL,
  split_by_sep = "_",
  flip = FALSE,
  links_color = c("from", "to"),
  theme = "theme_this",
  theme_args = list(),
  palette = "Paired",
  palcolor = NULL,
  palreverse = FALSE,
  alpha = 0.5,
  labels_rot = FALSE,
  title = NULL,
  subtitle = NULL,
  seed = 8525,
  keep_na = FALSE,
  keep_empty = FALSE,
  combine = TRUE,
  nrow = NULL,
  ncol = NULL,
  byrow = TRUE,
  axes = NULL,
  axis_titles = axes,
  guides = NULL,
  design = NULL,
  ...
)
}
\arguments{
\item{data}{A data frame.}

\item{y}{A character string specifying the column name of the data frame to plot for the y-axis.}

\item{from}{A character string (or vector) specifying the column name(s)
for the source nodes.  Character/factor columns are expected.  Multiple
columns are concatenated with \code{from_sep}.}

\item{from_sep}{A character string to join multiple \code{from} columns.
Default \code{"_"}.}

\item{to}{A character string (or vector) specifying the column name(s)
for the target nodes.  Character/factor columns are expected.  Multiple
columns are concatenated with \code{to_sep}.}

\item{to_sep}{A character string to join multiple \code{to} columns.
Default \code{"_"}.}

\item{split_by}{The column(s) to split the data by for separate sub-diagrams.
Multiple columns are concatenated with \code{split_by_sep}.}

\item{split_by_sep}{A character string to separate concatenated
\code{split_by} columns.  Default \code{"_"}.}

\item{flip}{Logical; if \code{TRUE}, swap the source and target nodes,
reversing the link direction.}

\item{links_color}{A character string controlling which node's colour
each link ribbon takes: \code{"from"} (default) or \code{"to"}.}

\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{labels_rot}{Logical; if \code{TRUE}, rotate sector labels by 90
degrees (clockwise).  Default \code{FALSE} uses \code{niceFacing} for
automatic orientation.}

\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{seed}{A numeric seed for reproducibility.}

\item{keep_na}{A logical value or a character to replace the NA values in the data.
It can also take a named list to specify different behavior for different columns.
If TRUE or NA, NA values will be replaced with NA.
If FALSE, NA values will be removed from the data before plotting.
If a character string is provided, NA values will be replaced with the provided string.
If a named vector/list is provided, the names should be the column names to apply the behavior to,
and the values should be one of TRUE, FALSE, or a character string.
Without a named vector/list, the behavior applies to categorical/character columns used on the plot,
for example, the \code{x}, \code{group_by}, \code{fill_by}, etc.}

\item{keep_empty}{One of FALSE, TRUE and "level". It can also take a named list to specify
different behavior for different columns. Without a named list, the behavior applies to the
categorical/character columns used on the plot, for example, the \code{x}, \code{group_by}, \code{fill_by}, etc.
\itemize{
\item{\code{FALSE} (default): Drop empty factor levels from the data before plotting.}
\item{\code{TRUE}: Keep empty factor levels and show them as a separate category in the plot.}
\item{\code{"level"}: Keep empty factor levels, but do not show them in the plot.
But they will be assigned colors from the palette to maintain consistency across multiple plots.
Alias: \code{levels}}
}}

\item{combine}{Logical; when \code{TRUE} (default), returns a combined
\code{patchwork} object.  When \code{FALSE}, returns a named list of
individual wrapped elements.}

\item{ncol, nrow}{Integer number of columns / rows for the combined layout.}

\item{byrow}{Logical; fill the combined layout by row (default \code{TRUE}).}

\item{axes, axis_titles}{Character strings for axis handling in the
combined layout.}

\item{guides}{Character string for legend collection across panels.}

\item{design}{A custom layout design for the combined plot.}

\item{...}{Additional arguments.}
}
\value{
A \code{patchwork} object or a named list of wrapped elements
(when \code{combine = FALSE}), each with \code{height} and \code{width}
attributes in inches.
}
\description{
Draws a chord diagram (also known as a circos plot) to visualise
relationships between two categorical variables.  Categories are arranged
around a circle, and connecting ribbons (links) represent the flow or
association between source and target nodes.  The width of each link is
proportional to the associated numeric value or observation count.

The function supports \strong{count aggregation} (omit \code{y} to plot
observation counts per pair), \strong{link colouring} by source or target
node, \strong{label rotation} options, and splitting into separate
sub-diagrams via \code{split_by}.

\code{CircosPlot} is an alias of \code{ChordPlot}.

\code{CircosPlot} is an alias for \code{\link{ChordPlot}}.
}
\section{split_by workflow}{

When \code{split_by} is provided:
\enumerate{
\item \code{\link{check_keep_na}()} and \code{\link{check_keep_empty}()}
normalise the \code{keep_na} / \code{keep_empty} arguments for all
columns (\code{split_by}, \code{from}, \code{to}).
\item The \code{split_by} column is validated and its NA / empty levels
are processed.  It is then removed from the per-column lists.
\item The data is split by \code{split_by} (preserving level order).  If
\code{split_by} is \code{NULL}, the data is wrapped in a
single-element list with name \code{"..."}.
\item Per-split \code{palette} and \code{palcolor} are resolved via
\code{\link{check_palette}()} and \code{\link{check_palcolor}()}.
\item \code{\link{ChordPlotAtomic}()} is called for each split.
When \code{title} is a function, it receives the split level name
for dynamic titles.
\item Results are combined via \code{\link{combine_plots}()}.
}
}

\examples{
\donttest{
set.seed(8525)
data <- data.frame(
    nodes1 = sample(c("Soure1", "Source2", "Source3"), 10, replace = TRUE),
    nodes2 = sample(letters[1:3], 10, replace = TRUE),
    y = sample(1:5, 10, replace = TRUE)
)

# Basic chord diagram (counts)
ChordPlot(data, from = "nodes1", to = "nodes2")

# Links coloured by target + rotated labels
ChordPlot(data, from = "nodes1", to = "nodes2",
          links_color = "to", labels_rot = TRUE)

# With explicit y values (link thickness)
ChordPlot(data, from = "nodes1", to = "nodes2", y = "y")

# Split by a column — one diagram per split level
ChordPlot(data, from = "nodes1", to = "nodes2", split_by = "y")

# Per-split palettes
ChordPlot(data, from = "nodes1", to = "nodes2", split_by = "y",
          palette = c("1" = "Reds", "2" = "Blues",
                      "3" = "Greens", "4" = "Purp"))

# Flip source/target direction
ChordPlot(data, from = "nodes1", to = "nodes2", flip = TRUE)
}
}
