Core implementation for drawing a single radar (or spider) chart. This
is the workhorse behind the exported RadarPlot and
SpiderPlot functions — it takes a single data frame
(no split_by support) and returns a ggplot object.
Radar charts display multivariate data on a two-dimensional polar
coordinate system where each variable (x-axis category) is placed
along a radial axis evenly spaced around the circle. The data values
are connected by lines forming a polygon (or multiple polygons when
group_by is provided). When polygon = FALSE (the
default) the grid is rendered as concentric circles (classic radar
chart); when polygon = TRUE straight polygonal grid lines are
used (spider chart variant).
The polar coordinate system is implemented via a local
coord_radar() helper that extends
ggplot2::CoordPolar with is_linear = TRUE, allowing
discrete x-axis positions to be mapped to evenly spaced angular
coordinates.
Usage
RadarPlotAtomic(
data,
x,
x_sep = "_",
group_by = NULL,
group_by_sep = "_",
y = NULL,
group_name = NULL,
groups = NULL,
scale_y = c("group", "global", "x", "none"),
y_min = 0,
y_max = NULL,
y_nbreaks = 4,
polygon = FALSE,
fill = TRUE,
linewidth = 1,
pt_size = 4,
max_charwidth = 16,
bg_color = "grey80",
bg_alpha = 0.1,
theme = "theme_this",
theme_args = list(),
palette = "Paired",
palcolor = NULL,
palreverse = FALSE,
facet_by = NULL,
facet_scales = "fixed",
facet_ncol = NULL,
facet_nrow = NULL,
facet_byrow = TRUE,
alpha = 0.2,
aspect.ratio = 1,
legend.position = waiver(),
legend.direction = "vertical",
keep_na = FALSE,
keep_empty = FALSE,
title = NULL,
subtitle = NULL,
...
)Arguments
- data
A data frame.
- x
A character string specifying the column name of the data frame to plot on the x-axis (the radial categories). Must be character or factor. Multiple columns can be provided; they are concatenated with
x_sepas the separator.- x_sep
A character string used to join multiple
xcolumns. Default"_". Ignored whenxis a single column.- group_by
A character vector of column names to group the data into separate filled polygons. Each unique combination becomes a distinct polygon layer. Multiple columns are concatenated with
group_by_sep. WhenNULL, a single polygon is drawn with no legend.- group_by_sep
A character string used to join multiple
group_bycolumns. Default"_".- y
A character string specifying the numeric column for the radial axis. When
NULL, the count of observations in each (x,group_by,facet_by) combination is used.- group_name
A character string used as the colour/fill legend title. When
NULL, thegroup_bycolumn name is used.- groups
A character vector of group values (in the
group_bycolumn) to include in the plot. WhenNULL, all groups are included. This can control which groups appear and their legend order. Implieskeep_empty = FALSEfor thegroup_bycolumn: groups not present in the data are not shown in the legend.- scale_y
How should the radial axis be scaled? Default is
"group". Options are"group","global","x", and"none"."group"— scaled to the fraction within each group."global"— scaled to the fraction of the total."x"— scaled to the fraction within each x-axis category."none"— raw counts or values, no scaling.
- y_min
A numeric value setting the minimum of the radial axis. Default
0.- y_max
A numeric value setting the maximum of the radial axis. When
NULL, the maximum data value is used.- y_nbreaks
A numeric value for the number of breaks (concentric grid lines) on the radial axis. Default
4.- polygon
A logical value. When
TRUE, the background grid is drawn as a polygon (spider chart style); whenFALSE(default), concentric circles are used (radar chart style).- fill
A logical value. When
TRUE(default), the data polygons are filled with the group colour. WhenFALSE, only outlines are drawn.- linewidth
A numeric value for the width of the polygon outline lines. Default
1.- pt_size
A numeric value for the size of the data point markers. Default
4.- max_charwidth
A numeric value for the maximum character width of x-axis labels before wrapping. Default
16.- bg_color
A character string specifying the background fill colour. Default
"grey80".- bg_alpha
A numeric value for the transparency of the background fill. Default
0.1.- 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
split_byvalues.- palcolor
A character string specifying the color to use in the palette. A named list can be used to specify the colors for different
split_byvalues. If some values are missing, the values from the palette will be used (palcolor will be NULL for those values).- palreverse
A logical value indicating whether to reverse the palette. Default is FALSE.
- facet_by
A character string specifying the column name of the data frame to facet the plot. Otherwise, the data will be split by
split_byand generate multiple plots and combine them into one usingpatchwork::wrap_plots- facet_scales
Whether to scale the axes of facets. Default is "fixed" Other options are "free", "free_x", "free_y". See
ggplot2::facet_wrap- 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.
- alpha
A numeric value specifying the transparency of the plot.
- aspect.ratio
A numeric value specifying the aspect ratio of the plot.
- legend.position
A character string specifying the position of the legend. if
waiver(), for single groups, the legend will be "none", otherwise "right".- legend.direction
A character string specifying the direction of the legend.
- 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
x,group_by,fill_by, etc.- 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
x,group_by,fill_by, etc.FALSE(default): Drop empty factor levels from the data before plotting.TRUE: Keep empty factor levels and show them as a separate category in the plot."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:levels
- 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.
- ...
Additional arguments.
Architecture
Column resolution —
x,y,group_by, andfacet_byare validated and transformed viacheck_columns. Multi-column inputs forxandgroup_byare concatenated usingx_sepandgroup_by_sep.Group setup — When
group_byisNULL, a dummy.groupfactor is created so polygons still draw. The legend is suppressed. Whengroupsis provided, data is filtered to only those group levels.Count aggregation — When
y = NULL, the count of observations in each unique (x,group_by,facet_by) combination is computed. Factor levels are preserved after aggregation.Proportion scaling — The
scale_yargument controls y-value normalisation:"group"scales within each group,"global"within each facet (or the whole dataset),"x"within each x category, and"none"leaves values raw. Percent labels are used for scaled modes.NA / empty-level handling —
process_keep_na_empty()applieskeep_naandkeep_emptypolicies. Per-columnkeep_emptysettings forx,group_by, andfacet_byare extracted; facet columns must share the same value.Coordinate setup — The local
coord_radar()creates aCoordPolarsubclass withis_linear = TRUE, placing discrete x positions at evenly spaced angles.Y-axis range —
y_min,y_max, andy_nbreaksdetermine the radial axis limits and the number of concentric grid lines.Colour mapping —
palette_this()assigns colours to allgroup_bylevels, includingNA(defaulting to"grey80").Background layer — When
polygon = TRUE, a polygonal background fills the area betweeny_minandy_max. Whenpolygon = FALSE, a smooth circular background is interpolated via 360 sample points.Radial grid lines —
geom_path()draws radial lines fromy_mintoy_maxat each x-axis position, respecting facets.Polygon grid — When
polygon = TRUE, concentric polygonal grid lines are drawn at each break level viageom_polygon().Data rendering —
geom_polygon()draws the filled (or unfilled) polygons for each group.geom_point()adds points at each observation. Radial axis labels are rendered as text at a fixed angular offset.Scale configuration —
scale_y_continuous()sets the radial axis limits and breaks.scale_x_discrete()positions the category labels. Colour scales usescale_fill_manual()andscale_color_manual()withdropcontrolled bykeep_empty_group.Circular grid lines — When
polygon = FALSE, dashed circular grid lines are styled viapanel.grid.major.y.Dimension calculation —
calculate_plot_dimensions()computes plot height and width fromaspect.ratio, legend metrics, and a base height.Faceting —
facet_plot()wraps the plot withfacet_wrap/facet_gridiffacet_byis supplied, respecting thekeep_emptysetting for facet variables.
