stat_rollingkernel {ggh4x} | R Documentation |
Rolling Kernel
Description
A rolling kernel moves along one of the axes and assigns weights to datapoints depending on the distance to the kernel's location. It then calculates a weighted average on the y-values of the datapoints, creating a trendline. In contrast to (weighted) rolling averages, the interval between datapoints do not need to be constant.
Usage
stat_rollingkernel(
mapping = NULL,
data = NULL,
geom = "line",
position = "identity",
...,
bw = "nrd",
kernel = "gaussian",
n = 256,
expand = 0.1,
na.rm = FALSE,
orientation = "x",
show.legend = NA,
inherit.aes = TRUE
)
Arguments
mapping |
Set of aesthetic mappings created by |
data |
The data to be displayed in this layer. There are three options: If A A |
geom |
Use to override the default geom ( |
position |
Position adjustment, either as a string naming the adjustment
(e.g. |
... |
Other arguments passed on to |
bw |
A bandwidth, which can be one of the following:
|
kernel |
One of the following:
|
n |
An |
expand |
A |
na.rm |
If |
orientation |
A |
show.legend |
logical. Should this layer be included in the legends?
|
inherit.aes |
If |
Value
A Layer ggproto object.
Aesthetics
stat_rollingkernel()
understands the following
aesthetics (required aesthetics are in bold)
-
x
-
y
group
Computed variables
x
A sequence of ordered x positions.
y
The weighted value of the rolling kernel.
weight
The sum of weight strengths at a position.
scaled
The fraction of weight strengths at a position. This is the same as
weight / sum(weight)
by group.
Examples
ggplot(mpg, aes(displ, hwy, colour = class)) +
geom_point() +
stat_rollingkernel()
# The (scaled) weights can be used to emphasise data-dense areas
ggplot(mpg, aes(displ, hwy, colour = class)) +
geom_point() +
stat_rollingkernel(aes(alpha = after_stat(scaled)))