-
Notifications
You must be signed in to change notification settings - Fork 180
/
Copy pathggplot-geom_chart.R
236 lines (198 loc) · 10.5 KB
/
ggplot-geom_chart.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
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
#' Plot Financial Charts in ggplot2
#'
#' Financial charts provide visual cues to open, high, low, and close prices.
#' Use [coord_x_date()] to zoom into specific plot regions.
#' The following financial chart geoms are available:
#' \itemize{
#' \item \strong{\href{https://www.investopedia.com/terms/b/barchart.asp}{Bar Chart}}
#' \item \strong{\href{https://www.investopedia.com/terms/c/candlestick.asp}{Candlestick Chart}}
#' }
#'
#' @inheritParams geom_ma
#' @inheritParams ggplot2::geom_linerange
#' @param colour_up,colour_down Select colors to be applied based on price movement
#' from open to close. If `close >= open`, `colour_up` is used. Otherwise,
#' `colour_down` is used. The default is `"darkblue"` and `"red"`, respectively.
#' @param fill_up,fill_down Select fills to be applied based on price movement
#' from open to close. If close >= open, `fill_up` is used. Otherwise,
#' `fill_down` is used. The default is `"darkblue"` and "red", respectively.
#' Only affects `geom_candlestick()`.
#'
#' @section Aesthetics:
#' The following aesthetics are understood (required are in bold):
#' \itemize{
#' \item \strong{`x`}, Typically a date
#' \item \strong{`open`}, Required to be the open price
#' \item \strong{`high`}, Required to be the high price
#' \item \strong{`low`}, Required to be the low price
#' \item \strong{`close`}, Required to be the close price
#' \item `alpha`
#' \item `group`
#' \item `linetype`
#' \item `size`
#' }
#'
#' @seealso See individual modeling functions for underlying parameters:
#' \itemize{
#' \item [geom_ma()] for adding moving averages to ggplots
#' \item [geom_bbands()] for adding Bollinger Bands to ggplots
#' \item [coord_x_date()] for zooming into specific regions of a plot
#' }
#'
#' @name geom_chart
#'
#' @export
#'
#' @examples
#' library(dplyr)
#' library(ggplot2)
#' library(lubridate)
#'
#' AAPL <- tq_get("AAPL", from = "2013-01-01", to = "2016-12-31")
#'
#' # Bar Chart
#' AAPL %>%
#' ggplot(aes(x = date, y = close)) +
#' geom_barchart(aes(open = open, high = high, low = low, close = close)) +
#' geom_ma(color = "darkgreen") +
#' coord_x_date(xlim = c("2016-01-01", "2016-12-31"),
#' ylim = c(20, 30))
#'
#' # Candlestick Chart
#' AAPL %>%
#' ggplot(aes(x = date, y = close)) +
#' geom_candlestick(aes(open = open, high = high, low = low, close = close)) +
#' geom_ma(color = "darkgreen") +
#' coord_x_date(xlim = c("2016-01-01", "2016-12-31"),
#' ylim = c(20, 30))
# Bar Chart -----
#' @rdname geom_chart
#' @export
geom_barchart <- function(mapping = NULL, data = NULL, stat = "identity",
position = "identity", na.rm = TRUE, show.legend = NA,
inherit.aes = TRUE,
colour_up = "darkblue", colour_down = "red",
fill_up = "darkblue", fill_down = "red",
...) {
linerange <- ggplot2::layer(
stat = StatLinerangeBC, geom = GeomLinerangeBC, data = data, mapping = mapping,
position = position, show.legend = show.legend, inherit.aes = inherit.aes,
params = list(na.rm = na.rm, fill_up = fill_up, fill_down = fill_down,
colour_up = colour_up, colour_down = colour_down, ...)
)
segment_left <- ggplot2::layer(
stat = StatSegmentLeftBC, geom = GeomSegmentBC, data = data, mapping = mapping,
position = position, show.legend = show.legend, inherit.aes = inherit.aes,
params = list(na.rm = na.rm, fill_up = fill_up, fill_down = fill_down,
colour_up = colour_up, colour_down = colour_down, ...)
)
segment_right <- ggplot2::layer(
stat = StatSegmentRightBC, geom = GeomSegmentBC, data = data, mapping = mapping,
position = position, show.legend = show.legend, inherit.aes = inherit.aes,
params = list(na.rm = na.rm, fill_up = fill_up, fill_down = fill_down,
colour_up = colour_up, colour_down = colour_down, ...)
)
list(linerange, segment_left, segment_right)
}
StatLinerangeBC <- ggplot2::ggproto("StatLinerangeBC", ggplot2::Stat,
required_aes = c("x", "open", "high", "low", "close"),
dropped_aes = c("open", "high", "low", "close", "y"),
compute_group = function(data, scales, params,
fill_up, fill_down,
colour_up, colour_down) {
data <- data %>%
dplyr::mutate(color = ifelse(open < close, colour_up, colour_down))
tibble::tibble(x = data$x,
ymin = data$low,
ymax = data$high,
colour = data$color)
}
)
StatSegmentLeftBC <- ggplot2::ggproto("StatSegmentLeftBC", ggplot2::Stat,
required_aes = c("x", "open", "high", "low", "close"),
dropped_aes = c("open", "high", "low", "close"),
compute_group = function(data, scales, params,
fill_up, fill_down,
colour_up, colour_down) {
data <- data %>%
dplyr::mutate(color = ifelse(open < close, colour_up, colour_down))
tibble::tibble(x = data$x,
xend = data$x - 0.5,
y = data$open,
yend = data$open,
colour = data$color)
}
)
StatSegmentRightBC <- ggplot2::ggproto("StatSegmentRightBC", ggplot2::Stat,
required_aes = c("x", "open", "high", "low", "close"),
dropped_aes = c("open", "high", "low", "close"),
compute_group = function(data, scales, params,
fill_up, fill_down,
colour_up, colour_down) {
data <- data %>%
dplyr::mutate(color = ifelse(open < close, colour_up, colour_down))
tibble::tibble(x = data$x,
xend = data$x + 0.5,
y = data$close,
yend = data$close,
colour = data$color)
}
)
GeomLinerangeBC <- ggplot2::ggproto("GeomLinerangeBC", ggplot2::GeomLinerange,
default_aes = ggplot2::aes(
linewidth = 0.5,
linetype = 1,
alpha = NA)
)
GeomSegmentBC <- ggplot2::ggproto("GeomSegmentBC", ggplot2::GeomSegment,
default_aes = ggplot2::aes(
linewidth = 0.5,
linetype = 1,
alpha = NA)
)
# Candlestick Chart -----
#' @rdname geom_chart
#' @export
geom_candlestick <- function(mapping = NULL, data = NULL, stat = "identity",
position = "identity", na.rm = TRUE, show.legend = NA,
inherit.aes = TRUE,
colour_up = "darkblue", colour_down = "red",
fill_up = "darkblue", fill_down = "red",
...) {
linerange <- ggplot2::layer(
stat = StatLinerangeBC, geom = GeomLinerangeBC, data = data, mapping = mapping,
position = position, show.legend = show.legend, inherit.aes = inherit.aes,
params = list(na.rm = na.rm, fill_up = fill_up, fill_down = fill_down,
colour_up = colour_up, colour_down = colour_down, ...)
)
rect <- ggplot2::layer(
stat = StatRectCS, geom = GeomRectCS, data = data, mapping = mapping,
position = position, show.legend = show.legend, inherit.aes = inherit.aes,
params = list(na.rm = na.rm, fill_up = fill_up, fill_down = fill_down,
colour_up = colour_up, colour_down = colour_down, ...)
)
list(linerange, rect)
}
StatRectCS <- ggplot2::ggproto("StatRectCS", ggplot2::Stat,
required_aes = c("x", "open", "high", "low", "close"),
dropped_aes = c("open", "high", "low", "close", "x", "y"),
compute_group = function(data, scales, params,
fill_up, fill_down,
colour_up, colour_down) {
data <- data %>%
dplyr::mutate(fill = ifelse(open < close, fill_up, fill_down),
ymin = ifelse(open < close, open, close),
ymax = ifelse(open < close, close, open))
tibble::tibble(xmin = data$x - 0.45,
xmax = data$x + 0.45,
ymin = data$ymin,
ymax = data$ymax,
fill = data$fill)
}
)
GeomRectCS <- ggplot2::ggproto("GeomRectCS", ggplot2::GeomRect,
default_aes = ggplot2::aes(colour = NA,
linewidth = 0.5,
linetype = 1,
alpha = NA)
)