-
Notifications
You must be signed in to change notification settings - Fork 25
/
geom_slab.Rd
executable file
·315 lines (273 loc) · 16 KB
/
geom_slab.Rd
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
% Generated by roxygen2: do not edit by hand
% Please edit documentation in R/geom_slab.R
\name{geom_slab}
\alias{geom_slab}
\title{Slab (ridge) plot (shortcut geom)}
\usage{
geom_slab(
mapping = NULL,
data = NULL,
stat = "identity",
position = "identity",
...,
orientation = NA,
subscale = "thickness",
normalize = "all",
fill_type = "segments",
subguide = "slab",
na.rm = FALSE,
show.legend = NA,
inherit.aes = TRUE
)
}
\arguments{
\item{mapping}{Set of aesthetic mappings created by \code{\link[ggplot2:aes]{aes()}}. If specified and
\code{inherit.aes = TRUE} (the default), it is combined with the default mapping
at the top level of the plot. You must supply \code{mapping} if there is no plot
mapping.}
\item{data}{The data to be displayed in this layer. There are three
options:
If \code{NULL}, the default, the data is inherited from the plot
data as specified in the call to \code{\link[ggplot2:ggplot]{ggplot()}}.
A \code{data.frame}, or other object, will override the plot
data. All objects will be fortified to produce a data frame. See
\code{\link[ggplot2:fortify]{fortify()}} for which variables will be created.
A \code{function} will be called with a single argument,
the plot data. The return value must be a \code{data.frame}, and
will be used as the layer data. A \code{function} can be created
from a \code{formula} (e.g. \code{~ head(.x, 10)}).}
\item{stat}{The statistical transformation to use on the data for this
layer, either as a \code{ggproto} \code{Geom} subclass or as a string naming the
stat stripped of the \code{stat_} prefix (e.g. \code{"count"} rather than
\code{"stat_count"})}
\item{position}{Position adjustment, either as a string, or the result of a call to a position adjustment function.
Setting this equal to \code{"dodge"} (\code{\link[=position_dodge]{position_dodge()}}) or \code{"dodgejust"} (\code{\link[=position_dodgejust]{position_dodgejust()}}) can be useful if
you have overlapping geometries.}
\item{...}{Other arguments passed to \code{\link[=layer]{layer()}}. These are often aesthetics, used to set an aesthetic
to a fixed value, like \code{colour = "red"} or \code{linewidth = 3} (see \strong{Aesthetics}, below). They may also be
parameters to the paired geom/stat.}
\item{orientation}{Whether this geom is drawn horizontally or vertically. One of:
\itemize{
\item \code{NA} (default): automatically detect the orientation based on how the aesthetics
are assigned. Automatic detection works most of the time.
\item \code{"horizontal"} (or \code{"y"}): draw horizontally, using the \code{y} aesthetic to identify different
groups. For each group, uses the \code{x}, \code{xmin}, \code{xmax}, and \code{thickness} aesthetics to
draw points, intervals, and slabs.
\item \code{"vertical"} (or \code{"x"}): draw vertically, using the \code{x} aesthetic to identify different
groups. For each group, uses the \code{y}, \code{ymin}, \code{ymax}, and \code{thickness} aesthetics to
draw points, intervals, and slabs.
}
For compatibility with the base ggplot naming scheme for \code{orientation}, \code{"x"} can be used as an alias
for \code{"vertical"} and \code{"y"} as an alias for \code{"horizontal"} (\pkg{ggdist} had an \code{orientation} parameter
before base ggplot did, hence the discrepancy).}
\item{subscale}{Sub-scale used to scale values of the \code{thickness} aesthetic within
the groups determined by \code{normalize}. One of:
\itemize{
\item A function that takes an \code{x} argument giving a numeric vector
of values to be scaled and then returns a \link{thickness} vector representing
the scaled values, such as \code{\link[=subscale_thickness]{subscale_thickness()}} or \code{\link[=subscale_identity]{subscale_identity()}}.
\item A string giving the name of such a function when prefixed
with \code{"subscale_"}; e.g. \code{"thickness"} or \code{"identity"}. The value
\code{"thickness"} using the default subscale, which can be modified by
setting \code{\link{subguide_thickness}}; see the documentation for that
function.
}
For a comprehensive discussion and examples of slab scaling and normalization, see the
\href{https://mjskay.github.io/ggdist/articles/thickness.html}{\code{thickness} scale article}.}
\item{normalize}{Groups within which to scale values of the \code{thickness} aesthetic. One of:
\itemize{
\item \code{"all"}: normalize so that the maximum height across all data is \code{1}.
\item \code{"panels"}: normalize within panels so that the maximum height in each panel is \code{1}.
\item \code{"xy"}: normalize within the x/y axis opposite the \code{orientation} of this geom so
that the maximum height at each value of the opposite axis is \code{1}.
\item \code{"groups"}: normalize within values of the opposite axis and within each
group so that the maximum height in each group is \code{1}.
\item \code{"none"}: values are taken as is with no normalization (this should probably
only be used with functions whose values are in [0,1], such as CDFs).
}
For a comprehensive discussion and examples of slab scaling and normalization, see the
\href{https://mjskay.github.io/ggdist/articles/thickness.html}{\code{thickness} scale article}.}
\item{fill_type}{What type of fill to use when the fill color or alpha varies within a slab. One of:
\itemize{
\item \code{"segments"}: breaks up the slab geometry into segments for each unique combination of fill color and
alpha value. This approach is supported by all graphics devices and works well for sharp cutoff values,
but can give ugly results if a large number of unique fill colors are being used (as in gradients,
like in \code{\link[=stat_gradientinterval]{stat_gradientinterval()}}).
\item \code{"gradient"}: a \code{grid::linearGradient()} is used to create a smooth gradient fill. This works well for
large numbers of unique fill colors, but requires R >= 4.1 and is not yet supported on all graphics devices.
As of this writing, the \code{png()} graphics device with \code{type = "cairo"}, the \code{svg()} device, the \code{pdf()}
device, and the \code{ragg::agg_png()} devices are known to support this option. On R < 4.1, this option
will fall back to \code{fill_type = "segments"} with a message.
\item \code{"auto"}: attempts to use \code{fill_type = "gradient"} if support for it can be auto-detected. On R >= 4.2,
support for gradients can be auto-detected on some graphics devices; if support is not detected, this
option will fall back to \code{fill_type = "segments"} (in case of a false negative, \code{fill_type = "gradient"}
can be set explicitly). On R < 4.2, support for gradients cannot be auto-detected, so this will always
fall back to \code{fill_type = "segments"}, in which case you can set \code{fill_type = "gradient"} explicitly
if you are using a graphics device that support gradients.
}}
\item{subguide}{Sub-guide used to annotate the \code{thickness} scale. One of:
\itemize{
\item A function that takes a \code{scale} argument giving a \link[ggplot2:ggplot2-ggproto]{ggplot2::Scale}
object and an \code{orientation} argument giving the orientation of the
geometry and then returns a \link[grid:grid.grob]{grid::grob} that will draw the axis
annotation, such as \code{\link[=subguide_axis]{subguide_axis()}} (to draw a traditional axis) or
\code{\link[=subguide_none]{subguide_none()}} (to draw no annotation). See \code{\link[=subguide_axis]{subguide_axis()}}
for a list of possibilities and examples.
\item A string giving the name of such a function when prefixed
with \code{"subguide_"}; e.g. \code{"axis"} or \code{"none"}. The values \code{"slab"},
\code{"dots"}, and \code{"spike"} use the default subguide for their geom
families (no subguide), which can be modified by setting
\code{\link{subguide_slab}}, \code{\link{subguide_dots}}, or \code{\link{subguide_spike}};
see the documentation for those functions.
}}
\item{na.rm}{If \code{FALSE}, the default, missing values are removed with a warning. If \code{TRUE}, missing
values are silently removed.}
\item{show.legend}{logical. Should this layer be included in the legends?
\code{NA}, the default, includes if any aesthetics are mapped.
\code{FALSE} never includes, and \code{TRUE} always includes.
It can also be a named logical vector to finely select the aesthetics to
display.}
\item{inherit.aes}{If \code{FALSE}, overrides the default aesthetics,
rather than combining with them. This is most useful for helper functions
that define both data and aesthetics and shouldn't inherit behaviour from
the default plot specification, e.g. \code{\link[ggplot2:borders]{borders()}}.}
}
\value{
A \link[ggplot2:ggplot2-ggproto]{ggplot2::Geom} representing a slab (ridge) geometry which can
be added to a \code{\link[=ggplot]{ggplot()}} object.
}
\description{
Shortcut version of \code{\link[=geom_slabinterval]{geom_slabinterval()}} for creating slab (ridge) plots.
\strong{Roughly equivalent to:}
\preformatted{geom_slabinterval(
show_point = FALSE,
show_interval = FALSE
)}
}
\section{Aesthetics}{
The slab+interval \code{stat}s and \code{geom}s have a wide variety of aesthetics that control
the appearance of their three sub-geometries: the \strong{slab}, the \strong{point}, and
the \strong{interval}.
\strong{Positional aesthetics}
\itemize{
\item{\code{x}: x position of the geometry}
\item{\code{y}: y position of the geometry}
}
\strong{Slab-specific aesthetics}
\itemize{
\item{\code{thickness}: The thickness of the slab at each \code{x} value (if \code{orientation = "horizontal"}) or
\code{y} value (if \code{orientation = "vertical"}) of the slab.}
\item{\code{side}: Which side to place the slab on. \code{"topright"}, \code{"top"}, and \code{"right"} are synonyms
which cause the slab to be drawn on the top or the right depending on if \code{orientation} is \code{"horizontal"}
or \code{"vertical"}. \code{"bottomleft"}, \code{"bottom"}, and \code{"left"} are synonyms which cause the slab
to be drawn on the bottom or the left depending on if \code{orientation} is \code{"horizontal"} or
\code{"vertical"}. \code{"topleft"} causes the slab to be drawn on the top or the left, and \code{"bottomright"}
causes the slab to be drawn on the bottom or the right. \code{"both"} draws the slab mirrored on both
sides (as in a violin plot).}
\item{\code{scale}: What proportion of the region allocated to this geom to use to draw the slab. If \code{scale = 1},
slabs that use the maximum range will just touch each other. Default is \code{0.9} to leave some space
between adjacent slabs. For a comprehensive discussion and examples of slab scaling and normalization,
see the \href{https://mjskay.github.io/ggdist/articles/thickness.html}{\code{thickness} scale article}.}
\item{\code{justification}: Justification of the interval relative to the slab, where \code{0} indicates bottom/left
justification and \code{1} indicates top/right justification (depending on \code{orientation}). If \code{justification}
is \code{NULL} (the default), then it is set automatically based on the value of \code{side}: when \code{side} is
\code{"top"}/\code{"right"} \code{justification} is set to \code{0}, when \code{side} is \code{"bottom"}/\code{"left"}
\code{justification} is set to \code{1}, and when \code{side} is \code{"both"} \code{justification} is set to 0.5.}
}
\strong{Color aesthetics}
\itemize{
\item{\code{colour}: (or \code{color}) The color of the \strong{interval} and \strong{point} sub-geometries.
Use the \code{slab_color}, \code{interval_color}, or \code{point_color} aesthetics (below) to
set sub-geometry colors separately.}
\item{\code{fill}: The fill color of the \strong{slab} and \strong{point} sub-geometries. Use the \code{slab_fill}
or \code{point_fill} aesthetics (below) to set sub-geometry colors separately.}
\item{\code{alpha}: The opacity of the \strong{slab}, \strong{interval}, and \strong{point} sub-geometries. Use the \code{slab_alpha},
\code{interval_alpha}, or \code{point_alpha} aesthetics (below) to set sub-geometry colors separately.}
\item{\code{colour_ramp}: (or \code{color_ramp}) A secondary scale that modifies the \code{color}
scale to "ramp" to another color. See \code{\link[=scale_colour_ramp]{scale_colour_ramp()}} for examples.}
\item{\code{fill_ramp}: A secondary scale that modifies the \code{fill}
scale to "ramp" to another color. See \code{\link[=scale_fill_ramp]{scale_fill_ramp()}} for examples.}
}
\strong{Line aesthetics}
\itemize{
\item{\code{linewidth}: Width of the line used to draw the \strong{interval} (except with \code{\link[=geom_slab]{geom_slab()}}: then
it is the width of the \strong{slab}). With composite geometries including an interval and slab,
use \code{slab_linewidth} to set the line width of the \strong{slab} (see below). For \strong{interval}, raw
\code{linewidth} values are transformed according to the \code{interval_size_domain} and \code{interval_size_range}
parameters of the \code{geom} (see above).}
\item{\code{size}: Determines the size of the \strong{point}. If \code{linewidth} is not provided, \code{size} will
also determines the width of the line used to draw the \strong{interval} (this allows line width and
point size to be modified together by setting only \code{size} and not \code{linewidth}). Raw
\code{size} values are transformed according to the \code{interval_size_domain}, \code{interval_size_range},
and \code{fatten_point} parameters of the \code{geom} (see above). Use the \code{point_size} aesthetic
(below) to set sub-geometry size directly without applying the effects of
\code{interval_size_domain}, \code{interval_size_range}, and \code{fatten_point}.}
\item{\code{stroke}: Width of the outline around the \strong{point} sub-geometry.}
\item{\code{linetype}: Type of line (e.g., \code{"solid"}, \code{"dashed"}, etc) used to draw the \strong{interval}
and the outline of the \strong{slab} (if it is visible). Use the \code{slab_linetype} or
\code{interval_linetype} aesthetics (below) to set sub-geometry line types separately.}
}
\strong{Slab-specific color and line override aesthetics}
\itemize{
\item{\code{slab_fill}: Override for \code{fill}: the fill color of the slab.}
\item{\code{slab_colour}: (or \code{slab_color}) Override for \code{colour}/\code{color}: the outline color of the slab.}
\item{\code{slab_alpha}: Override for \code{alpha}: the opacity of the slab.}
\item{\code{slab_linewidth}: Override for \code{linwidth}: the width of the outline of the slab.}
\item{\code{slab_linetype}: Override for \code{linetype}: the line type of the outline of the slab.}
}
\strong{Deprecated aesthetics}
\itemize{
\item{\code{slab_size}: Use \code{slab_linewidth}.}
}
\strong{Other aesthetics} (these work as in standard \code{geom}s)
\itemize{
\item{\code{width}}
\item{\code{height}}
\item{\code{group}}
}
See examples of some of these aesthetics in action in \code{vignette("slabinterval")}.
Learn more about the sub-geom override aesthetics (like \code{interval_color}) in the
\link[ggdist]{scales} documentation. Learn more about basic ggplot aesthetics in
\code{vignette("ggplot2-specs")}.
}
\examples{
library(dplyr)
library(ggplot2)
theme_set(theme_ggdist())
# we will manually demonstrate plotting a density with geom_slab(),
# though generally speaking this is easier to do using stat_slab(), which
# will determine sensible limits automatically and correctly adjust
# densities when using scale transformations
df = expand.grid(
mean = 1:3,
input = seq(-2, 6, length.out = 100)
) \%>\%
mutate(
group = letters[4 - mean],
density = dnorm(input, mean, 1)
)
# orientation is detected automatically based on
# use of x or y
df \%>\%
ggplot(aes(y = group, x = input, thickness = density)) +
geom_slab()
df \%>\%
ggplot(aes(x = group, y = input, thickness = density)) +
geom_slab()
# RIDGE PLOTS
# "ridge" plots can be created by increasing the slab height and
# setting the slab color
df \%>\%
ggplot(aes(y = group, x = input, thickness = density)) +
geom_slab(height = 2, color = "black")
}
\seealso{
See \code{\link[=stat_slab]{stat_slab()}} for the stat version, intended for
use on sample data or analytical distributions.
See \code{\link[=geom_slabinterval]{geom_slabinterval()}} for the geometry this shortcut is based on.
Other slabinterval geoms:
\code{\link{geom_interval}()},
\code{\link{geom_pointinterval}()},
\code{\link{geom_spike}()}
}
\concept{slabinterval geoms}