View source: R/stat_ordered_path.R
| stat_ordered_path | R Documentation |
stat_ordered_path() reorders observations along each path using a user-supplied
ordering aesthetic (order_by) before drawing the path. The statistic can be
used to ensure that paths are drawn in a consistent order even when the input
data are not pre-sorted. This is equivalent to reordering the data before
passing it to geom_path().
stat_ordered_path(
mapping = NULL,
data = NULL,
geom = "path",
position = "identity",
show.legend = NA,
inherit.aes = TRUE,
decreasing = TRUE,
na_method = c("drop_na", "drop_group"),
...
)
StatOrderedPath
mapping |
Set of aesthetic mappings created by |
data |
Data frame to be used for this layer. If |
geom |
The geometric object to use to draw the paths. Defaults to |
position |
A position adjustment to use on the data for this layer. This
can be used in various ways, including to prevent overplotting and
improving the display. The
|
show.legend |
Logical or |
inherit.aes |
If |
decreasing |
Logical. If |
na_method |
Character string specifying how to handle missing values in
|
... |
Additional parameters passed on to the underlying geom. |
An object of class StatOrdered (inherits from Stat, ggproto, gg) of length 4.
The statistic expects an order_by aesthetic that supplies the variable used
to order observations within each group.
Missing values order_by are handled according to na_method.
If na_method is "drop_na", missing values are dropped from the data, and the path is
still drawn, but might skipping steps due to missing values. If na_method is "drop_group",
the entire group whose missing values belong is dropped from the data, and the path is not drawn.
Ties in order_by are allowed but trigger a warning and preserve the original
row order for tied values.
Duplicates in order_by are dropped and a warning is issued.
Grouping is controlled via the usual group aesthetic. If a group column
is present in the data, reordering is performed independently within each
group; otherwise, the entire data is treated as a single path.
A ggplot object
A ggplot2 layer that can be added to a plot object.
stat_ordered_path() understands the following aesthetics (required are in
bold).
x
y
order_by
group
alpha, colour, linewidth, linetype, etc. (inherited from ggplot2::geom_path())
library(ggplot2)
library(dplyr)
# Data prep
input_df <- prefviz::aecdop22_transformed |>
filter(DivisionNm %in% c("Higgins", "Monash"))
tern22 <- as_ternable(input_df, ALP:Other)
# Base plot
p <- get_tern_data(tern22, plot_type = "2D") |>
ggplot(aes(x = x1, y = x2)) +
add_ternary_base() +
geom_ternary_region(
aes(fill = after_stat(vertex_labels)),
vertex_labels = tern22$vertex_labels,
alpha = 0.3, color = "grey50",
show.legend = FALSE
) +
geom_point(aes(color = ElectedParty)) +
add_vertex_labels(tern22$simplex_vertices) +
scale_color_manual(
values = c("ALP" = "red", "LNP" = "blue", "Other" = "grey70"),
aesthetics = c("fill", "colour")
)
# Add ordered paths
p +
stat_ordered_path(
aes(group = DivisionNm, order_by = CountNumber, color = ElectedParty))
Add the following code to your website.
For more information on customizing the embed code, read Embedding Snippets.