blob: 137446e00dbcaa3cf3968e2e1eed60ec0aa698c9 [file] [log] [blame]
Hao Zhu5fe235c2020-08-26 00:26:49 -04001#' Helper functions to generate inline sparklines
2#'
3#' @description These functions helps you quickly generate sets of sparkline
Bill Evans5a383e52020-08-30 20:09:52 -07004#' style plots using base R plotting system. Currently, we support histogram,
Hao Zhuf6b60e82020-10-21 18:58:19 -04005#' boxplot, line, scatter and pointrange plots. You can use them together with
6#' `column_spec` to generate inline plot in tables. By default, this function
7#' will save images in a folder called "kableExtra" and return the address of
8#' the file.
Hao Zhu5fe235c2020-08-26 00:26:49 -04009#'
10#' @param x Vector of values or List of vectors of values.
11#' @param width The width of the plot in pixel
12#' @param height The height of the plot in pixel
13#' @param res The resolution of the plot. Default is 300.
Hao Zhuf6b60e82020-10-21 18:58:19 -040014#' @param breaks The `break` option in `hist`. Default is "Sturges" but you can
15#' also provide a vector to manually specify break points.
Hao Zhu5fe235c2020-08-26 00:26:49 -040016#' @param same_lim T/F. If x is a list of vectors, should all the plots be
17#' plotted in the same range? Default is True.
Hao Zhudefd1892020-09-09 00:08:09 -040018#' @param lim Manually specify plotting range in the form of
19#' `c(0, 10)`.
Hao Zhu5fe235c2020-08-26 00:26:49 -040020#' @param xaxt On/Off for xaxis text
21#' @param yaxt On/Off for yaxis text
22#' @param ann On/Off for annotations (titles and axis titles)
23#' @param col Color for the fill of the histogram bar/boxplot box.
24#' @param border Color for the border.
25#' @param dir Directory of where the images will be saved.
26#' @param file File name. If not provided, a random name will be used
Bill Evans95a04282020-09-14 12:39:25 -070027#' @param file_type Graphic device. Can be character (e.g., `"pdf"`)
28#' or a graphics device function (`grDevices::pdf`). This defaults
29#' to `"pdf"` if the rendering is in LaTeX and `"svg"` otherwise.
Hao Zhu5fe235c2020-08-26 00:26:49 -040030#' for HTML output
Hao Zhudefd1892020-09-09 00:08:09 -040031#' @param ... extra parameters sending to `hist()`
Hao Zhu5fe235c2020-08-26 00:26:49 -040032#'
Hao Zhu5fe235c2020-08-26 00:26:49 -040033#' @export
34spec_hist <- function(x, width = 200, height = 50, res = 300,
35 breaks = "Sturges",
36 same_lim = TRUE, lim = NULL,
37 xaxt = 'n', yaxt = 'n', ann = FALSE,
38 col = "lightgray", border = NULL,
39 dir = if (is_latex()) rmd_files_dir() else tempdir(),
40 file = NULL,
Hao Zhu30db0272021-02-19 13:02:29 -050041 file_type = if (is_latex()) "pdf" else svglite::svglite,
42 ...) {
Hao Zhu5fe235c2020-08-26 00:26:49 -040043 if (is.list(x)) {
44 if (same_lim & is.null(lim)) {
Vincent Arel-Bundock4bdbdb82020-10-07 07:58:52 -040045 lim <- base::range(unlist(x), na.rm=TRUE)
Hao Zhu5fe235c2020-08-26 00:26:49 -040046 }
Bill Evans95a04282020-09-14 12:39:25 -070047
48 dots <- listify_args(x, width, height, res, breaks,
49 lim, xaxt, yaxt, ann, col, border,
50 dir, file, file_type,
51 lengths = c(1, length(x)))
52 return(do.call(Map, c(list(f = spec_hist), dots)))
Hao Zhu5fe235c2020-08-26 00:26:49 -040053 }
54
Bill Evans8def4da2020-09-11 09:58:26 -070055 if (is.null(x)) return(NULL)
56
Hao Zhu5fe235c2020-08-26 00:26:49 -040057 if (is.null(lim)) {
Vincent Arel-Bundock4bdbdb82020-10-07 07:58:52 -040058 lim <- base::range(x, na.rm=TRUE)
Hao Zhu5fe235c2020-08-26 00:26:49 -040059 }
60
Hao Zhu5fe235c2020-08-26 00:26:49 -040061 if (!dir.exists(dir)) {
62 dir.create(dir)
63 }
64
Bill Evans95a04282020-09-14 12:39:25 -070065 file_ext <- dev_chr(file_type)
Hao Zhu5fe235c2020-08-26 00:26:49 -040066 if (is.null(file)) {
Bill Evans95a04282020-09-14 12:39:25 -070067 file <- normalizePath(
68 tempfile(pattern = "hist_", tmpdir = dir, fileext = paste0(".", file_ext)),
69 winslash = "/", mustWork = FALSE)
Hao Zhu5fe235c2020-08-26 00:26:49 -040070 }
71
Bill Evans95a04282020-09-14 12:39:25 -070072 graphics_dev(filename = file, dev = file_type,
73 width = width, height = height, res = res,
74 bg = "transparent")
75 curdev <- grDevices::dev.cur()
76 on.exit(grDevices::dev.off(curdev), add = TRUE)
Hao Zhu5fe235c2020-08-26 00:26:49 -040077
78 graphics::par(mar = c(0, 0, 0.2, 0), lwd=0.5)
79 graphics::hist(x, breaks = breaks, xlim = lim, border = border,
80 xaxt = xaxt, yaxt = yaxt, ann = ann, col = col, ...)
Hao Zhu5fe235c2020-08-26 00:26:49 -040081
Bill Evans95a04282020-09-14 12:39:25 -070082 grDevices::dev.off(curdev)
Hao Zhu5fe235c2020-08-26 00:26:49 -040083
Bill Evans95a04282020-09-14 12:39:25 -070084 out <- make_inline_plot(
85 file, file_ext, file_type,
86 width, height, res,
87 del = TRUE)
Hao Zhu5fe235c2020-08-26 00:26:49 -040088 return(out)
89}
90
Hao Zhudefd1892020-09-09 00:08:09 -040091#' Helper functions to generate inline sparklines
92#'
93#' @description These functions helps you quickly generate sets of sparkline
94#' style plots using base R plotting system. Currently, we support histogram,
Hao Zhuf6b60e82020-10-21 18:58:19 -040095#' boxplot, line, scatter and pointrange plots. You can use them together with
96#' `column_spec` to generate inline plot in tables. By default, this function
97#' will save images in a folder called "kableExtra" and return the address of
98#' the file.
Hao Zhudefd1892020-09-09 00:08:09 -040099#'
100#' @param x Vector of values or List of vectors of values.
101#' @param width The width of the plot in pixel
102#' @param height The height of the plot in pixel
103#' @param res The resolution of the plot. Default is 300.
104#' @param add_label For boxplot. T/F to add labels for min, mean and max.
105#' @param label_digits If T for add_label, rounding digits for the label.
106#' Default is 2.
107#' @param same_lim T/F. If x is a list of vectors, should all the plots be
108#' plotted in the same range? Default is True.
Hao Zhuf6b60e82020-10-21 18:58:19 -0400109#' @param lim Manually specify plotting range in the form of
110#' `c(0, 10)`.
Hao Zhudefd1892020-09-09 00:08:09 -0400111#' @param xaxt On/Off for xaxis text
112#' @param yaxt On/Off for yaxis text
113#' @param ann On/Off for annotations (titles and axis titles)
114#' @param col Color for the fill of the histogram bar/boxplot box.
115#' @param border Color for the border.
116#' @param boxlty Boxplot - box boarder type
117#' @param medcol Boxplot - median line color
118#' @param medlwd Boxplot - median line width
119#' @param dir Directory of where the images will be saved.
120#' @param file File name. If not provided, a random name will be used
Bill Evans95a04282020-09-14 12:39:25 -0700121#' @param file_type Graphic device. Can be character (e.g., `"pdf"`)
122#' or a graphics device function (`grDevices::pdf`). This defaults
123#' to `"pdf"` if the rendering is in LaTeX and `"svg"` otherwise.
Hao Zhudefd1892020-09-09 00:08:09 -0400124#' @param ... extraparameters passing to boxplot
125#'
Hao Zhu5fe235c2020-08-26 00:26:49 -0400126#' @export
127spec_boxplot <- function(x, width = 200, height = 50, res = 300,
128 add_label = FALSE, label_digits = 2,
129 same_lim = TRUE, lim = NULL,
130 xaxt = 'n', yaxt = 'n', ann = FALSE,
131 col = "lightgray", border = NULL,
132 boxlty = 0, medcol = "red", medlwd = 1,
133 dir = if (is_latex()) rmd_files_dir() else tempdir(),
134 file = NULL,
Hao Zhu30db0272021-02-19 13:02:29 -0500135 file_type = if (is_latex()) "pdf" else svglite::svglite,
136 ...) {
Hao Zhu5fe235c2020-08-26 00:26:49 -0400137 if (is.list(x)) {
138 if (same_lim & is.null(lim)) {
Vincent Arel-Bundock4bdbdb82020-10-07 07:58:52 -0400139 lim <- base::range(unlist(x), na.rm=TRUE)
Hao Zhu5fe235c2020-08-26 00:26:49 -0400140 }
Bill Evans95a04282020-09-14 12:39:25 -0700141
142 dots <- listify_args(x, width, height, res,
143 add_label, label_digits,
144 lim, xaxt, yaxt, ann, col, border,
145 dir, file, file_type,
146 lengths = c(1, length(x)))
147 return(do.call(Map, c(list(f = spec_boxplot), dots)))
Hao Zhu5fe235c2020-08-26 00:26:49 -0400148 }
149
Bill Evans8def4da2020-09-11 09:58:26 -0700150 if (is.null(x)) return(NULL)
151
Hao Zhu5fe235c2020-08-26 00:26:49 -0400152 if (is.null(lim)) {
Vincent Arel-Bundock4bdbdb82020-10-07 07:58:52 -0400153 lim <- base::range(x, na.rm=TRUE)
Hao Zhu5fe235c2020-08-26 00:26:49 -0400154 lim[1] <- lim[1] - (lim[2] - lim[1]) / 10
155 lim[2] <- (lim[2] - lim[1]) / 10 + lim[2]
156 }
157
Hao Zhu5fe235c2020-08-26 00:26:49 -0400158 if (!dir.exists(dir)) {
159 dir.create(dir)
160 }
161
Bill Evans95a04282020-09-14 12:39:25 -0700162 file_ext <- dev_chr(file_type)
Hao Zhu5fe235c2020-08-26 00:26:49 -0400163 if (is.null(file)) {
Bill Evans95a04282020-09-14 12:39:25 -0700164 file <- normalizePath(
165 tempfile(pattern = "boxplot_", tmpdir = dir, fileext = paste0(".", file_ext)),
166 winslash = "/", mustWork = FALSE)
Hao Zhu5fe235c2020-08-26 00:26:49 -0400167 }
168
Bill Evans95a04282020-09-14 12:39:25 -0700169 graphics_dev(filename = file, dev = file_type,
170 width = width, height = height, res = res,
171 bg = "transparent")
172 curdev <- grDevices::dev.cur()
173 on.exit(grDevices::dev.off(curdev), add = TRUE)
Hao Zhu5fe235c2020-08-26 00:26:49 -0400174
175 graphics::par(mar = c(0, 0, 0, 0))
176
177 graphics::boxplot(x, horizontal = TRUE, ann = ann, frame = FALSE, bty = 'n', ylim = lim,
Hao Zhuf6b60e82020-10-21 18:58:19 -0400178 col = col, border = border,
179 boxlty = boxlty, medcol = medcol, medlwd = medlwd,
180 axes = FALSE, outcex = 0.2, whisklty = 1,
181 ...)
Hao Zhu5fe235c2020-08-26 00:26:49 -0400182 if (add_label) {
183 x_median <- round(median(x, na.rm = T), label_digits)
184 x_min <- round(min(x, na.rm = T), label_digits)
185 x_max <- round(max(x, na.rm = T), label_digits)
186 graphics::text(x_median, y = 1.4, labels = x_median, cex = 0.5)
187 graphics::text(x_min, y = 0.6, labels = x_min, cex = 0.5)
188 graphics::text(x_max, y = 0.6, labels = x_max, cex = 0.5)
189 }
Hao Zhu5fe235c2020-08-26 00:26:49 -0400190
Bill Evans95a04282020-09-14 12:39:25 -0700191 grDevices::dev.off(curdev)
192
193 out <- make_inline_plot(
194 file, file_ext, file_type,
195 width, height, res,
196 del = TRUE)
Hao Zhu5fe235c2020-08-26 00:26:49 -0400197 return(out)
198}
Jebu38a27242021-12-16 19:25:33 +0100199#' Helper functions to generate inline sparklines
200#'
201#' @description These functions helps you quickly generate sets of sparkline
202#' style plots using base R plotting system. Currently, we support histogram,
203#' boxplot, line, scatter, pointrange, barplot plots. You can use them together with
204#' `column_spec` to generate inline plot in tables. By default, this function
205#' will save images in a folder called "kableExtra" and return the address of
206#' the file.
207#'
208#' @param x Vector of values or List of vectors of values.
209#' @param width The width of the plot in pixel
210#' @param height The height of the plot in pixel
211#' @param res The resolution of the plot. Default is 300.
212#' @param add_label For boxplot. T/F to add labels for min, mean and max.
213#' @param label_digits If T for add_label, rounding digits for the label.
214#' Default is 2.
215#' @param same_lim T/F. If x is a list of vectors, should all the plots be
216#' plotted in the same range? Default is True.
217#' @param lim Manually specify plotting range in the form of
218#' `c(0, 10)`.
219#' @param xaxt On/Off for xaxis text
220#' @param yaxt On/Off for yaxis text
221#' @param ann On/Off for annotations (titles and axis titles)
222#' @param col Color for the fill of the histogram bar/boxplot box.
223#' @param border Color for the border.
224#' @param boxlty Boxplot - box boarder type
225#' @param medcol Boxplot - median line color
226#' @param medlwd Boxplot - median line width
227#' @param dir Directory of where the images will be saved.
228#' @param file File name. If not provided, a random name will be used
229#' @param file_type Graphic device. Can be character (e.g., `"pdf"`)
230#' or a graphics device function (`grDevices::pdf`). This defaults
231#' to `"pdf"` if the rendering is in LaTeX and `"svg"` otherwise.
232#' @param ... extraparameters passing to boxplot
233#'
234#' @export
235spec_barplot <- function(x, devwidth = 200, devheight = 40, res = 300,
236 beside = F,
237 horiz = F,
238 same_lim = TRUE, lim = NULL,
239 xaxt = 'n', yaxt = 'n', ann = FALSE,
240 col = NULL, border = NA,
241 dir = if (is_latex()) rmd_files_dir() else tempdir(),
242 file = NULL,
243 file_type = if (is_latex()) "pdf" else svglite::svglite,
244 ...) {
245 if (is.list(x)) {
246 if (same_lim & is.null(lim)) {
247 lim <- base::range(unlist(x), na.rm=TRUE)
248 }
249
250 dots <- listify_args(x, devwidth, devheight, res, beside,horiz,
251 lim, xaxt, yaxt, ann, col, border,
252 dir, file, file_type,
253 lengths = c(1, length(x)))
254 return(do.call(Map, c(list(f = spec_barplot), dots)))
255 }
256
257 if (is.null(x)) return(NULL)
258
259 if (is.null(lim)) {
260 lim <- base::range(x, na.rm=TRUE)
261 }
262
263 if (!dir.exists(dir)) {
264 dir.create(dir)
265 }
266 height<-matrix(x)
267 height<-cbind(height,0)
268 file_ext <- dev_chr(file_type)
269 if (is.null(file)) {
270 file <- normalizePath(
271 tempfile(pattern = "barplot_", tmpdir = dir, fileext = paste0(".", file_ext)),
272 winslash = "/", mustWork = FALSE)
273 }
274
275 graphics_dev(filename = file, dev = file_type,
276 width = devwidth, height = devheight, res = res,
277 bg = "transparent")
278 curdev <- grDevices::dev.cur()
279 on.exit(grDevices::dev.off(curdev), add = TRUE)
280
281 graphics::par(mar = c(0, 0, 0, 0), lwd=0.5)
282 graphics::barplot(height=height, beside = beside,horiz = horiz, col = col, border = border,xaxt = xaxt, yaxt = yaxt, ann = ann)#,xlim = lim, ann = ann, ...)
283
284 grDevices::dev.off(curdev)
285
286 out <- make_inline_plot(
287 file, file_ext, file_type,
288 devwidth, devheight, res,
289 del = TRUE)
290 return(out)
291}
Hao Zhu5fe235c2020-08-26 00:26:49 -0400292
293is_latex <- knitr::is_latex_output
294
295rmd_files_dir <- function(create = TRUE) {
296 curr_file_name <- sub("\\.[^\\.]*$", "", knitr::current_input())
297 dir_name <- paste0(curr_file_name, "_files")
298 if (!dir.exists(dir_name) & create) dir.create(dir_name)
Hao Zhu7f3fa852020-08-26 13:55:38 -0400299 fig_dir_name <- file.path(dir_name, "figure-latex/")
Hao Zhu5fe235c2020-08-26 00:26:49 -0400300 if (!dir.exists(fig_dir_name) & create) dir.create(fig_dir_name)
301 return(fig_dir_name)
302}
303
Hao Zhudefd1892020-09-09 00:08:09 -0400304#' Helper functions to generate inline sparklines
305#'
306#' @description These functions helps you quickly generate sets of sparkline
307#' style plots using base R plotting system. Currently, we support histogram,
Hao Zhuf6b60e82020-10-21 18:58:19 -0400308#' boxplot, line, scatter and pointrange plots. You can use them together with
309#' `column_spec` to generate inline plot in tables. By default, this function
310#' will save images in a folder called "kableExtra" and return the address of
311#' the file.
Hao Zhudefd1892020-09-09 00:08:09 -0400312#'
313#' @param x,y Vector of values or List of vectors of values. y is optional.
314#' @param width The width of the plot in pixel
315#' @param height The height of the plot in pixel
316#' @param res The resolution of the plot. Default is 300.
317#' @param same_lim T/F. If x is a list of vectors, should all the plots be
318#' plotted in the same range? Default is True.
319#' @param xlim,ylim Manually specify plotting range in the form of
320#' `c(0, 10)`.
321#' @param xaxt On/Off for xaxis text
322#' @param yaxt On/Off for yaxis text
323#' @param ann On/Off for annotations (titles and axis titles)
324#' @param col Color for the fill of the histogram bar/boxplot box.
325#' @param border Color for the border.
Bill Evans548d7152020-09-13 21:44:24 -0700326#' @param frame.plot On/Off for surrounding box (`spec_plot` only). Default
Hao Zhudefd1892020-09-09 00:08:09 -0400327#' is False.
Bill Evans548d7152020-09-13 21:44:24 -0700328#' @param lwd Line width for `spec_plot`; within `spec_plot`, the `minmax`
Hao Zhudefd1892020-09-09 00:08:09 -0400329#' argument defaults to use this value for `cex` for points. Default is 2.
Bill Evansa8ef1fb2020-09-12 22:55:46 -0700330#' @param pch,cex Shape and size for points (if type is other than "l").
331#' @param type Passed to `plot`, often one of "l", "p", or "b", see
332#' [graphics::plot.default()] for more details. Ignored when 'polymin' is
333#' not 'NA'.
334#' @param polymin Special argument that converts a "line" to a polygon,
335#' where the flat portion is this value, and the other side of the polygon
336#' is the 'y' value ('x' if no 'y' provided). If 'NA' (the default), then
Bill Evansad86c072020-09-13 21:54:52 -0700337#' this is ignored; otherwise if this is numeric then a polygon is
338#' created (and 'type' is ignored). Note that if 'polymin' is in the middle
339#' of the 'y' values, it will generate up/down polygons around this value.
Hao Zhudefd1892020-09-09 00:08:09 -0400340#' @param minmax,min,max Arguments passed to `points` to highlight minimum
Bill Evans548d7152020-09-13 21:44:24 -0700341#' and maximum values in `spec_plot`. If `min` or `max` are `NULL`, they
Hao Zhudefd1892020-09-09 00:08:09 -0400342#' default to the value of `minmax`. Set to an empty `list()` to disable.
343#' @param dir Directory of where the images will be saved.
344#' @param file File name. If not provided, a random name will be used
Bill Evansb62414a2020-09-14 12:33:38 -0700345#' @param file_type Graphic device. Can be character (e.g., `"pdf"`)
346#' or a graphics device function (`grDevices::pdf`). This defaults
347#' to `"pdf"` if the rendering is in LaTeX and `"svg"` otherwise.
Hao Zhudefd1892020-09-09 00:08:09 -0400348#' @param ... extra parameters passing to `plot`
349#'
Bill Evanscebc9712020-08-30 19:55:24 -0700350#' @export
Bill Evans548d7152020-09-13 21:44:24 -0700351spec_plot <- function(x, y = NULL, width = 200, height = 50, res = 300,
Bill Evanscebc9712020-08-30 19:55:24 -0700352 same_lim = TRUE, xlim = NULL, ylim = NULL,
353 xaxt = 'n', yaxt = 'n', ann = FALSE,
354 col = "lightgray", border = NULL,
355 frame.plot = FALSE, lwd = 2,
Bill Evansad86c072020-09-13 21:54:52 -0700356 pch = ".", cex = 2, type = "l", polymin = NA,
357 minmax = list(pch = ".", cex = cex, col = "red"),
Bill Evanscebc9712020-08-30 19:55:24 -0700358 min = minmax, max = minmax,
359 dir = if (is_latex()) rmd_files_dir() else tempdir(),
Hao Zhu30db0272021-02-19 13:02:29 -0500360 file = NULL, file_type = if (is_latex()) "pdf" else svglite::svglite,
361 ...) {
Bill Evanscebc9712020-08-30 19:55:24 -0700362 if (is.list(x)) {
Bill Evans60fd80b2020-09-11 10:40:25 -0700363 lenx <- length(x)
364
Bill Evanscebc9712020-08-30 19:55:24 -0700365 if (same_lim) {
366 if (is.null(xlim)) {
Bill Evansad86c072020-09-13 21:54:52 -0700367 xlim <- base::range(unlist(x), na.rm = TRUE)
Bill Evanscebc9712020-08-30 19:55:24 -0700368 }
369 if (is.null(ylim) && !is.null(y)) {
Bill Evansad86c072020-09-13 21:54:52 -0700370 ylim <- base::range(c(unlist(y), polymin), na.rm = TRUE)
Bill Evanscebc9712020-08-30 19:55:24 -0700371 }
372 }
Bill Evansad86c072020-09-13 21:54:52 -0700373
Bill Evanscebc9712020-08-30 19:55:24 -0700374 if (is.null(y)) {
Bill Evansad86c072020-09-13 21:54:52 -0700375 y <- list(y)
376 } else if (length(y) != lenx) {
Bill Evanscebc9712020-08-30 19:55:24 -0700377 stop("'x' and 'y' are not the same length")
378 }
Bill Evans60fd80b2020-09-11 10:40:25 -0700379
Bill Evansb62414a2020-09-14 12:33:38 -0700380 dots <- listify_args(x, y = y, width, height, res,
381 xlim, ylim, xaxt, yaxt, ann, col, border, frame.plot,
382 lwd, pch, cex, type, polymin, minmax, min, max,
383 dir, file, file_type,
384 lengths = c(1, lenx))
Bill Evans60fd80b2020-09-11 10:40:25 -0700385
Bill Evans548d7152020-09-13 21:44:24 -0700386 return(do.call(Map, c(list(f = spec_plot), dots)))
Bill Evans60fd80b2020-09-11 10:40:25 -0700387
Bill Evanscebc9712020-08-30 19:55:24 -0700388 }
389
Bill Evans8def4da2020-09-11 09:58:26 -0700390 if (is.null(x)) return(NULL)
391
Bill Evanscebc9712020-08-30 19:55:24 -0700392 if (is.null(y) || !length(y)) {
393 y <- x
Bill Evansa8ef1fb2020-09-12 22:55:46 -0700394 x <- seq_along(y)
395 if (!is.null(xlim) && is.null(ylim)) {
Bill Evansad86c072020-09-13 21:54:52 -0700396 ylim <- range(c(xlim, polymin), na.rm = TRUE)
Bill Evansa8ef1fb2020-09-12 22:55:46 -0700397 xlim <- range(x)
398 }
Bill Evanscebc9712020-08-30 19:55:24 -0700399 }
400
401 if (is.null(xlim)) {
Bill Evansad86c072020-09-13 21:54:52 -0700402 xlim <- base::range(x, na.rm = TRUE)
Bill Evanscebc9712020-08-30 19:55:24 -0700403 }
404
405 if (is.null(ylim) && !is.null(y)) {
Bill Evansa8ef1fb2020-09-12 22:55:46 -0700406 ylim <- base::range(c(y, polymin), na.rm = TRUE)
Bill Evanscebc9712020-08-30 19:55:24 -0700407 }
408
409 if (is.null(min)) min <- minmax
410 if (is.null(max)) max <- minmax
411
412 expand <- c(
Bill Evans60fd80b2020-09-11 10:40:25 -0700413 if (!is.null(min) && length(min)) -0.04 else 0,
414 if (!is.null(max) && length(max)) +0.04 else 0)
415 xlim <- xlim + diff(xlim) * expand
416 ylim <- ylim + diff(ylim) * expand
Bill Evanscebc9712020-08-30 19:55:24 -0700417
Bill Evanscebc9712020-08-30 19:55:24 -0700418 if (!dir.exists(dir)) {
419 dir.create(dir)
420 }
421
Bill Evansb62414a2020-09-14 12:33:38 -0700422 file_ext <- dev_chr(file_type)
Bill Evanscebc9712020-08-30 19:55:24 -0700423 if (is.null(file)) {
Bill Evansb62414a2020-09-14 12:33:38 -0700424 file <- normalizePath(
425 tempfile(pattern = "plot_", tmpdir = dir, fileext = paste0(".", file_ext)),
426 winslash = "/", mustWork = FALSE)
Bill Evanscebc9712020-08-30 19:55:24 -0700427 }
428
Bill Evansb62414a2020-09-14 12:33:38 -0700429 graphics_dev(filename = file, dev = file_type,
430 width = width, height = height, res = res,
431 bg = "transparent")
Bill Evanscebc9712020-08-30 19:55:24 -0700432 curdev <- grDevices::dev.cur()
433 on.exit(grDevices::dev.off(curdev), add = TRUE)
434
Bill Evansa8ef1fb2020-09-12 22:55:46 -0700435 graphics::par(mar = c(0, 0, 0, 0), lwd = lwd)
436
437 dots <- list(...)
438 if (!is.na(polymin) && "angle" %in% names(dots)) {
439 angle <- dots$angle
440 dots$angle <- NULL
441 } else angle <- 45
442
443 do.call(graphics::plot,
444 c(list(x, y, type = if (is.na(polymin)) type else "n",
445 xlim = xlim, ylim = ylim,
Bill Evanscebc9712020-08-30 19:55:24 -0700446 xaxt = xaxt, yaxt = yaxt, ann = ann, col = col,
Bill Evansad86c072020-09-13 21:54:52 -0700447 frame.plot = frame.plot, cex = cex, pch = pch),
448 dots))
Bill Evansa8ef1fb2020-09-12 22:55:46 -0700449
450 if (!is.na(polymin)) {
451 lty <- if ("lty" %in% names(dots)) dots$lty else graphics::par("lty")
Hao Zhuf6b60e82020-10-21 18:58:19 -0400452 graphics::polygon(c(x[1], x, x[length(x)]), c(polymin, y, polymin),
Bill Evansad86c072020-09-13 21:54:52 -0700453 border = NA, col = col, angle = angle, lty = lty,
454 xpd = if ("xpd" %in% names(dots)) dots$xpd else NA)
Bill Evansa8ef1fb2020-09-12 22:55:46 -0700455 }
Bill Evanscebc9712020-08-30 19:55:24 -0700456
457 if (!is.null(min) && length(min)) {
Bill Evansad86c072020-09-13 21:54:52 -0700458 if (!"xpd" %in% names(min)) min$xpd <- NA
Bill Evanscebc9712020-08-30 19:55:24 -0700459 ind <- which.min(y)
Bill Evansad86c072020-09-13 21:54:52 -0700460 do.call(graphics::points, c(list(x[ind], y[ind]), min))
Bill Evanscebc9712020-08-30 19:55:24 -0700461 }
462
463 if (!is.null(max) && length(max)) {
Bill Evansad86c072020-09-13 21:54:52 -0700464 if (!"xpd" %in% names(max)) max$xpd <- NA
Bill Evanscebc9712020-08-30 19:55:24 -0700465 ind <- which.max(y)
Bill Evansad86c072020-09-13 21:54:52 -0700466 do.call(graphics::points, c(list(x[ind], y[ind]), max))
Bill Evanscebc9712020-08-30 19:55:24 -0700467 }
468
469 grDevices::dev.off(curdev)
470
Bill Evansb62414a2020-09-14 12:33:38 -0700471 out <- make_inline_plot(
472 file, file_ext, file_type,
473 width, height, res,
474 del = TRUE)
Bill Evanscebc9712020-08-30 19:55:24 -0700475 return(out)
476}
Hao Zhuf6b60e82020-10-21 18:58:19 -0400477
478
479#' Helper functions to generate inline sparklines
480#'
481#' @description These functions helps you quickly generate sets of sparkline
482#' style plots using base R plotting system. Currently, we support histogram,
483#' boxplot, line, scatter and pointrange plots. You can use them together with
484#' `column_spec` to generate inline plot in tables. By default, this function
485#' will save images in a folder called "kableExtra" and return the address of
486#' the file.
487#'
488#' @param x,xmin,xmax A scalar value or List of scalar values for dot, left
489#' and right errorbar.
490#' @param vline A scalar value for where to draw a vertical line.
491#' @param width The width of the plot in pixel
492#' @param height The height of the plot in pixel
493#' @param res The resolution of the plot. Default is 300.
494#' @param same_lim T/F. If x is a list of vectors, should all the plots be
495#' plotted in the same range? Default is True.
496#' @param lim Manually specify plotting range in the form of
497#' `c(0, 10)`.
498#' @param xaxt On/Off for xaxis text
499#' @param yaxt On/Off for yaxis text
500#' @param ann On/Off for annotations (titles and axis titles)
Hao Zhu78988912021-02-23 11:00:56 -0500501#' @param col Color for mean dot.
502#' @param line_col Color for the line and the error bar.
Hao Zhuf6b60e82020-10-21 18:58:19 -0400503#' @param cex size of the mean dot and error bar size.
504#' @param frame.plot T/F for whether to plot the plot frames.
505#' @param dir Directory of where the images will be saved.
506#' @param file File name. If not provided, a random name will be used
507#' @param file_type Graphic device. Can be character (e.g., `"pdf"`)
508#' or a graphics device function (`grDevices::pdf`). This defaults
509#' to `"pdf"` if the rendering is in LaTeX and `"svg"` otherwise.
510#' for HTML output
511#' @param ... extra parameters sending to `hist()`
512#'
513#' @export
514spec_pointrange <- function(
515 x, xmin, xmax, vline = NULL,
516 width = 200, height = 50, res = 300,
517 same_lim = TRUE, lim = NULL,
518 xaxt = 'n', yaxt = 'n', ann = FALSE,
Hao Zhu78988912021-02-23 11:00:56 -0500519 col = "red", line_col = "black", cex = 0.3, frame.plot = FALSE,
Hao Zhuf6b60e82020-10-21 18:58:19 -0400520 dir = if (is_latex()) rmd_files_dir() else tempdir(),
521 file = NULL,
Hao Zhu30db0272021-02-19 13:02:29 -0500522 file_type = if (is_latex()) "pdf" else svglite::svglite, ...) {
Hao Zhuf6b60e82020-10-21 18:58:19 -0400523 if (length(x) > 1) {
524 if (same_lim & is.null(lim)) {
525 all_range <- c(unlist(xmin), unlist(xmax))
526 lim <- base::range(all_range, na.rm=TRUE)
527 lim <- lim + c(-0.04 * diff(lim), 0.04 * diff(lim))
528 }
529
530 dots <- listify_args(
531 x = as.list(x), xmin = as.list(xmin), xmax = as.list(xmax), vline,
532 width, height, res,
Hao Zhu78988912021-02-23 11:00:56 -0500533 lim, xaxt, yaxt, ann, col, line_col, cex, frame.plot,
Hao Zhuf6b60e82020-10-21 18:58:19 -0400534 dir, file, file_type,
535 lengths = c(1, length(x)),
536 passthru = c("x", "xmin", "xmax"))
537 return(do.call(Map, c(list(f = spec_pointrange), dots)))
538 }
539
540 if (is.null(x)) return(NULL)
541
542 if (is.null(lim)) {
543 one_range <- unlist(c(xmin, xmax))
544 lim <- base::range(one_range, na.rm=TRUE)
545 lim <- lim + c(-0.04 * diff(lim), 0.04 * diff(lim))
546 }
547
548 if (!dir.exists(dir)) {
549 dir.create(dir)
550 }
551
552 file_ext <- dev_chr(file_type)
553 if (is.null(file)) {
554 file <- normalizePath(
555 tempfile(pattern = "pointrange_", tmpdir = dir, fileext = paste0(".", file_ext)),
556 winslash = "/", mustWork = FALSE)
557 }
558
559 graphics_dev(filename = file, dev = file_type,
560 width = width, height = height, res = res,
561 bg = "transparent")
562 curdev <- grDevices::dev.cur()
563 on.exit(grDevices::dev.off(curdev), add = TRUE)
564
565 graphics::par(mar = c(0, 0, 0.2, 0), lwd=1,
566 ann = ann, xaxt = xaxt, yaxt = yaxt)
567
568 graphics::plot(x, 0, type = "p", pch = ".",
569 xlim = lim, frame.plot = frame.plot)
Hao Zhu78988912021-02-23 11:00:56 -0500570 graphics::arrows(xmin, 0, xmax, 0, cex / 15, angle = 90, code = 3,
571 col = line_col)
Hao Zhuf6b60e82020-10-21 18:58:19 -0400572 graphics::points(x, 0, col = col, type = "p", pch = 15, cex = cex)
573 if (!is.null(vline)) {
574 graphics::abline(v = vline, lty = 3)
575 }
576
577 grDevices::dev.off(curdev)
578
579 out <- make_inline_plot(
580 file, file_ext, file_type,
581 width, height, res,
582 del = TRUE)
583 return(out)
584}