-
Notifications
You must be signed in to change notification settings - Fork 7
/
stat-stepribbon.R
104 lines (93 loc) · 3.24 KB
/
stat-stepribbon.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
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
#' Step ribbon statistic
#'
#' Provides stair step values for ribbon plots (Copied this from the
#' https://github.com/hrbrmstr/ggalt version 0.6.0, which is not yet on CRAN.
#' Some minor modifications to the file have been made).
#'
#' @inheritParams ggplot2::geom_ribbon
#' @param geom which geom to use; defaults to \code{ribbon}
#' @param direction \code{hv} for horizontal-vertical steps, \code{vh} for
#' vertical-horizontal steps
#' @references \url{https://groups.google.com/forum/?fromgroups=#!topic/ggplot2/9cFWHaH1CPs}
#' @examples
#' x <- 1:10
#' df <- data.frame(x=x, y=x+10, ymin=x+7, ymax=x+12)
#'
#' # horizontal-vertical steps (default)
#' gg <- ggplot2::ggplot(df, ggplot2::aes(x, y))
#' gg <- gg + ggplot2::geom_ribbon(ggplot2::aes(ymin=ymin, ymax=ymax),
#' stat="stepribbon", fill="#b2b2b2",
#' direction="hv")
#' gg <- gg + ggplot2::geom_step(color="#2b2b2b")
#' gg
#'
#' # vertical-horizontal steps (default)
#' gg <- ggplot2::ggplot(df, ggplot2::aes(x, y))
#' gg <- gg + ggplot2::geom_ribbon(ggplot2::aes(ymin=ymin, ymax=ymax),
#' stat="stepribbon", fill="#b2b2b2",
#' direction="vh")
#' gg <- gg + ggplot2::geom_step(color="#2b2b2b")
#' gg
#'
#' # The same plot calling stat_stepribbon directly
#' gg <- ggplot2::ggplot(df, ggplot2::aes(x, y))
#' gg <- gg + stat_stepribbon(mapping = ggplot2::aes(ymin=ymin, ymax=ymax),
#' fill="#b2b2b2", direction="vh")
#' gg <- gg + ggplot2::geom_step(color="#2b2b2b")
#' gg
#'
#' @export
stat_stepribbon <- function(mapping=NULL, data=NULL, geom="ribbon",
position="identity",
na.rm=FALSE, show.legend=NA, inherit.aes=TRUE,
direction="hv", ...) {
ggplot2::layer(
data = data,
mapping = mapping,
stat = StatStepribbon,
geom = geom,
position = position,
show.legend = show.legend,
inherit.aes = inherit.aes,
params = list(
na.rm = na.rm,
direction = direction,
...
)
)
}
#' @title Stat Step Ribbon
#' @description
#' Provides stair step values for ribbon plots (Copied this from the
#' https://github.com/hrbrmstr/ggalt version 0.6.0, which is not yet on CRAN.
#' Some minor modifications to the file have been made).
#' @format NULL
#' @usage NULL
#' @references \url{https://groups.google.com/forum/?fromgroups=#!topic/ggplot2/9cFWHaH1CPs}
#' @export
StatStepribbon <-
ggplot2::ggproto(
"StatStepRibbon", ggplot2::Stat,
required_aes = c("x", "ymin", "ymax"),
compute_group = function(data, scales, direction="hv",
yvars=c("ymin", "ymax"), ...) {
stairstepn(data=data, direction=direction, yvars=yvars)
}
)
stairstepn <- function(data, direction="hv", yvars="y") {
direction <- match.arg(direction, c("hv", "vh"))
data <- as.data.frame(data)[order(data$x),]
n <- nrow(data)
if (direction == "vh") {
xs <- rep(1:n, each=2)[-2*n]
ys <- c(1, rep( 2:n, each=2))
} else {
ys <- rep(1:n, each=2)[-2*n]
xs <- c(1, rep(2:n, each=2))
}
data.frame(
x=data$x[xs],
data[ys, yvars, drop=FALSE],
data[xs, setdiff(names(data), c("x", yvars)), drop=FALSE]
)
}