| JitterPlotAtomic | R Documentation |
Core implementation for drawing a single jittered point plot. This is the
workhorse behind the exported JitterPlot function — it takes a
single data frame (no split_by support) and returns a
ggplot object. The plot displays individual data points with random
jitter along the x-axis (and optionally the y-axis) to reveal the
distribution of values within each x category, making it especially useful
for visualising overlapping discrete data.
The function provides extensive annotation features including point labelling
(via geom_text_repel with automatic top-n selection
using a configurable distance metric), point highlighting, optional dodging
via group_by, and horizontal reference lines. It supports both long
and wide input formats, quantile-based axis limits, and x-axis reordering by
y-value summaries (mean or median).
JitterPlotAtomic(
data,
x,
x_sep = "_",
y = NULL,
in_form = c("long", "wide"),
keep_na = FALSE,
keep_empty = FALSE,
sort_x = c("none", "mean_asc", "mean_desc", "mean", "median_asc", "median_desc",
"median"),
flip = FALSE,
group_by = NULL,
group_by_sep = "_",
group_name = NULL,
x_text_angle = 0,
order_by = "-({y}^2 + {size_by}^2)",
theme = "theme_this",
theme_args = list(),
palette = "Paired",
palcolor = NULL,
palreverse = FALSE,
alpha = 1,
aspect.ratio = NULL,
legend.position = "right",
legend.direction = "vertical",
shape = 21,
border = "black",
size_by = 2,
size_name = NULL,
size_trans = NULL,
y_nbreaks = 4,
jitter_width = 0.5,
jitter_height = 0,
y_max = NULL,
y_min = NULL,
y_trans = "identity",
add_bg = FALSE,
bg_palette = "stripe",
bg_palcolor = NULL,
bg_alpha = 0.2,
add_hline = NULL,
hline_type = "solid",
hline_width = 0.5,
hline_color = "black",
hline_alpha = 1,
labels = NULL,
label_by = NULL,
nlabel = 5,
label_size = 3,
label_fg = "black",
label_bg = "white",
label_bg_r = 0.1,
highlight = NULL,
highlight_color = "red2",
highlight_size = 1,
highlight_alpha = 1,
facet_by = NULL,
facet_scales = "fixed",
facet_ncol = NULL,
facet_nrow = NULL,
facet_byrow = TRUE,
title = NULL,
subtitle = NULL,
xlab = NULL,
ylab = NULL,
seed = 8525,
...
)
data |
A data frame. |
x |
A character string specifying the column name for the x-axis.
Must be character or factor. Multiple columns can be provided; they are
concatenated with |
x_sep |
A character string used to join multiple |
y |
A character string specifying the numeric column for the y-axis.
Required when |
in_form |
A character string specifying the input data format. Either
|
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 |
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
|
sort_x |
A character string controlling x-axis level reordering by
y-value summaries. One of |
flip |
A logical value. When |
group_by |
A character vector of column names for dodging the points.
Each unique combination becomes a separate dodge group and the points are
offset horizontally via |
group_by_sep |
A character string used to join multiple
|
group_name |
A character string for the dodge-group legend title.
When |
x_text_angle |
A numeric value specifying the angle of the x-axis text. |
order_by |
A string expression passed to |
theme |
A character string or a theme class (i.e. ggplot2::theme_classic) specifying the theme to use. Default is "theme_this". |
theme_args |
A list of arguments to pass to the theme function. |
palette |
A character string specifying the palette to use.
A named list or vector can be used to specify the palettes for different |
palcolor |
A character string specifying the color to use in the palette.
A named list can be used to specify the colors for different |
palreverse |
A logical value indicating whether to reverse the palette. Default is FALSE. |
alpha |
A numeric value in |
aspect.ratio |
A numeric value specifying the aspect ratio of the plot. |
legend.position |
A character string specifying the position of the legend.
if |
legend.direction |
A character string specifying the direction of the legend. |
shape |
A numeric value specifying the point shape (ggplot2 point
shape codes). Shapes 21–25 are filled shapes with borders; for these
shapes the border behaviour is controlled by |
border |
Controls the border of points when the shape has a border
(21–25). If |
size_by |
A numeric column name or a single numeric value controlling
point size. When a column name is provided, sizes are scaled using
|
size_name |
A character string for the size legend title. When
|
size_trans |
A function or a function name (as a string) to transform
the |
y_nbreaks |
A numeric value hinting at the number of break intervals
for the y-axis. Passed to |
jitter_width |
A numeric value controlling the amount of horizontal
jitter (in x-axis units). Passed to |
jitter_height |
A numeric value controlling the amount of vertical
jitter (in y-axis units). Passed to |
y_max, y_min |
Numeric values or quantile strings (e.g. |
y_trans |
A character string specifying a transformation for the
y-axis (e.g. |
add_bg |
A logical value. When |
bg_palette |
A character string specifying the palette for the
background stripe colours. Passed to |
bg_palcolor |
A character vector of colours for the background
stripes. Passed to |
bg_alpha |
A numeric value in |
add_hline |
One or more numeric values specifying y-values at which
to draw horizontal reference lines. When |
hline_type |
A character string specifying the line type for the
horizontal reference line(s). Default: |
hline_width |
A numeric value specifying the line width for the
horizontal reference line(s). Default: |
hline_color |
A character string specifying the colour for the
horizontal reference line(s). Default: |
hline_alpha |
A numeric value in |
labels |
A vector of row names or row indices specifying which points
to label. When |
label_by |
A character string naming a column whose values are used
as label text. When |
nlabel |
An integer specifying the number of points to label per
x-group when |
label_size, label_fg, label_bg, label_bg_r |
Label aesthetics for
|
highlight |
A specification of which points to highlight. Can be:
|
highlight_color |
A character string specifying the colour of
highlighted points. Default: |
highlight_size |
A numeric value specifying the size of highlighted
points. Default: |
highlight_alpha |
A numeric value in |
facet_by |
A character string specifying the column name of the data frame to facet the plot.
Otherwise, the data will be split by |
facet_scales |
Whether to scale the axes of facets. Default is "fixed"
Other options are "free", "free_x", "free_y". See |
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. |
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. |
facet_byrow |
A logical value indicating whether to fill the plots by row. Default is TRUE. |
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. |
subtitle |
A character string specifying the subtitle of the plot. |
xlab |
A character string specifying the x-axis label. |
ylab |
A character string specifying the y-axis label. |
seed |
The random seed to use. Default is 8525. |
... |
Additional arguments. |
A ggplot object with height and width
attributes (in inches) attached.
ggplot dispatch — selects gglogger::ggplot or
ggplot2::ggplot based on
getOption("plotthis.gglogger.enabled").
in_form handling — when in_form = "wide", the x
columns are pivoted to long format via
pivot_longer(), creating a key column
.x and a value column .y.
Column resolution — x, y, group_by,
and facet_by are validated and transformed via
check_columns. Multi-column inputs for x and
group_by are concatenated into single columns using their
respective separators (x_sep, group_by_sep).
NA / empty-level handling — process_keep_na_empty()
applies keep_na and keep_empty policies. Per-column
keep_empty settings are extracted for x,
group_by, and facet_by independently. When
facet_by has more than one column, the keep_empty
values for all facet columns must be identical.
sort_x resolution — match.arg() normalises the
sort_x argument, and per-group (x, group_by,
facet_by) means and medians of y are computed for
subsequent reordering.
y-axis limit computation — y_max and y_min
support raw numeric values or quantile strings (e.g. "q95"),
which are resolved via quantile(). These
limits are used for fixed coord_cartesian / coord_flip.
Highlight flag — when highlight is provided, a
logical column .highlight is created. Values can be
TRUE (all points), a numeric vector of row indices, a single
character string parsed as an R expression, or a character vector
matched against row names.
Label preparation — labels are resolved from
label_by or row names. When labels is NULL
and nlabel > 0, the top nlabel points per x-group are
selected by descending order of order_by (default:
-({y}^2 + {size_by}^2), the radial distance from zero in
y-size space, analogous to VolcanoPlot). When facet_by is
active, the ranking is nested within each facet panel.
x-axis reordering — when sort_x is not
"none", the x-axis factor levels are reordered by the
per-group mean or median via reorder().
Flip handling — when flip = TRUE, the x-axis
factor levels are reversed and the aspect ratio is inverted.
Colour assignment — palette_this() assigns
colours to the levels of group_by (or x when
group_by is NULL), including NA levels which
are relabelled as "NA".
Background layer — when add_bg = TRUE, alternating
background stripes are drawn via bg_layer() using the
x-axis level order.
Position jitter — when group_by is NULL,
position_jitter() is used. When
group_by is provided, position_jitterdodge()
is used so points are dodged by group before jittering.
Pre-compute jittered positions — a temporary ggplot
is built and rendered via ggplot_build() to extract the
actual jittered coordinates. These are stored as .x_jittered
and .y_jittered columns and used by the label layer because
geom_text_repel does not natively respect
position_jitter / position_jitterdodge.
Point layer — geom_point() is built with the
appropriate aesthetic mapping: fill (for shapes 21–25) or colour
(for other shapes) mapped to the grouping variable. Border
behaviour for shapes 21–25 is controlled by border
(constant string, mapped to group colour, or NA). When
size_by is a column, size is mapped via aes(size).
Discrete colour/fill scales —
scale_fill_manual() or scale_color_manual() with the
assigned palette colours. When keep_empty is TRUE
for the colour column, drop = FALSE and explicit
breaks / limits preserve empty levels. An additional
scale_color_manual() is added for border colour when
border = TRUE.
Size scale — when size_by is a column,
scale_size_area(max_size = 6) is applied. If
size_trans is provided, the legend labels show the original
(untransformed) values while the mapping uses transformed values.
Highlight overlay — if any points are highlighted, a
second geom_point() layer is drawn on top using the
highlight_color, highlight_size, and
highlight_alpha settings. The highlight layer does not
affect the legend.
Label layer — if any points are selected for labelling,
geom_text_repel() is rendered at the
pre-computed jittered coordinates.
Horizontal reference lines — when add_hline is
provided, geom_hline() is drawn using the specified line
aesthetics.
Axes and labels — scale_x_discrete() (with
drop = !isTRUE(keep_empty_x)),
scale_y_continuous(trans = y_trans, n.breaks = y_nbreaks),
and labs() set the axis scale properties and titles.
Coordinate system — when flip = TRUE,
coord_flip() is used with (or without) fixed y-limits
depending on free-scale faceting. When flip = FALSE,
coord_cartesian() sets the y-axis limits unless free scales
are in use.
Theme — do_call(theme, theme_args) applies the
selected theme, with additional aspect.ratio, x-axis text
rotation, legend position, and legend direction adjustments.
Panel grid lines are styled: major y-axis lines (dashed grey) for
non-flipped plots; major x-axis lines for flipped plots.
Dimension calculation — calculate_plot_dimensions()
computes plot height and width from the x-axis category count,
aspect.ratio, dodge group count, legend metrics, and label
character width. The minimum dimensions account for x-axis label
length. The resulting height / width attributes are
stored on the ggplot object. For flipped plots, the width
is set to at least max(width, height).
Faceting — facet_plot() wraps the plot
with facet_wrap / facet_grid if facet_by is
provided, respecting the keep_empty setting for facet
variables.
Add the following code to your website.
For more information on customizing the embed code, read Embedding Snippets.