View source: R/stat-referent.r
| stat_referent | R Documentation |
Compute statistics with respect to a reference data set with shared positional variables.
stat_referent(
mapping = NULL,
data = NULL,
geom = "blank",
position = "identity",
referent = NULL,
show.legend = NA,
inherit.aes = TRUE,
...
)
## S3 method for class 'LayerRef'
ggplot_add(object, plot, ...)
mapping |
Set of aesthetic mappings created by |
data |
The data to be displayed in this layer. There are three options: If A A |
geom |
The geometric object to use to display the data for this layer.
When using a
|
position |
A position adjustment to use on the data for this layer. This
can be used in various ways, including to prevent overplotting and
improving the display. The
|
referent |
The reference data set, admitting the same 3 options as
|
show.legend |
logical. Should this layer be included in the legends?
|
inherit.aes |
If |
... |
Additional arguments passed to |
object |
An object to add to the plot |
plot |
The ggplot object to add |
Often in geometric data analysis a statistical transformation applied to data
X will also depend on data Y, for example when drawing the
projections of vectors X onto vectors Y. The stat layer
stat_referent() accepts Y as an argument to the referent argument
and pre-processes them using the existing positional aesthetic mappings to
x and y.
If a function is passed to referent, then the reference data are obtained
by evaluating the function at the primary data. Alongside borrowing the
aesthetic mappings, the evaluation is done during addition via
ggplot2::ggplot_add() of the layer of custom class LayerRef.
The ggproto can be used as a parent to more elaborate statistical
transformations, or the stat can be paired with geoms that expect the
referent argument and use it to position their transformations of X.
It pairs by default to ggplot2::geom_blank() so as to prevent possibly
confusing output.
A ggproto layer.
# simplify the Motor Trends data to two predictors legible at aspect ratio 1
mtcars %>%
transform(hp00 = hp/100) %>%
subset(select = c(mpg, hp00, wt)) ->
subcars
# compute the gradient of `mpg` against these two predictors
lm(mpg ~ hp00 + wt, subcars) %>%
coefficients() %>%
as.list() %>% as.data.frame() ->
grad
# use the gradient as a reference (to no effect in this basic ggproto)
ggplot(subcars, aes(x = hp00, y = wt)) +
coord_equal() +
geom_point() +
stat_referent(referent = grad)
ggplot(subcars, aes(x = hp00, y = wt)) +
coord_equal() +
stat_referent(geom = "point", referent = grad)
# passing a function yields a transformation of the primary data
p <- ggplot(subcars, aes(x = hp00, y = wt)) +
stat_referent(
data = head,
referent = function(d) as.data.frame(lapply(d, mean))
)
b <- ggplot_build(p)
# original data
b@plot@data
# head of original data
b@data[[1]]
# means of original data
b@plot@layers$stat_referent$stat_params$referent
# means of head of original data
as.data.frame(lapply(layer_data(p, 1), mean))
Add the following code to your website.
For more information on customizing the embed code, read Embedding Snippets.