% Generated by roxygen2: do not edit by hand
% Please edit documentation in R/wordcloudplot.R
\name{WordCloudPlot}
\alias{WordCloudPlot}
\title{Word cloud plot}
\usage{
WordCloudPlot(
  data,
  word_by = NULL,
  sentence_by = NULL,
  count_by = NULL,
  score_by = NULL,
  count_name = NULL,
  score_name = NULL,
  split_by = NULL,
  split_by_sep = "_",
  words_excluded = plotthis::words_excluded,
  score_agg = mean,
  minchar = 2,
  word_size = c(2, 8),
  top_words = 100,
  facet_by = NULL,
  facet_scales = "fixed",
  facet_ncol = NULL,
  facet_nrow = NULL,
  facet_byrow = TRUE,
  theme = "theme_this",
  theme_args = list(),
  palette = "Spectral",
  palcolor = NULL,
  alpha = 1,
  palreverse = FALSE,
  aspect.ratio = 1,
  legend.position = "right",
  legend.direction = "vertical",
  title = NULL,
  subtitle = 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.}

\item{word_by}{A character string specifying the column name containing
pre-tokenized words. A character column is expected. Use this when your
data already has one word per row (or a list of words per row). Mutually
exclusive with \code{sentence_by}.}

\item{sentence_by}{A character string specifying the column name containing
sentences or phrases to be split into individual words. A character column
is expected. The text is lowercased and punctuation is removed before
splitting on whitespace boundaries. Mutually exclusive with
\code{word_by}.}

\item{count_by}{A character string specifying the numeric column for the
count or frequency of each word. When \code{NULL} (the default), each
occurrence counts as 1. Must be \code{NULL} when \code{sentence_by} is
used, as counts are derived from the number of occurrences after splitting.}

\item{score_by}{A character string specifying the numeric column for the
score of each word, mapped to the text colour via a continuous gradient.
When \code{NULL} (the default), all words receive a score of 1 and are
coloured at the low end of the palette.}

\item{count_name}{A character string for the size legend title. When
\code{NULL} (the default), \code{"Count"} is used.}

\item{score_name}{A character string for the colour-bar legend title. When
\code{NULL} (the default), \code{"Score"} is used.}

\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{words_excluded}{A character vector of words to exclude from the word
cloud. Matching is case-insensitive. Defaults to
\code{plotthis::words_excluded}, a built-in set of common English stop
words.}

\item{score_agg}{A function to aggregate the scores when multiple
observations of the same word exist. Default is \code{mean}. Other
options include \code{sum}, \code{median}, or a custom function.}

\item{minchar}{A numeric value specifying the minimum number of characters
a word must have to be included. Words with fewer characters are filtered
out. Default: \code{2}.}

\item{word_size}{A numeric vector of length 2 specifying the range of font
sizes (in mm) for the words. Passed to \code{scale_size(range =
 word_size)}. Default: \code{c(2, 8)}.}

\item{top_words}{A numeric value specifying the maximum number of words to
display, selected by highest score. Default: \code{100}.}

\item{facet_by}{A character string specifying the column name of the data frame to facet the plot.
Otherwise, the data will be split by \code{split_by} and generate multiple plots
and combine them into one using \code{patchwork::wrap_plots}}

\item{facet_scales}{Whether to scale the axes of facets. Default is "fixed"
Other options are "free", "free_x", "free_y". See \code{ggplot2::facet_wrap}}

\item{facet_ncol}{A numeric value specifying the number of columns in the facet.
When facet_by is a single column and facet_wrap is used.}

\item{facet_nrow}{A numeric value specifying the number of rows in the facet.
When facet_by is a single column and facet_wrap is used.}

\item{facet_byrow}{A logical value indicating whether to fill the plots by row. Default is TRUE.}

\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{alpha}{A numeric value specifying the transparency of the plot.}

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

\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{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.}
}
\value{
A \code{ggplot} object (when no \code{split_by} is used), a
\code{patchwork} object (when \code{combine = TRUE} and
\code{split_by} is used), or a named list of \code{ggplot} objects
(when \code{combine = FALSE}), each with \code{height} and
\code{width} attributes in inches.
}
\description{
Draws a word cloud plot that visualises word frequency and importance.
Words are displayed with font size proportional to a count variable and
colour based on a continuous score variable, using
\code{\link[ggwordcloud]{geom_text_wordcloud}} for rendering.

The function supports \strong{pre-tokenised words} (via \code{word_by})
and \strong{sentence splitting} (via \code{sentence_by}). Sentences are
automatically lowercased, stripped of punctuation, and split into
individual words before aggregation. Common stop words can be excluded
via \code{words_excluded}, and the number of displayed words is
controlled by \code{top_words}.

The word cloud can be \strong{faceted} (via \code{facet_by}) or
\strong{split} into separate sub-plots via \code{split_by}. When
\code{split_by} is used, each split level receives its own word cloud,
and the results are combined into a single layout via
\code{\link{combine_plots}}.
}
\section{split_by workflow}{

When \code{split_by} is provided:
\enumerate{
\item \code{\link{validate_common_args}()} validates the \code{seed}
and \code{facet_by} constraints (maximum 2 facet columns).
\item The \code{theme} argument is resolved via
\code{\link{process_theme}()}.
\item The \code{split_by} column(s) are validated and transformed via
\code{\link{check_columns}()} with \code{force_factor = TRUE}
and \code{concat_multi = TRUE}.
\item The data frame is split by \code{split_by} (preserving factor
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}, \code{palcolor},
\code{legend.position}, and \code{legend.direction} are resolved
via \code{\link{check_palette}()}, \code{\link{check_palcolor}()},
and \code{\link{check_legend}()}.
\item \code{\link{WordCloudPlotAtomic}()} is called for each split. If
\code{title} is a function, it receives the split level name and
can generate dynamic titles per sub-plot.
\item Results are combined via \code{\link{combine_plots}()} (when
\code{combine = TRUE}) or returned as a named list (when
\code{combine = FALSE}).
}
}

\examples{
\donttest{
set.seed(8525)
data <- data.frame(
    word = c("apple", "banana", "cherry", "date", "elderberry",
             "fig", "grape", "honeydew", "kiwi", "lemon"),
    count = c(10, 20, 30, 40, 50, 15, 25, 35, 45, 55),
    score = c(1, 2, 3, 4, 5, 1.5, 2.5, 3.5, 4.5, 5.5),
    facet = rep(c("Group1", "Group2"), each = 5),
    split = rep(c("A", "B"), 5)
)

# Basic word cloud with word, count, and score columns
WordCloudPlot(data, word_by = "word",
              count_by = "count", score_by = "score")

# Word cloud using sentence_by (sentences split into words)
data_sent <- data.frame(
    sentence = c("The quick brown fox jumps over the lazy dog",
                 "A quick brown dog jumps over a lazy fox"),
    score = c(10, 5)
)
WordCloudPlot(data_sent, sentence_by = "sentence", score_by = "score")

# Word cloud with faceting
WordCloudPlot(data, word_by = "word",
              count_by = "count", score_by = "score",
              facet_by = "facet")

# Word cloud split by a grouping variable
WordCloudPlot(data, word_by = "word",
              count_by = "count", score_by = "score",
              split_by = "split")
}
}
