geom_spike {ggdist} | R Documentation |
Spike plot (ggplot2 geom)
Description
Geometry for drawing "spikes" (optionally with points on them) on top of
geom_slabinterval()
geometries: this geometry understands the scaling and
positioning of the thickness
aesthetic from geom_slabinterval()
, which
allows you to position spikes and points along a slab.
Usage
geom_spike(
mapping = NULL,
data = NULL,
stat = "identity",
position = "identity",
...,
orientation = NA,
normalize = "all",
arrow = NULL,
subguide = "none",
na.rm = FALSE,
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 |
stat |
The statistical transformation to use on the data for this
layer, either as a |
position |
Position adjustment, either as a string, or the result of a call to a position adjustment function.
Setting this equal to |
... |
Other arguments passed to |
orientation |
Whether this geom is drawn horizontally or vertically. One of:
For compatibility with the base ggplot naming scheme for |
normalize |
How to normalize heights of functions input to the
For a comprehensive discussion and examples of slab scaling and normalization, see the
|
arrow |
|
subguide |
Sub-guide used to annotate the
|
na.rm |
If |
show.legend |
logical. Should this layer be included in the legends?
|
inherit.aes |
If |
Details
This geometry consists of a "spike" (vertical/horizontal line segment) and a
"point" (at the end of the line segment). It uses the thickness
aesthetic
to determine where the endpoint of the line is, which allows it to be used
with geom_slabinterval()
geometries for labeling specific values of the
thickness function.
Value
A ggplot2::Geom representing a spike geometry which can
be added to a ggplot()
object.
rd_slabinterval_aesthetics(geom_name),
Aesthetics
The spike geom
has a wide variety of aesthetics that control
the appearance of its two sub-geometries: the spike and the point.
Positional aesthetics
x
: x position of the geometryy
: y position of the geometry
Spike-specific (aka Slab-specific) aesthetics
thickness
: The thickness of the slab at eachx
value (iforientation = "horizontal"
) ory
value (iforientation = "vertical"
) of the slab.side
: Which side to place the slab on."topright"
,"top"
, and"right"
are synonyms which cause the slab to be drawn on the top or the right depending on iforientation
is"horizontal"
or"vertical"
."bottomleft"
,"bottom"
, and"left"
are synonyms which cause the slab to be drawn on the bottom or the left depending on iforientation
is"horizontal"
or"vertical"
."topleft"
causes the slab to be drawn on the top or the left, and"bottomright"
causes the slab to be drawn on the bottom or the right."both"
draws the slab mirrored on both sides (as in a violin plot).scale
: What proportion of the region allocated to this geom to use to draw the slab. Ifscale = 1
, slabs that use the maximum range will just touch each other. Default is0.9
to leave some space between adjacent slabs. For a comprehensive discussion and examples of slab scaling and normalization, see thethickness
scale article.
Color aesthetics
colour
: (orcolor
) The color of the spike and point sub-geometries.fill
: The fill color of the point sub-geometry.alpha
: The opacity of the spike and point sub-geometries.colour_ramp
: (orcolor_ramp
) A secondary scale that modifies thecolor
scale to "ramp" to another color. Seescale_colour_ramp()
for examples.fill_ramp
: A secondary scale that modifies thefill
scale to "ramp" to another color. Seescale_fill_ramp()
for examples.
Line aesthetics
linewidth
: Width of the line used to draw the spike sub-geometry.size
: Size of the point sub-geometry.stroke
: Width of the outline around the point sub-geometry.linetype
: Type of line (e.g.,"solid"
,"dashed"
, etc) used to draw the spike.
Other aesthetics (these work as in standard geom
s)
width
height
group
See examples of some of these aesthetics in action in vignette("slabinterval")
.
Learn more about the sub-geom override aesthetics (like interval_color
) in the
scales documentation. Learn more about basic ggplot aesthetics in
vignette("ggplot2-specs")
.
See Also
See stat_spike()
for the stat version, intended for
use on sample data or analytical distributions.
Other slabinterval geoms:
geom_interval()
,
geom_pointinterval()
,
geom_slab()
Examples
library(ggplot2)
library(distributional)
library(dplyr)
# geom_spike is easiest to use with distributional or
# posterior::rvar objects
df = tibble(
d = dist_normal(1:2, 1:2), g = c("a", "b")
)
# annotate the density at the mean of a distribution
df %>% mutate(
mean = mean(d),
density(d, list(density_at_mean = mean))
) %>%
ggplot(aes(y = g)) +
stat_slab(aes(xdist = d)) +
geom_spike(aes(x = mean, thickness = density_at_mean)) +
# need shared thickness scale so that stat_slab and geom_spike line up
scale_thickness_shared()
# annotate the endpoints of intervals of a distribution
# here we'll use an arrow instead of a point by setting size = 0
arrow_spec = arrow(angle = 45, type = "closed", length = unit(4, "pt"))
df %>% mutate(
median_qi(d, .width = 0.9),
density(d, list(density_lower = .lower, density_upper = .upper))
) %>%
ggplot(aes(y = g)) +
stat_halfeye(aes(xdist = d), .width = 0.9, color = "gray35") +
geom_spike(
aes(x = .lower, thickness = density_lower),
size = 0, arrow = arrow_spec, color = "blue", linewidth = 0.75
) +
geom_spike(
aes(x = .upper, thickness = density_upper),
size = 0, arrow = arrow_spec, color = "red", linewidth = 0.75
) +
scale_thickness_shared()