Skip to content

Commit 9ca4353

Browse files
committed
apply boilerplate to stat_connect()
1 parent 9039e10 commit 9ca4353

File tree

1 file changed

+36
-60
lines changed

1 file changed

+36
-60
lines changed

R/stat-connect.R

Lines changed: 36 additions & 60 deletions
Original file line numberDiff line numberDiff line change
@@ -1,63 +1,3 @@
1-
#' Connect observations
2-
#'
3-
#' Connect successive points with lines of different shapes.
4-
#'
5-
#' @inheritParams layer
6-
#' @inheritParams geom_point
7-
#' @param connection A specification of how two points are connected. Can be one
8-
#' of the folloing:
9-
#' * A string giving a named connection. These options are:
10-
#' * `"hv"` to first jump horizontally, then vertically.
11-
#' * `"vh"` to first jump vertically, then horizontally.
12-
#' * `"mid"` to step half-way between adjacent x-values.
13-
#' * `"linear"` to use a straight segment.
14-
#' * A numeric matrix with two columns giving x and y coordinates respectively.
15-
#' The coordinates should describe points on a path that connect point A
16-
#' at location (0, 0) and point B at location (1, 1). At least one of these
17-
#' two points is expected to be included in the coordinates.
18-
#'
19-
#' @eval rd_aesthetics("stat", "connect")
20-
#' @export
21-
#'
22-
#' @examples
23-
#' ggplot(head(economics, 20), aes(date, unemploy)) +
24-
#' stat_connect(connection = "hv")
25-
#'
26-
#' # Setup custom connections
27-
#' x <- seq(0, 1, length.out = 20)[-1]
28-
#' smooth <- cbind(x, scales::rescale(1 / (1 + exp(-(x * 10 - 5)))))
29-
#' zigzag <- cbind(c(0.4, 0.6, 1), c(0.75, 0.25, 1))
30-
#'
31-
#' ggplot(head(economics, 10), aes(date, unemploy)) +
32-
#' geom_point() +
33-
#' stat_connect(aes(colour = "zigzag"), connection = zigzag) +
34-
#' stat_connect(aes(colour = "smooth"), connection = smooth)
35-
stat_connect <- function(
36-
mapping = NULL,
37-
data = NULL,
38-
geom = "path",
39-
position = "identity",
40-
...,
41-
connection = "hv",
42-
na.rm = FALSE,
43-
show.legend = NA,
44-
inherit.aes = TRUE) {
45-
layer(
46-
data = data,
47-
mapping = mapping,
48-
stat = StatConnect,
49-
geom = geom,
50-
position = position,
51-
show.legend = show.legend,
52-
inherit.aes = inherit.aes,
53-
params = list2(
54-
na.rm = na.rm,
55-
connection = connection,
56-
...
57-
)
58-
)
59-
}
60-
611
#' @rdname ggplot2-ggproto
622
#' @format NULL
633
#' @usage NULL
@@ -160,3 +100,39 @@ StatConnect <- ggproto(
160100
}
161101

162102
)
103+
104+
#' Connect observations
105+
#'
106+
#' Connect successive points with lines of different shapes.
107+
#'
108+
#' @inheritParams layer
109+
#' @inheritParams geom_point
110+
#' @param connection A specification of how two points are connected. Can be one
111+
#' of the folloing:
112+
#' * A string giving a named connection. These options are:
113+
#' * `"hv"` to first jump horizontally, then vertically.
114+
#' * `"vh"` to first jump vertically, then horizontally.
115+
#' * `"mid"` to step half-way between adjacent x-values.
116+
#' * `"linear"` to use a straight segment.
117+
#' * A numeric matrix with two columns giving x and y coordinates respectively.
118+
#' The coordinates should describe points on a path that connect point A
119+
#' at location (0, 0) and point B at location (1, 1). At least one of these
120+
#' two points is expected to be included in the coordinates.
121+
#'
122+
#' @eval rd_aesthetics("stat", "connect")
123+
#' @export
124+
#'
125+
#' @examples
126+
#' ggplot(head(economics, 20), aes(date, unemploy)) +
127+
#' stat_connect(connection = "hv")
128+
#'
129+
#' # Setup custom connections
130+
#' x <- seq(0, 1, length.out = 20)[-1]
131+
#' smooth <- cbind(x, scales::rescale(1 / (1 + exp(-(x * 10 - 5)))))
132+
#' zigzag <- cbind(c(0.4, 0.6, 1), c(0.75, 0.25, 1))
133+
#'
134+
#' ggplot(head(economics, 10), aes(date, unemploy)) +
135+
#' geom_point() +
136+
#' stat_connect(aes(colour = "zigzag"), connection = zigzag) +
137+
#' stat_connect(aes(colour = "smooth"), connection = smooth)
138+
stat_connect <- make_constructor(StatConnect, geom = "path")

0 commit comments

Comments
 (0)