blob: cef8c5e672217425e7ab354bf3072d3898f63032 [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,
Bill Evans95a04282020-09-14 12:39:25 -070041 file_type = if (is_latex()) "pdf" else "svg", ...) {
Hao Zhu5fe235c2020-08-26 00:26:49 -040042 if (is.list(x)) {
43 if (same_lim & is.null(lim)) {
Vincent Arel-Bundock4bdbdb82020-10-07 07:58:52 -040044 lim <- base::range(unlist(x), na.rm=TRUE)
Hao Zhu5fe235c2020-08-26 00:26:49 -040045 }
Bill Evans95a04282020-09-14 12:39:25 -070046
47 dots <- listify_args(x, width, height, res, breaks,
48 lim, xaxt, yaxt, ann, col, border,
49 dir, file, file_type,
50 lengths = c(1, length(x)))
51 return(do.call(Map, c(list(f = spec_hist), dots)))
Hao Zhu5fe235c2020-08-26 00:26:49 -040052 }
53
Bill Evans8def4da2020-09-11 09:58:26 -070054 if (is.null(x)) return(NULL)
55
Hao Zhu5fe235c2020-08-26 00:26:49 -040056 if (is.null(lim)) {
Vincent Arel-Bundock4bdbdb82020-10-07 07:58:52 -040057 lim <- base::range(x, na.rm=TRUE)
Hao Zhu5fe235c2020-08-26 00:26:49 -040058 }
59
Hao Zhu5fe235c2020-08-26 00:26:49 -040060 if (!dir.exists(dir)) {
61 dir.create(dir)
62 }
63
Bill Evans95a04282020-09-14 12:39:25 -070064 file_ext <- dev_chr(file_type)
Hao Zhu5fe235c2020-08-26 00:26:49 -040065 if (is.null(file)) {
Bill Evans95a04282020-09-14 12:39:25 -070066 file <- normalizePath(
67 tempfile(pattern = "hist_", tmpdir = dir, fileext = paste0(".", file_ext)),
68 winslash = "/", mustWork = FALSE)
Hao Zhu5fe235c2020-08-26 00:26:49 -040069 }
70
Bill Evans95a04282020-09-14 12:39:25 -070071 graphics_dev(filename = file, dev = file_type,
72 width = width, height = height, res = res,
73 bg = "transparent")
74 curdev <- grDevices::dev.cur()
75 on.exit(grDevices::dev.off(curdev), add = TRUE)
Hao Zhu5fe235c2020-08-26 00:26:49 -040076
77 graphics::par(mar = c(0, 0, 0.2, 0), lwd=0.5)
78 graphics::hist(x, breaks = breaks, xlim = lim, border = border,
79 xaxt = xaxt, yaxt = yaxt, ann = ann, col = col, ...)
Hao Zhu5fe235c2020-08-26 00:26:49 -040080
Bill Evans95a04282020-09-14 12:39:25 -070081 grDevices::dev.off(curdev)
Hao Zhu5fe235c2020-08-26 00:26:49 -040082
Bill Evans95a04282020-09-14 12:39:25 -070083 out <- make_inline_plot(
84 file, file_ext, file_type,
85 width, height, res,
86 del = TRUE)
Hao Zhu5fe235c2020-08-26 00:26:49 -040087 return(out)
88}
89
Hao Zhudefd1892020-09-09 00:08:09 -040090#' Helper functions to generate inline sparklines
91#'
92#' @description These functions helps you quickly generate sets of sparkline
93#' style plots using base R plotting system. Currently, we support histogram,
Hao Zhuf6b60e82020-10-21 18:58:19 -040094#' boxplot, line, scatter and pointrange plots. You can use them together with
95#' `column_spec` to generate inline plot in tables. By default, this function
96#' will save images in a folder called "kableExtra" and return the address of
97#' the file.
Hao Zhudefd1892020-09-09 00:08:09 -040098#'
99#' @param x Vector of values or List of vectors of values.
100#' @param width The width of the plot in pixel
101#' @param height The height of the plot in pixel
102#' @param res The resolution of the plot. Default is 300.
103#' @param add_label For boxplot. T/F to add labels for min, mean and max.
104#' @param label_digits If T for add_label, rounding digits for the label.
105#' Default is 2.
106#' @param same_lim T/F. If x is a list of vectors, should all the plots be
107#' plotted in the same range? Default is True.
Hao Zhuf6b60e82020-10-21 18:58:19 -0400108#' @param lim Manually specify plotting range in the form of
109#' `c(0, 10)`.
Hao Zhudefd1892020-09-09 00:08:09 -0400110#' @param xaxt On/Off for xaxis text
111#' @param yaxt On/Off for yaxis text
112#' @param ann On/Off for annotations (titles and axis titles)
113#' @param col Color for the fill of the histogram bar/boxplot box.
114#' @param border Color for the border.
115#' @param boxlty Boxplot - box boarder type
116#' @param medcol Boxplot - median line color
117#' @param medlwd Boxplot - median line width
118#' @param dir Directory of where the images will be saved.
119#' @param file File name. If not provided, a random name will be used
Bill Evans95a04282020-09-14 12:39:25 -0700120#' @param file_type Graphic device. Can be character (e.g., `"pdf"`)
121#' or a graphics device function (`grDevices::pdf`). This defaults
122#' to `"pdf"` if the rendering is in LaTeX and `"svg"` otherwise.
Hao Zhudefd1892020-09-09 00:08:09 -0400123#' @param ... extraparameters passing to boxplot
124#'
Hao Zhu5fe235c2020-08-26 00:26:49 -0400125#' @export
126spec_boxplot <- function(x, width = 200, height = 50, res = 300,
127 add_label = FALSE, label_digits = 2,
128 same_lim = TRUE, lim = NULL,
129 xaxt = 'n', yaxt = 'n', ann = FALSE,
130 col = "lightgray", border = NULL,
131 boxlty = 0, medcol = "red", medlwd = 1,
132 dir = if (is_latex()) rmd_files_dir() else tempdir(),
133 file = NULL,
Bill Evans95a04282020-09-14 12:39:25 -0700134 file_type = if (is_latex()) "pdf" else "svg", ...) {
Hao Zhu5fe235c2020-08-26 00:26:49 -0400135 if (is.list(x)) {
136 if (same_lim & is.null(lim)) {
Vincent Arel-Bundock4bdbdb82020-10-07 07:58:52 -0400137 lim <- base::range(unlist(x), na.rm=TRUE)
Hao Zhu5fe235c2020-08-26 00:26:49 -0400138 }
Bill Evans95a04282020-09-14 12:39:25 -0700139
140 dots <- listify_args(x, width, height, res,
141 add_label, label_digits,
142 lim, xaxt, yaxt, ann, col, border,
143 dir, file, file_type,
144 lengths = c(1, length(x)))
145 return(do.call(Map, c(list(f = spec_boxplot), dots)))
Hao Zhu5fe235c2020-08-26 00:26:49 -0400146 }
147
Bill Evans8def4da2020-09-11 09:58:26 -0700148 if (is.null(x)) return(NULL)
149
Hao Zhu5fe235c2020-08-26 00:26:49 -0400150 if (is.null(lim)) {
Vincent Arel-Bundock4bdbdb82020-10-07 07:58:52 -0400151 lim <- base::range(x, na.rm=TRUE)
Hao Zhu5fe235c2020-08-26 00:26:49 -0400152 lim[1] <- lim[1] - (lim[2] - lim[1]) / 10
153 lim[2] <- (lim[2] - lim[1]) / 10 + lim[2]
154 }
155
Hao Zhu5fe235c2020-08-26 00:26:49 -0400156 if (!dir.exists(dir)) {
157 dir.create(dir)
158 }
159
Bill Evans95a04282020-09-14 12:39:25 -0700160 file_ext <- dev_chr(file_type)
Hao Zhu5fe235c2020-08-26 00:26:49 -0400161 if (is.null(file)) {
Bill Evans95a04282020-09-14 12:39:25 -0700162 file <- normalizePath(
163 tempfile(pattern = "boxplot_", tmpdir = dir, fileext = paste0(".", file_ext)),
164 winslash = "/", mustWork = FALSE)
Hao Zhu5fe235c2020-08-26 00:26:49 -0400165 }
166
Bill Evans95a04282020-09-14 12:39:25 -0700167 graphics_dev(filename = file, dev = file_type,
168 width = width, height = height, res = res,
169 bg = "transparent")
170 curdev <- grDevices::dev.cur()
171 on.exit(grDevices::dev.off(curdev), add = TRUE)
Hao Zhu5fe235c2020-08-26 00:26:49 -0400172
173 graphics::par(mar = c(0, 0, 0, 0))
174
175 graphics::boxplot(x, horizontal = TRUE, ann = ann, frame = FALSE, bty = 'n', ylim = lim,
Hao Zhuf6b60e82020-10-21 18:58:19 -0400176 col = col, border = border,
177 boxlty = boxlty, medcol = medcol, medlwd = medlwd,
178 axes = FALSE, outcex = 0.2, whisklty = 1,
179 ...)
Hao Zhu5fe235c2020-08-26 00:26:49 -0400180 if (add_label) {
181 x_median <- round(median(x, na.rm = T), label_digits)
182 x_min <- round(min(x, na.rm = T), label_digits)
183 x_max <- round(max(x, na.rm = T), label_digits)
184 graphics::text(x_median, y = 1.4, labels = x_median, cex = 0.5)
185 graphics::text(x_min, y = 0.6, labels = x_min, cex = 0.5)
186 graphics::text(x_max, y = 0.6, labels = x_max, cex = 0.5)
187 }
Hao Zhu5fe235c2020-08-26 00:26:49 -0400188
Bill Evans95a04282020-09-14 12:39:25 -0700189 grDevices::dev.off(curdev)
190
191 out <- make_inline_plot(
192 file, file_ext, file_type,
193 width, height, res,
194 del = TRUE)
Hao Zhu5fe235c2020-08-26 00:26:49 -0400195 return(out)
196}
197
198is_latex <- knitr::is_latex_output
199
200rmd_files_dir <- function(create = TRUE) {
201 curr_file_name <- sub("\\.[^\\.]*$", "", knitr::current_input())
202 dir_name <- paste0(curr_file_name, "_files")
203 if (!dir.exists(dir_name) & create) dir.create(dir_name)
Hao Zhu7f3fa852020-08-26 13:55:38 -0400204 fig_dir_name <- file.path(dir_name, "figure-latex/")
Hao Zhu5fe235c2020-08-26 00:26:49 -0400205 if (!dir.exists(fig_dir_name) & create) dir.create(fig_dir_name)
206 return(fig_dir_name)
207}
208
Hao Zhudefd1892020-09-09 00:08:09 -0400209#' Helper functions to generate inline sparklines
210#'
211#' @description These functions helps you quickly generate sets of sparkline
212#' style plots using base R plotting system. Currently, we support histogram,
Hao Zhuf6b60e82020-10-21 18:58:19 -0400213#' boxplot, line, scatter and pointrange plots. You can use them together with
214#' `column_spec` to generate inline plot in tables. By default, this function
215#' will save images in a folder called "kableExtra" and return the address of
216#' the file.
Hao Zhudefd1892020-09-09 00:08:09 -0400217#'
218#' @param x,y Vector of values or List of vectors of values. y is optional.
219#' @param width The width of the plot in pixel
220#' @param height The height of the plot in pixel
221#' @param res The resolution of the plot. Default is 300.
222#' @param same_lim T/F. If x is a list of vectors, should all the plots be
223#' plotted in the same range? Default is True.
224#' @param xlim,ylim Manually specify plotting range in the form of
225#' `c(0, 10)`.
226#' @param xaxt On/Off for xaxis text
227#' @param yaxt On/Off for yaxis text
228#' @param ann On/Off for annotations (titles and axis titles)
229#' @param col Color for the fill of the histogram bar/boxplot box.
230#' @param border Color for the border.
Bill Evans548d7152020-09-13 21:44:24 -0700231#' @param frame.plot On/Off for surrounding box (`spec_plot` only). Default
Hao Zhudefd1892020-09-09 00:08:09 -0400232#' is False.
Bill Evans548d7152020-09-13 21:44:24 -0700233#' @param lwd Line width for `spec_plot`; within `spec_plot`, the `minmax`
Hao Zhudefd1892020-09-09 00:08:09 -0400234#' argument defaults to use this value for `cex` for points. Default is 2.
Bill Evansa8ef1fb2020-09-12 22:55:46 -0700235#' @param pch,cex Shape and size for points (if type is other than "l").
236#' @param type Passed to `plot`, often one of "l", "p", or "b", see
237#' [graphics::plot.default()] for more details. Ignored when 'polymin' is
238#' not 'NA'.
239#' @param polymin Special argument that converts a "line" to a polygon,
240#' where the flat portion is this value, and the other side of the polygon
241#' is the 'y' value ('x' if no 'y' provided). If 'NA' (the default), then
Bill Evansad86c072020-09-13 21:54:52 -0700242#' this is ignored; otherwise if this is numeric then a polygon is
243#' created (and 'type' is ignored). Note that if 'polymin' is in the middle
244#' of the 'y' values, it will generate up/down polygons around this value.
Hao Zhudefd1892020-09-09 00:08:09 -0400245#' @param minmax,min,max Arguments passed to `points` to highlight minimum
Bill Evans548d7152020-09-13 21:44:24 -0700246#' and maximum values in `spec_plot`. If `min` or `max` are `NULL`, they
Hao Zhudefd1892020-09-09 00:08:09 -0400247#' default to the value of `minmax`. Set to an empty `list()` to disable.
248#' @param dir Directory of where the images will be saved.
249#' @param file File name. If not provided, a random name will be used
Bill Evansb62414a2020-09-14 12:33:38 -0700250#' @param file_type Graphic device. Can be character (e.g., `"pdf"`)
251#' or a graphics device function (`grDevices::pdf`). This defaults
252#' to `"pdf"` if the rendering is in LaTeX and `"svg"` otherwise.
Hao Zhudefd1892020-09-09 00:08:09 -0400253#' @param ... extra parameters passing to `plot`
254#'
Bill Evanscebc9712020-08-30 19:55:24 -0700255#' @export
Bill Evans548d7152020-09-13 21:44:24 -0700256spec_plot <- function(x, y = NULL, width = 200, height = 50, res = 300,
Bill Evanscebc9712020-08-30 19:55:24 -0700257 same_lim = TRUE, xlim = NULL, ylim = NULL,
258 xaxt = 'n', yaxt = 'n', ann = FALSE,
259 col = "lightgray", border = NULL,
260 frame.plot = FALSE, lwd = 2,
Bill Evansad86c072020-09-13 21:54:52 -0700261 pch = ".", cex = 2, type = "l", polymin = NA,
262 minmax = list(pch = ".", cex = cex, col = "red"),
Bill Evanscebc9712020-08-30 19:55:24 -0700263 min = minmax, max = minmax,
264 dir = if (is_latex()) rmd_files_dir() else tempdir(),
Bill Evansb62414a2020-09-14 12:33:38 -0700265 file = NULL, file_type = if (is_latex()) "pdf" else "svg", ...) {
Bill Evanscebc9712020-08-30 19:55:24 -0700266 if (is.list(x)) {
Bill Evans60fd80b2020-09-11 10:40:25 -0700267 lenx <- length(x)
268
Bill Evanscebc9712020-08-30 19:55:24 -0700269 if (same_lim) {
270 if (is.null(xlim)) {
Bill Evansad86c072020-09-13 21:54:52 -0700271 xlim <- base::range(unlist(x), na.rm = TRUE)
Bill Evanscebc9712020-08-30 19:55:24 -0700272 }
273 if (is.null(ylim) && !is.null(y)) {
Bill Evansad86c072020-09-13 21:54:52 -0700274 ylim <- base::range(c(unlist(y), polymin), na.rm = TRUE)
Bill Evanscebc9712020-08-30 19:55:24 -0700275 }
276 }
Bill Evansad86c072020-09-13 21:54:52 -0700277
Bill Evanscebc9712020-08-30 19:55:24 -0700278 if (is.null(y)) {
Bill Evansad86c072020-09-13 21:54:52 -0700279 y <- list(y)
280 } else if (length(y) != lenx) {
Bill Evanscebc9712020-08-30 19:55:24 -0700281 stop("'x' and 'y' are not the same length")
282 }
Bill Evans60fd80b2020-09-11 10:40:25 -0700283
Bill Evansb62414a2020-09-14 12:33:38 -0700284 dots <- listify_args(x, y = y, width, height, res,
285 xlim, ylim, xaxt, yaxt, ann, col, border, frame.plot,
286 lwd, pch, cex, type, polymin, minmax, min, max,
287 dir, file, file_type,
288 lengths = c(1, lenx))
Bill Evans60fd80b2020-09-11 10:40:25 -0700289
Bill Evans548d7152020-09-13 21:44:24 -0700290 return(do.call(Map, c(list(f = spec_plot), dots)))
Bill Evans60fd80b2020-09-11 10:40:25 -0700291
Bill Evanscebc9712020-08-30 19:55:24 -0700292 }
293
Bill Evans8def4da2020-09-11 09:58:26 -0700294 if (is.null(x)) return(NULL)
295
Bill Evanscebc9712020-08-30 19:55:24 -0700296 if (is.null(y) || !length(y)) {
297 y <- x
Bill Evansa8ef1fb2020-09-12 22:55:46 -0700298 x <- seq_along(y)
299 if (!is.null(xlim) && is.null(ylim)) {
Bill Evansad86c072020-09-13 21:54:52 -0700300 ylim <- range(c(xlim, polymin), na.rm = TRUE)
Bill Evansa8ef1fb2020-09-12 22:55:46 -0700301 xlim <- range(x)
302 }
Bill Evanscebc9712020-08-30 19:55:24 -0700303 }
304
305 if (is.null(xlim)) {
Bill Evansad86c072020-09-13 21:54:52 -0700306 xlim <- base::range(x, na.rm = TRUE)
Bill Evanscebc9712020-08-30 19:55:24 -0700307 }
308
309 if (is.null(ylim) && !is.null(y)) {
Bill Evansa8ef1fb2020-09-12 22:55:46 -0700310 ylim <- base::range(c(y, polymin), na.rm = TRUE)
Bill Evanscebc9712020-08-30 19:55:24 -0700311 }
312
313 if (is.null(min)) min <- minmax
314 if (is.null(max)) max <- minmax
315
316 expand <- c(
Bill Evans60fd80b2020-09-11 10:40:25 -0700317 if (!is.null(min) && length(min)) -0.04 else 0,
318 if (!is.null(max) && length(max)) +0.04 else 0)
319 xlim <- xlim + diff(xlim) * expand
320 ylim <- ylim + diff(ylim) * expand
Bill Evanscebc9712020-08-30 19:55:24 -0700321
Bill Evanscebc9712020-08-30 19:55:24 -0700322 if (!dir.exists(dir)) {
323 dir.create(dir)
324 }
325
Bill Evansb62414a2020-09-14 12:33:38 -0700326 file_ext <- dev_chr(file_type)
Bill Evanscebc9712020-08-30 19:55:24 -0700327 if (is.null(file)) {
Bill Evansb62414a2020-09-14 12:33:38 -0700328 file <- normalizePath(
329 tempfile(pattern = "plot_", tmpdir = dir, fileext = paste0(".", file_ext)),
330 winslash = "/", mustWork = FALSE)
Bill Evanscebc9712020-08-30 19:55:24 -0700331 }
332
Bill Evansb62414a2020-09-14 12:33:38 -0700333 graphics_dev(filename = file, dev = file_type,
334 width = width, height = height, res = res,
335 bg = "transparent")
Bill Evanscebc9712020-08-30 19:55:24 -0700336 curdev <- grDevices::dev.cur()
337 on.exit(grDevices::dev.off(curdev), add = TRUE)
338
Bill Evansa8ef1fb2020-09-12 22:55:46 -0700339 graphics::par(mar = c(0, 0, 0, 0), lwd = lwd)
340
341 dots <- list(...)
342 if (!is.na(polymin) && "angle" %in% names(dots)) {
343 angle <- dots$angle
344 dots$angle <- NULL
345 } else angle <- 45
346
347 do.call(graphics::plot,
348 c(list(x, y, type = if (is.na(polymin)) type else "n",
349 xlim = xlim, ylim = ylim,
Bill Evanscebc9712020-08-30 19:55:24 -0700350 xaxt = xaxt, yaxt = yaxt, ann = ann, col = col,
Bill Evansad86c072020-09-13 21:54:52 -0700351 frame.plot = frame.plot, cex = cex, pch = pch),
352 dots))
Bill Evansa8ef1fb2020-09-12 22:55:46 -0700353
354 if (!is.na(polymin)) {
355 lty <- if ("lty" %in% names(dots)) dots$lty else graphics::par("lty")
Hao Zhuf6b60e82020-10-21 18:58:19 -0400356 graphics::polygon(c(x[1], x, x[length(x)]), c(polymin, y, polymin),
Bill Evansad86c072020-09-13 21:54:52 -0700357 border = NA, col = col, angle = angle, lty = lty,
358 xpd = if ("xpd" %in% names(dots)) dots$xpd else NA)
Bill Evansa8ef1fb2020-09-12 22:55:46 -0700359 }
Bill Evanscebc9712020-08-30 19:55:24 -0700360
361 if (!is.null(min) && length(min)) {
Bill Evansad86c072020-09-13 21:54:52 -0700362 if (!"xpd" %in% names(min)) min$xpd <- NA
Bill Evanscebc9712020-08-30 19:55:24 -0700363 ind <- which.min(y)
Bill Evansad86c072020-09-13 21:54:52 -0700364 do.call(graphics::points, c(list(x[ind], y[ind]), min))
Bill Evanscebc9712020-08-30 19:55:24 -0700365 }
366
367 if (!is.null(max) && length(max)) {
Bill Evansad86c072020-09-13 21:54:52 -0700368 if (!"xpd" %in% names(max)) max$xpd <- NA
Bill Evanscebc9712020-08-30 19:55:24 -0700369 ind <- which.max(y)
Bill Evansad86c072020-09-13 21:54:52 -0700370 do.call(graphics::points, c(list(x[ind], y[ind]), max))
Bill Evanscebc9712020-08-30 19:55:24 -0700371 }
372
373 grDevices::dev.off(curdev)
374
Bill Evansb62414a2020-09-14 12:33:38 -0700375 out <- make_inline_plot(
376 file, file_ext, file_type,
377 width, height, res,
378 del = TRUE)
Bill Evanscebc9712020-08-30 19:55:24 -0700379 return(out)
380}
Hao Zhuf6b60e82020-10-21 18:58:19 -0400381
382
383#' Helper functions to generate inline sparklines
384#'
385#' @description These functions helps you quickly generate sets of sparkline
386#' style plots using base R plotting system. Currently, we support histogram,
387#' boxplot, line, scatter and pointrange plots. You can use them together with
388#' `column_spec` to generate inline plot in tables. By default, this function
389#' will save images in a folder called "kableExtra" and return the address of
390#' the file.
391#'
392#' @param x,xmin,xmax A scalar value or List of scalar values for dot, left
393#' and right errorbar.
394#' @param vline A scalar value for where to draw a vertical line.
395#' @param width The width of the plot in pixel
396#' @param height The height of the plot in pixel
397#' @param res The resolution of the plot. Default is 300.
398#' @param same_lim T/F. If x is a list of vectors, should all the plots be
399#' plotted in the same range? Default is True.
400#' @param lim Manually specify plotting range in the form of
401#' `c(0, 10)`.
402#' @param xaxt On/Off for xaxis text
403#' @param yaxt On/Off for yaxis text
404#' @param ann On/Off for annotations (titles and axis titles)
405#' @param col Color for the fill of the histogram bar/boxplot box.
406#' @param cex size of the mean dot and error bar size.
407#' @param frame.plot T/F for whether to plot the plot frames.
408#' @param dir Directory of where the images will be saved.
409#' @param file File name. If not provided, a random name will be used
410#' @param file_type Graphic device. Can be character (e.g., `"pdf"`)
411#' or a graphics device function (`grDevices::pdf`). This defaults
412#' to `"pdf"` if the rendering is in LaTeX and `"svg"` otherwise.
413#' for HTML output
414#' @param ... extra parameters sending to `hist()`
415#'
416#' @export
417spec_pointrange <- function(
418 x, xmin, xmax, vline = NULL,
419 width = 200, height = 50, res = 300,
420 same_lim = TRUE, lim = NULL,
421 xaxt = 'n', yaxt = 'n', ann = FALSE,
422 col = "red", cex = 0.3, frame.plot = FALSE,
423 dir = if (is_latex()) rmd_files_dir() else tempdir(),
424 file = NULL,
425 file_type = if (is_latex()) "pdf" else "svg", ...) {
426 if (length(x) > 1) {
427 if (same_lim & is.null(lim)) {
428 all_range <- c(unlist(xmin), unlist(xmax))
429 lim <- base::range(all_range, na.rm=TRUE)
430 lim <- lim + c(-0.04 * diff(lim), 0.04 * diff(lim))
431 }
432
433 dots <- listify_args(
434 x = as.list(x), xmin = as.list(xmin), xmax = as.list(xmax), vline,
435 width, height, res,
436 lim, xaxt, yaxt, ann, col, cex, frame.plot,
437 dir, file, file_type,
438 lengths = c(1, length(x)),
439 passthru = c("x", "xmin", "xmax"))
440 return(do.call(Map, c(list(f = spec_pointrange), dots)))
441 }
442
443 if (is.null(x)) return(NULL)
444
445 if (is.null(lim)) {
446 one_range <- unlist(c(xmin, xmax))
447 lim <- base::range(one_range, na.rm=TRUE)
448 lim <- lim + c(-0.04 * diff(lim), 0.04 * diff(lim))
449 }
450
451 if (!dir.exists(dir)) {
452 dir.create(dir)
453 }
454
455 file_ext <- dev_chr(file_type)
456 if (is.null(file)) {
457 file <- normalizePath(
458 tempfile(pattern = "pointrange_", tmpdir = dir, fileext = paste0(".", file_ext)),
459 winslash = "/", mustWork = FALSE)
460 }
461
462 graphics_dev(filename = file, dev = file_type,
463 width = width, height = height, res = res,
464 bg = "transparent")
465 curdev <- grDevices::dev.cur()
466 on.exit(grDevices::dev.off(curdev), add = TRUE)
467
468 graphics::par(mar = c(0, 0, 0.2, 0), lwd=1,
469 ann = ann, xaxt = xaxt, yaxt = yaxt)
470
471 graphics::plot(x, 0, type = "p", pch = ".",
472 xlim = lim, frame.plot = frame.plot)
473 graphics::arrows(xmin, 0, xmax, 0, cex / 15, angle = 90, code = 3)
474 graphics::points(x, 0, col = col, type = "p", pch = 15, cex = cex)
475 if (!is.null(vline)) {
476 graphics::abline(v = vline, lty = 3)
477 }
478
479 grDevices::dev.off(curdev)
480
481 out <- make_inline_plot(
482 file, file_ext, file_type,
483 width, height, res,
484 del = TRUE)
485 return(out)
486}