Skip to content

Commit 4b9c9df

Browse files
committed
Add stat_ksmooth()
1 parent a2d0181 commit 4b9c9df

File tree

5 files changed

+158
-3
lines changed

5 files changed

+158
-3
lines changed

DESCRIPTION

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -263,6 +263,7 @@ Collate:
263263
'stat-ellipse.R'
264264
'stat-function.R'
265265
'stat-identity.R'
266+
'stat-ksmooth.R'
266267
'stat-manual.R'
267268
'stat-qq-line.R'
268269
'stat-qq.R'

NAMESPACE

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -254,6 +254,7 @@ export(StatEcdf)
254254
export(StatEllipse)
255255
export(StatFunction)
256256
export(StatIdentity)
257+
export(StatKsmooth)
257258
export(StatManual)
258259
export(StatQq)
259260
export(StatQqLine)
@@ -701,6 +702,7 @@ export(stat_ecdf)
701702
export(stat_ellipse)
702703
export(stat_function)
703704
export(stat_identity)
705+
export(stat_ksmooth)
704706
export(stat_manual)
705707
export(stat_qq)
706708
export(stat_qq_line)

R/stat-ksmooth.R

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
#' @rdname Stat
2+
#' @format NULL
3+
#' @usage NULL
4+
#' @export
5+
StatKsmooth <- ggproto("StatKsmooth", Stat,
6+
required_aes = c("x", "y"),
7+
compute_group = function(data, scales, kernel = "box", bandwidth = 0.5) {
8+
as.data.frame(ksmooth(x = data$x, y = data$y, kernel = kernel, bandwidth = bandwidth))
9+
}
10+
)
11+
12+
#' Apply kernel regression smoother
13+
#'
14+
#' @export
15+
#' @inheritParams layer
16+
#' @inheritParams geom_point
17+
#' @inheritParams stats::ksmooth
18+
#' @examples
19+
#' ggplot(economics, aes(date, psavert)) +
20+
#' geom_point(alpha = 0.25) +
21+
#' stat_ksmooth(bandwidth = 180, kernel = "normal")
22+
#'
23+
stat_ksmooth <- function(mapping = NULL, data = NULL, geom = "line",
24+
position = "identity", na.rm = FALSE, show.legend = NA,
25+
inherit.aes = TRUE, kernel = "box", bandwidth = 0.5,
26+
...) {
27+
layer(
28+
stat = StatKsmooth, data = data, mapping = mapping, geom = geom,
29+
position = position, show.legend = show.legend, inherit.aes = inherit.aes,
30+
params = list(kernel = kernel, bandwidth = bandwidth, ...)
31+
)
32+
}

man/Stat.Rd

Lines changed: 4 additions & 3 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

man/stat_ksmooth.Rd

Lines changed: 119 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

0 commit comments

Comments
 (0)