stat_difference {ggh4x} | R Documentation |
Difference ribbon
Description
This makes a ribbon that is filled depending on whether the max
is
higher than min
. This can be useful for displaying differences
between two series.
Usage
stat_difference(
mapping = NULL,
data = NULL,
geom = "ribbon",
position = "identity",
...,
levels = c("+", "-"),
na.rm = FALSE,
orientation = NA,
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 connection between
|
position |
Position adjustment, either as a string naming the adjustment
(e.g. |
... |
Other arguments passed on to |
levels |
A |
na.rm |
If |
orientation |
The orientation of the layer. The default ( |
show.legend |
logical. Should this layer be included in the legends?
|
inherit.aes |
If |
Details
The stat may reorder the group
aesthetic to accommodate two
different fills for the signs of differences. The stat takes care to
interpolate a series whenever a crossover between max
and min
series
happens. This makes the ribbon not look stumpy at these crossovers.
Value
A Layer
object that can be added to a plot.
Aesthetics
geom_ribbon()
understands the following aesthetics (required aesthetics are in bold):
-
x
ory
-
ymin
orxmin
-
ymax
orxmax
-
alpha
-
colour
-
fill
-
group
-
linetype
-
linewidth
Learn more about setting these aesthetics in vignette("ggplot2-specs")
.
Computed variables
sign
A
factor
with thelevels
attribute set to thelevels
argument.
Note
When there is a run of more than two 0-difference values, the inner values will be ignored.
Examples
set.seed(2021)
df <- data.frame(
x = 1:100,
y = cumsum(rnorm(100)),
z = cumsum(rnorm(100))
)
ggplot(df, aes(x = x)) +
stat_difference(aes(ymin = y, ymax = z), alpha = 0.3) +
geom_line(aes(y = y, colour = "min")) +
geom_line(aes(y = z, colour = "max"))