View source: R/stat_difference.R
stat_difference | R Documentation |
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.
stat_difference(
mapping = NULL,
data = NULL,
geom = "ribbon",
position = "identity",
...,
levels = c("+", "-"),
na.rm = FALSE,
orientation = NA,
show.legend = NA,
inherit.aes = TRUE
)
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 |
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.
A Layer
object that can be added to a plot.
geom_ribbon()
understands the following aesthetics (required aesthetics are in bold):
x
or y
ymin
or xmin
ymax
or xmax
alpha
colour
fill
group
linetype
linewidth
Learn more about setting these aesthetics in vignette("ggplot2-specs")
.
sign
A factor
with the levels
attribute set to the levels
argument.
When there is a run of more than two 0-difference values, the inner values will be ignored.
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"))
Add the following code to your website.
For more information on customizing the embed code, read Embedding Snippets.