-
Notifications
You must be signed in to change notification settings - Fork 165
/
Copy pathstat_mean.R
41 lines (39 loc) · 1.44 KB
/
stat_mean.R
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
#' @include utilities.R
NULL
#' Draw group mean points
#' @description Draw the mean point of each group.
#' @inheritParams ggplot2::layer
#' @param ... other arguments to pass to \code{\link[ggplot2]{geom_point}}.
#' @param na.rm If FALSE (the default), removes missing values with a warning.
#' If TRUE silently removes missing values.
#' @seealso \code{\link{stat_conf_ellipse}}, \code{\link{stat_chull}} and
#' \code{\link{ggscatter}}
#' @examples
#' # Load data
#' data("mtcars")
#' df <- mtcars
#' df$cyl <- as.factor(df$cyl)
#'
#' # Scatter plot with ellipses and group mean points
#' ggscatter(df, x = "wt", y = "mpg",
#' color = "cyl", shape = "cyl", ellipse = TRUE)+
#' stat_mean(aes(color = cyl, shape = cyl), size = 4)
#'
#' @export
stat_mean <- function(mapping = NULL, data = NULL, geom = "point",
position = "identity", na.rm = FALSE, show.legend = NA,
inherit.aes = TRUE, ...) {
layer(
stat = StatMean, data = data, mapping = mapping, geom = geom,
position = position, show.legend = show.legend, inherit.aes = inherit.aes,
params = list(na.rm = na.rm, ...)
)
}
StatMean<- ggproto("StatMean", Stat,
required_aes = c("x", "y"),
compute_group = function(data, scales) {
res <- data.frame(x = mean(data$x, na.rm = TRUE),
y = mean(data$y, na.rm = TRUE))
res
}
)