blob: caa0348f55f769b5df5824228df2fe20b84de006 [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}
199
200is_latex <- knitr::is_latex_output
201
202rmd_files_dir <- function(create = TRUE) {
203 curr_file_name <- sub("\\.[^\\.]*$", "", knitr::current_input())
204 dir_name <- paste0(curr_file_name, "_files")
205 if (!dir.exists(dir_name) & create) dir.create(dir_name)
Hao Zhu7f3fa852020-08-26 13:55:38 -0400206 fig_dir_name <- file.path(dir_name, "figure-latex/")
Hao Zhu5fe235c2020-08-26 00:26:49 -0400207 if (!dir.exists(fig_dir_name) & create) dir.create(fig_dir_name)
208 return(fig_dir_name)
209}
210
Hao Zhudefd1892020-09-09 00:08:09 -0400211#' Helper functions to generate inline sparklines
212#'
213#' @description These functions helps you quickly generate sets of sparkline
214#' style plots using base R plotting system. Currently, we support histogram,
Hao Zhuf6b60e82020-10-21 18:58:19 -0400215#' boxplot, line, scatter and pointrange plots. You can use them together with
216#' `column_spec` to generate inline plot in tables. By default, this function
217#' will save images in a folder called "kableExtra" and return the address of
218#' the file.
Hao Zhudefd1892020-09-09 00:08:09 -0400219#'
220#' @param x,y Vector of values or List of vectors of values. y is optional.
221#' @param width The width of the plot in pixel
222#' @param height The height of the plot in pixel
223#' @param res The resolution of the plot. Default is 300.
224#' @param same_lim T/F. If x is a list of vectors, should all the plots be
225#' plotted in the same range? Default is True.
226#' @param xlim,ylim Manually specify plotting range in the form of
227#' `c(0, 10)`.
228#' @param xaxt On/Off for xaxis text
229#' @param yaxt On/Off for yaxis text
230#' @param ann On/Off for annotations (titles and axis titles)
231#' @param col Color for the fill of the histogram bar/boxplot box.
232#' @param border Color for the border.
Bill Evans548d7152020-09-13 21:44:24 -0700233#' @param frame.plot On/Off for surrounding box (`spec_plot` only). Default
Hao Zhudefd1892020-09-09 00:08:09 -0400234#' is False.
Bill Evans548d7152020-09-13 21:44:24 -0700235#' @param lwd Line width for `spec_plot`; within `spec_plot`, the `minmax`
Hao Zhudefd1892020-09-09 00:08:09 -0400236#' argument defaults to use this value for `cex` for points. Default is 2.
Bill Evansa8ef1fb2020-09-12 22:55:46 -0700237#' @param pch,cex Shape and size for points (if type is other than "l").
238#' @param type Passed to `plot`, often one of "l", "p", or "b", see
239#' [graphics::plot.default()] for more details. Ignored when 'polymin' is
240#' not 'NA'.
241#' @param polymin Special argument that converts a "line" to a polygon,
242#' where the flat portion is this value, and the other side of the polygon
243#' is the 'y' value ('x' if no 'y' provided). If 'NA' (the default), then
Bill Evansad86c072020-09-13 21:54:52 -0700244#' this is ignored; otherwise if this is numeric then a polygon is
245#' created (and 'type' is ignored). Note that if 'polymin' is in the middle
246#' of the 'y' values, it will generate up/down polygons around this value.
Hao Zhudefd1892020-09-09 00:08:09 -0400247#' @param minmax,min,max Arguments passed to `points` to highlight minimum
Bill Evans548d7152020-09-13 21:44:24 -0700248#' and maximum values in `spec_plot`. If `min` or `max` are `NULL`, they
Hao Zhudefd1892020-09-09 00:08:09 -0400249#' default to the value of `minmax`. Set to an empty `list()` to disable.
250#' @param dir Directory of where the images will be saved.
251#' @param file File name. If not provided, a random name will be used
Bill Evansb62414a2020-09-14 12:33:38 -0700252#' @param file_type Graphic device. Can be character (e.g., `"pdf"`)
253#' or a graphics device function (`grDevices::pdf`). This defaults
254#' to `"pdf"` if the rendering is in LaTeX and `"svg"` otherwise.
Hao Zhudefd1892020-09-09 00:08:09 -0400255#' @param ... extra parameters passing to `plot`
256#'
Bill Evanscebc9712020-08-30 19:55:24 -0700257#' @export
Bill Evans548d7152020-09-13 21:44:24 -0700258spec_plot <- function(x, y = NULL, width = 200, height = 50, res = 300,
Bill Evanscebc9712020-08-30 19:55:24 -0700259 same_lim = TRUE, xlim = NULL, ylim = NULL,
260 xaxt = 'n', yaxt = 'n', ann = FALSE,
261 col = "lightgray", border = NULL,
262 frame.plot = FALSE, lwd = 2,
Bill Evansad86c072020-09-13 21:54:52 -0700263 pch = ".", cex = 2, type = "l", polymin = NA,
264 minmax = list(pch = ".", cex = cex, col = "red"),
Bill Evanscebc9712020-08-30 19:55:24 -0700265 min = minmax, max = minmax,
266 dir = if (is_latex()) rmd_files_dir() else tempdir(),
Hao Zhu30db0272021-02-19 13:02:29 -0500267 file = NULL, file_type = if (is_latex()) "pdf" else svglite::svglite,
268 ...) {
Bill Evanscebc9712020-08-30 19:55:24 -0700269 if (is.list(x)) {
Bill Evans60fd80b2020-09-11 10:40:25 -0700270 lenx <- length(x)
271
Bill Evanscebc9712020-08-30 19:55:24 -0700272 if (same_lim) {
273 if (is.null(xlim)) {
Bill Evansad86c072020-09-13 21:54:52 -0700274 xlim <- base::range(unlist(x), na.rm = TRUE)
Bill Evanscebc9712020-08-30 19:55:24 -0700275 }
276 if (is.null(ylim) && !is.null(y)) {
Bill Evansad86c072020-09-13 21:54:52 -0700277 ylim <- base::range(c(unlist(y), polymin), na.rm = TRUE)
Bill Evanscebc9712020-08-30 19:55:24 -0700278 }
279 }
Bill Evansad86c072020-09-13 21:54:52 -0700280
Bill Evanscebc9712020-08-30 19:55:24 -0700281 if (is.null(y)) {
Bill Evansad86c072020-09-13 21:54:52 -0700282 y <- list(y)
283 } else if (length(y) != lenx) {
Bill Evanscebc9712020-08-30 19:55:24 -0700284 stop("'x' and 'y' are not the same length")
285 }
Bill Evans60fd80b2020-09-11 10:40:25 -0700286
Bill Evansb62414a2020-09-14 12:33:38 -0700287 dots <- listify_args(x, y = y, width, height, res,
288 xlim, ylim, xaxt, yaxt, ann, col, border, frame.plot,
289 lwd, pch, cex, type, polymin, minmax, min, max,
290 dir, file, file_type,
291 lengths = c(1, lenx))
Bill Evans60fd80b2020-09-11 10:40:25 -0700292
Bill Evans548d7152020-09-13 21:44:24 -0700293 return(do.call(Map, c(list(f = spec_plot), dots)))
Bill Evans60fd80b2020-09-11 10:40:25 -0700294
Bill Evanscebc9712020-08-30 19:55:24 -0700295 }
296
Bill Evans8def4da2020-09-11 09:58:26 -0700297 if (is.null(x)) return(NULL)
298
Bill Evanscebc9712020-08-30 19:55:24 -0700299 if (is.null(y) || !length(y)) {
300 y <- x
Bill Evansa8ef1fb2020-09-12 22:55:46 -0700301 x <- seq_along(y)
302 if (!is.null(xlim) && is.null(ylim)) {
Bill Evansad86c072020-09-13 21:54:52 -0700303 ylim <- range(c(xlim, polymin), na.rm = TRUE)
Bill Evansa8ef1fb2020-09-12 22:55:46 -0700304 xlim <- range(x)
305 }
Bill Evanscebc9712020-08-30 19:55:24 -0700306 }
307
308 if (is.null(xlim)) {
Bill Evansad86c072020-09-13 21:54:52 -0700309 xlim <- base::range(x, na.rm = TRUE)
Bill Evanscebc9712020-08-30 19:55:24 -0700310 }
311
312 if (is.null(ylim) && !is.null(y)) {
Bill Evansa8ef1fb2020-09-12 22:55:46 -0700313 ylim <- base::range(c(y, polymin), na.rm = TRUE)
Bill Evanscebc9712020-08-30 19:55:24 -0700314 }
315
316 if (is.null(min)) min <- minmax
317 if (is.null(max)) max <- minmax
318
319 expand <- c(
Bill Evans60fd80b2020-09-11 10:40:25 -0700320 if (!is.null(min) && length(min)) -0.04 else 0,
321 if (!is.null(max) && length(max)) +0.04 else 0)
322 xlim <- xlim + diff(xlim) * expand
323 ylim <- ylim + diff(ylim) * expand
Bill Evanscebc9712020-08-30 19:55:24 -0700324
Bill Evanscebc9712020-08-30 19:55:24 -0700325 if (!dir.exists(dir)) {
326 dir.create(dir)
327 }
328
Bill Evansb62414a2020-09-14 12:33:38 -0700329 file_ext <- dev_chr(file_type)
Bill Evanscebc9712020-08-30 19:55:24 -0700330 if (is.null(file)) {
Bill Evansb62414a2020-09-14 12:33:38 -0700331 file <- normalizePath(
332 tempfile(pattern = "plot_", tmpdir = dir, fileext = paste0(".", file_ext)),
333 winslash = "/", mustWork = FALSE)
Bill Evanscebc9712020-08-30 19:55:24 -0700334 }
335
Bill Evansb62414a2020-09-14 12:33:38 -0700336 graphics_dev(filename = file, dev = file_type,
337 width = width, height = height, res = res,
338 bg = "transparent")
Bill Evanscebc9712020-08-30 19:55:24 -0700339 curdev <- grDevices::dev.cur()
340 on.exit(grDevices::dev.off(curdev), add = TRUE)
341
Bill Evansa8ef1fb2020-09-12 22:55:46 -0700342 graphics::par(mar = c(0, 0, 0, 0), lwd = lwd)
343
344 dots <- list(...)
345 if (!is.na(polymin) && "angle" %in% names(dots)) {
346 angle <- dots$angle
347 dots$angle <- NULL
348 } else angle <- 45
349
350 do.call(graphics::plot,
351 c(list(x, y, type = if (is.na(polymin)) type else "n",
352 xlim = xlim, ylim = ylim,
Bill Evanscebc9712020-08-30 19:55:24 -0700353 xaxt = xaxt, yaxt = yaxt, ann = ann, col = col,
Bill Evansad86c072020-09-13 21:54:52 -0700354 frame.plot = frame.plot, cex = cex, pch = pch),
355 dots))
Bill Evansa8ef1fb2020-09-12 22:55:46 -0700356
357 if (!is.na(polymin)) {
358 lty <- if ("lty" %in% names(dots)) dots$lty else graphics::par("lty")
Hao Zhuf6b60e82020-10-21 18:58:19 -0400359 graphics::polygon(c(x[1], x, x[length(x)]), c(polymin, y, polymin),
Bill Evansad86c072020-09-13 21:54:52 -0700360 border = NA, col = col, angle = angle, lty = lty,
361 xpd = if ("xpd" %in% names(dots)) dots$xpd else NA)
Bill Evansa8ef1fb2020-09-12 22:55:46 -0700362 }
Bill Evanscebc9712020-08-30 19:55:24 -0700363
364 if (!is.null(min) && length(min)) {
Bill Evansad86c072020-09-13 21:54:52 -0700365 if (!"xpd" %in% names(min)) min$xpd <- NA
Bill Evanscebc9712020-08-30 19:55:24 -0700366 ind <- which.min(y)
Bill Evansad86c072020-09-13 21:54:52 -0700367 do.call(graphics::points, c(list(x[ind], y[ind]), min))
Bill Evanscebc9712020-08-30 19:55:24 -0700368 }
369
370 if (!is.null(max) && length(max)) {
Bill Evansad86c072020-09-13 21:54:52 -0700371 if (!"xpd" %in% names(max)) max$xpd <- NA
Bill Evanscebc9712020-08-30 19:55:24 -0700372 ind <- which.max(y)
Bill Evansad86c072020-09-13 21:54:52 -0700373 do.call(graphics::points, c(list(x[ind], y[ind]), max))
Bill Evanscebc9712020-08-30 19:55:24 -0700374 }
375
376 grDevices::dev.off(curdev)
377
Bill Evansb62414a2020-09-14 12:33:38 -0700378 out <- make_inline_plot(
379 file, file_ext, file_type,
380 width, height, res,
381 del = TRUE)
Bill Evanscebc9712020-08-30 19:55:24 -0700382 return(out)
383}
Hao Zhuf6b60e82020-10-21 18:58:19 -0400384
385
386#' Helper functions to generate inline sparklines
387#'
388#' @description These functions helps you quickly generate sets of sparkline
389#' style plots using base R plotting system. Currently, we support histogram,
390#' boxplot, line, scatter and pointrange plots. You can use them together with
391#' `column_spec` to generate inline plot in tables. By default, this function
392#' will save images in a folder called "kableExtra" and return the address of
393#' the file.
394#'
395#' @param x,xmin,xmax A scalar value or List of scalar values for dot, left
396#' and right errorbar.
397#' @param vline A scalar value for where to draw a vertical line.
398#' @param width The width of the plot in pixel
399#' @param height The height of the plot in pixel
400#' @param res The resolution of the plot. Default is 300.
401#' @param same_lim T/F. If x is a list of vectors, should all the plots be
402#' plotted in the same range? Default is True.
403#' @param lim Manually specify plotting range in the form of
404#' `c(0, 10)`.
405#' @param xaxt On/Off for xaxis text
406#' @param yaxt On/Off for yaxis text
407#' @param ann On/Off for annotations (titles and axis titles)
408#' @param col Color for the fill of the histogram bar/boxplot box.
409#' @param cex size of the mean dot and error bar size.
410#' @param frame.plot T/F for whether to plot the plot frames.
411#' @param dir Directory of where the images will be saved.
412#' @param file File name. If not provided, a random name will be used
413#' @param file_type Graphic device. Can be character (e.g., `"pdf"`)
414#' or a graphics device function (`grDevices::pdf`). This defaults
415#' to `"pdf"` if the rendering is in LaTeX and `"svg"` otherwise.
416#' for HTML output
417#' @param ... extra parameters sending to `hist()`
418#'
419#' @export
420spec_pointrange <- function(
421 x, xmin, xmax, vline = NULL,
422 width = 200, height = 50, res = 300,
423 same_lim = TRUE, lim = NULL,
424 xaxt = 'n', yaxt = 'n', ann = FALSE,
425 col = "red", cex = 0.3, frame.plot = FALSE,
426 dir = if (is_latex()) rmd_files_dir() else tempdir(),
427 file = NULL,
Hao Zhu30db0272021-02-19 13:02:29 -0500428 file_type = if (is_latex()) "pdf" else svglite::svglite, ...) {
Hao Zhuf6b60e82020-10-21 18:58:19 -0400429 if (length(x) > 1) {
430 if (same_lim & is.null(lim)) {
431 all_range <- c(unlist(xmin), unlist(xmax))
432 lim <- base::range(all_range, na.rm=TRUE)
433 lim <- lim + c(-0.04 * diff(lim), 0.04 * diff(lim))
434 }
435
436 dots <- listify_args(
437 x = as.list(x), xmin = as.list(xmin), xmax = as.list(xmax), vline,
438 width, height, res,
439 lim, xaxt, yaxt, ann, col, cex, frame.plot,
440 dir, file, file_type,
441 lengths = c(1, length(x)),
442 passthru = c("x", "xmin", "xmax"))
443 return(do.call(Map, c(list(f = spec_pointrange), dots)))
444 }
445
446 if (is.null(x)) return(NULL)
447
448 if (is.null(lim)) {
449 one_range <- unlist(c(xmin, xmax))
450 lim <- base::range(one_range, na.rm=TRUE)
451 lim <- lim + c(-0.04 * diff(lim), 0.04 * diff(lim))
452 }
453
454 if (!dir.exists(dir)) {
455 dir.create(dir)
456 }
457
458 file_ext <- dev_chr(file_type)
459 if (is.null(file)) {
460 file <- normalizePath(
461 tempfile(pattern = "pointrange_", tmpdir = dir, fileext = paste0(".", file_ext)),
462 winslash = "/", mustWork = FALSE)
463 }
464
465 graphics_dev(filename = file, dev = file_type,
466 width = width, height = height, res = res,
467 bg = "transparent")
468 curdev <- grDevices::dev.cur()
469 on.exit(grDevices::dev.off(curdev), add = TRUE)
470
471 graphics::par(mar = c(0, 0, 0.2, 0), lwd=1,
472 ann = ann, xaxt = xaxt, yaxt = yaxt)
473
474 graphics::plot(x, 0, type = "p", pch = ".",
475 xlim = lim, frame.plot = frame.plot)
476 graphics::arrows(xmin, 0, xmax, 0, cex / 15, angle = 90, code = 3)
477 graphics::points(x, 0, col = col, type = "p", pch = 15, cex = cex)
478 if (!is.null(vline)) {
479 graphics::abline(v = vline, lty = 3)
480 }
481
482 grDevices::dev.off(curdev)
483
484 out <- make_inline_plot(
485 file, file_ext, file_type,
486 width, height, res,
487 del = TRUE)
488 return(out)
489}