stat_manual | R Documentation |
stat_manual()
takes a function that computes a data transformation for
every group.
stat_manual(
mapping = NULL,
data = NULL,
geom = "point",
position = "identity",
...,
fun = identity,
args = list(),
na.rm = FALSE,
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 |
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
|
... |
Other arguments passed on to
|
fun |
Function that takes a data frame as input and returns a data
frame or data frame-like list as output. The default ( |
args |
A list of arguments to pass to the function given in |
na.rm |
If |
show.legend |
logical. Should this layer be included in the legends?
|
inherit.aes |
If |
stat_manual()
understands the following aesthetics (required aesthetics are in bold):
group
Learn more about setting these aesthetics in vignette("ggplot2-specs")
.
Input aesthetics are determined by the fun
argument. Output aesthetics must
include those required by geom
. Any aesthetic that is constant within a
group will be preserved even if dropped by fun
.
# A standard scatterplot
p <- ggplot(mtcars, aes(disp, mpg, colour = factor(cyl))) +
geom_point()
# The default just displays points as-is
p + stat_manual()
# Using a custom function
make_hull <- function(data) {
hull <- chull(x = data$x, y = data$y)
data.frame(x = data$x[hull], y = data$y[hull])
}
p + stat_manual(
geom = "polygon",
fun = make_hull,
fill = NA
)
# Using the `with` function with quoting
p + stat_manual(
fun = with,
args = list(expr = quote({
hull <- chull(x, y)
list(x = x[hull], y = y[hull])
})),
geom = "polygon", fill = NA
)
# Using the `transform` function with quoting
p + stat_manual(
geom = "segment",
fun = transform,
args = list(
xend = quote(mean(x)),
yend = quote(mean(y))
)
)
# Using dplyr verbs with `vars()`
if (requireNamespace("dplyr", quietly = TRUE)) {
# Get centroids with `summarise()`
p + stat_manual(
size = 10, shape = 21,
fun = dplyr::summarise,
args = vars(x = mean(x), y = mean(y))
)
# Connect to centroid with `mutate`
p + stat_manual(
geom = "segment",
fun = dplyr::mutate,
args = vars(xend = mean(x), yend = mean(y))
)
# Computing hull with `reframe()`
p + stat_manual(
geom = "polygon", fill = NA,
fun = dplyr::reframe,
args = vars(hull = chull(x, y), x = x[hull], y = y[hull])
)
}
Add the following code to your website.
For more information on customizing the embed code, read Embedding Snippets.