-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathutils.R
265 lines (221 loc) · 9.07 KB
/
utils.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
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
#' This finds the closest date to a given date that exists in a dataset.
#'
#' @param data A data frame that includes a column `timestamp`
#' @param date The date you want to match`
#' @param type Default "all", which returns closest date overall. Other options are "before", "after", "beforeequal", and "afterequal"`
#' @param force_val Default True. If True, then the function will return the "all" type if there is no date given for the type
#' @keywords
#' @export
#' @examples
#' closest_date(data = df, date = "2020-01-01", type = "all", force_val = T)
closest_date <- function(
data,
date,
type = "all",
force_val = T
){
# Put both into data format
data$timestamp <- ymd(data$timestamp)
date <- ymd(date)
# Calculate the difference between the date you gave and each date in the dataset
data$diff_with_date <- abs(as.numeric(data$timestamp - date))
# If we have a date, we want to get the closest date available in the dataset with
# a few options
# If we want the date that is closest AND before
before <- data %>% filter(timestamp < date) %>% filter(diff_with_date == min(.$diff_with_date, na.rm = T)) %>% pull(timestamp)
# If we want the date that is closest AND after
after <- data %>% filter(timestamp > date) %>% filter(diff_with_date == min(.$diff_with_date, na.rm = T)) %>% pull(timestamp)
# If we want the date that is closest AND (before or equal)
beforeequal <- data %>% filter(timestamp <= date) %>% filter(diff_with_date == min(.$diff_with_date, na.rm = T)) %>% pull(timestamp)
# If we want the date that is closest AND (after or equal)
afterequal <- data %>% filter(timestamp >= date) %>% filter(diff_with_date == min(.$diff_with_date, na.rm = T)) %>% pull(timestamp)
# If we want the date that is closest regardless of everything else
all <- data %>% filter(diff_with_date == min(.$diff_with_date, na.rm = T)) %>% pull(timestamp)
# The type argument lets us set up which option we want
if(type == "before") out <- before
if(type == "after") out <- after
if(type == "beforeequal") out <- beforeequal
if(type == "afterequal") out <- afterequal
if(type == "all") out <- all
# force_val allows us to default to `all`
if(force_val & length(out) == 0) out <- all
# We return the result as a Date
out <- ymd(out)
return(out)
}
#' Colorscheme
#'
#' @param col A data frame that includes a column `timestamp`
#' @keywords
#' @export
#' @examples
#' colorschemer("red")
colorschemer <- function(col){
# Every colorscheme has seven colors.
# - hicol: primary color
# - locol: subdued version of that color
# - nucol: neutral color
# - opcol: opposite color
# - highcol: intense version of the color
# - lowcol: opposite color
# - midcol: midway between highcol and lowcol
# First we look at the namespace of the function. If any of these exist,
# we use them. If not, we set them to NA
pe <- parent.frame()
hicol <- if (exists("hicol", pe)) get("hicol", pe) else {NA}
locol <- if (exists("locol", pe)) get("locol", pe) else {NA}
nucol <- if (exists("nucol", pe)) get("nucol", pe) else {NA}
opcol <- if (exists("opcol", pe)) get("opcol", pe) else {NA}
highcol <- if (exists("highcol", pe)) get("highcol", pe) else {NA}
lowcol <- if (exists("lowcol", pe)) get("lowcol", pe) else {NA}
midcol <- if (exists("midcol", pe)) get("midcol", pe) else {NA}
# This is a sample colorscheme. It will replace any value not found in the
# above with a color in the theme.
if(col == "blue"){
getPalette <- colorRampPalette(brewer.pal(100, "RdBu"))
if (is.null(hicol) || is.na(hicol)) assign("hicol", getPalette(1000)[900], pos=parent.frame())
if (is.null(locol) || is.na(locol)) assign("locol", getPalette(1000)[700], pos=parent.frame())
if (is.null(nucol) || is.na(nucol)) assign("nucol", getPalette(1000)[550], pos=parent.frame())
if (is.null(opcol) || is.na(opcol)) assign("opcol", getPalette(1000)[200], pos=parent.frame())
if (is.null(highcol) || is.na(highcol)) assign("highcol", getPalette(1000)[900], pos=parent.frame())
if (is.null(lowcol) || is.na(lowcol)) assign("lowcol", getPalette(1000)[200], pos=parent.frame())
if (is.null(midcol) || is.na(midcol)) assign("midcol", getPalette(1000)[550], pos=parent.frame())
}
if(col == "red"){
getPalette <- colorRampPalette(brewer.pal(100, "RdBu"))
if (is.null(hicol) || is.na(hicol)) assign("hicol", rev(getPalette(1000))[900], pos=parent.frame())
if (is.null(locol) || is.na(locol)) assign("locol", rev(getPalette(1000))[700], pos=parent.frame())
if (is.null(nucol) || is.na(nucol)) assign("nucol", rev(getPalette(1000))[550], pos=parent.frame())
if (is.null(opcol) || is.na(opcol)) assign("opcol", rev(getPalette(1000))[200], pos=parent.frame())
if (is.null(highcol) || is.na(highcol)) assign("highcol", rev(getPalette(1000))[900], pos=parent.frame())
if (is.null(lowcol) || is.na(lowcol)) assign("lowcol", rev(getPalette(1000))[200], pos=parent.frame())
if (is.null(midcol) || is.na(midcol)) assign("midcol", rev(getPalette(1000))[550], pos=parent.frame())
}
if (col == "jamaim"){
getPalette <- colorRampPalette(brewer.pal(100, "RdBu"))
if (is.null(hicol) || is.na(hicol)) assign("hicol", "#2F5763", pos=parent.frame())
if (is.null(locol) || is.na(locol)) assign("locol", "#F7941D", pos=parent.frame())
if (is.null(nucol) || is.na(nucol)) assign("nucol", "#BDD1DA", pos=parent.frame())
if (is.null(opcol) || is.na(opcol)) assign("opcol", "#F7941D", pos=parent.frame())
if (is.null(highcol) || is.na(highcol)) assign("highcol", "#2F5763", pos=parent.frame())
if (is.null(lowcol) || is.na(lowcol)) assign("lowcol", "white", pos=parent.frame())
if (is.null(midcol) || is.na(midcol)) assign("midcol", "#C3D6DE", pos=parent.frame())
}
if (col == "13rw"){
if(is.null(hicol) || is.na(hicol)) assign("hicol", "#1A4653", pos=parent.frame())
if(is.null(locol) || is.na(locol)) assign("locol", "#7796A2", pos=parent.frame())
if(is.null(nucol) || is.na(nucol)) assign("nucol", "#EEF3F5", pos=parent.frame())
if(is.null(opcol) || is.na(opcol)) assign("opcol", "#AA6C39", pos=parent.frame())
if(is.null(highcol) || is.na(highcol)) assign("highcol", "#1A4653", pos=parent.frame())
if(is.null(lowcol) || is.na(lowcol)) assign("lowcol", "white", pos=parent.frame())
if(is.null(midcol) || is.na(midcol)) assign("midcol", "#AA6C39", pos=parent.frame())
}
}
#' samplemean
#'
#' @param x vector
#' @param d indices
#' @keywords
#' @export
#' @examples
samplemean <- function(x, d) {
# This is a convenience function to give us the sample mean of a vector. Useful for bootstrapping.
return(mean(x[d]))
}
#' min0
#'
#' @param x vector
#' @param d indices
#' @keywords
#' @export
#' @examples
minpos <- function(x) {
# This makes it so that any numeric value is at least positive.
return(ifelse(x <= 0, 0.00000001, x))
}
#' annotation_compass: taken from
#' https://stackoverflow.com/questions/47916307/ggplot2-specify-geom-text-position-by-conventional-top-bottom-left-rig
#'
#' @param label label
#' @param position position
#' @keywords
#' @export
#' @examples
annotation_compass <- function(label,
position = c('N','NE','E','SE','S','SW','W','NW'),
padding = grid::unit(c(0.5,0.5),"line"), ...){
position <- match.arg(position)
x <- switch (position,
N = 0.5,
NE = 1,
E = 1,
SE = 1,
S = 0.5,
SW = 0,
W = 0,
NW = 0
)
y <- switch (position,
N = 1,
NE = 1,
E = 0.5,
SE = 0,
S = 0,
SW = 0,
W = 0.5,
NW = 1
)
hjust <- switch (position,
N = 0.5,
NE = 1,
E = 1,
SE = 1,
S = 0.5,
SW = 0,
W = 0,
NW = 0
)
vjust <- switch (position,
N = 1,
NE = 1,
E = 0.5,
SE = 0,
S = 0,
SW = 0,
W = 0.5,
NW = 1
)
f1 <- switch (position,
N = 0,
NE = -1,
E = -1,
SE = -1,
S = 0,
SW = 1,
W = 1,
NW = 1
)
f2 <- switch (position,
N = -1,
NE = -1,
E = 0,
SE = 1,
S = 1,
SW = 1,
W = 0,
NW = -1
)
annotation_custom(grid::textGrob(label,
x=grid::unit(x,"npc") + f1*padding[1] ,
y=grid::unit(y,"npc") + f2*padding[2],
hjust=hjust,vjust=vjust, ...))
}
#' ceiling_dec: taken from
#' https://stackoverflow.com/questions/35807523/r-decimal-ceiling
#'
#' @param label label
#' @param position position
#' @keywords
#' @export
#' @examples
ceiling_dec <- function(x, level=1) round(x + 5*10^(-level-1), level)