Hao Zhu | 5fe235c | 2020-08-26 00:26:49 -0400 | [diff] [blame] | 1 | #' Helper functions to generate inline sparklines |
| 2 | #' |
| 3 | #' @description These functions helps you quickly generate sets of sparkline |
| 4 | #' style plots using base R plotting system. Currently, we support histogram |
Bill Evans | cebc971 | 2020-08-30 19:55:24 -0700 | [diff] [blame^] | 5 | #' boxplot, and line. You can use them together with `column_spec` to |
Hao Zhu | 5fe235c | 2020-08-26 00:26:49 -0400 | [diff] [blame] | 6 | #' generate inline plot in tables. By default, this function will save images |
| 7 | #' in a folder called "kableExtra" and return the address of the file. |
| 8 | #' |
| 9 | #' @param x Vector of values or List of vectors of values. |
| 10 | #' @param width The width of the plot in pixel |
| 11 | #' @param height The height of the plot in pixel |
| 12 | #' @param res The resolution of the plot. Default is 300. |
| 13 | #' @param same_lim T/F. If x is a list of vectors, should all the plots be |
| 14 | #' plotted in the same range? Default is True. |
Bill Evans | cebc971 | 2020-08-30 19:55:24 -0700 | [diff] [blame^] | 15 | #' @param lim,xlim,ylim Manually specify plotting range in the form of |
| 16 | #' `c(0, 10)`. `lim` is used in `spec_hist` and `spec_boxplot`; `xlim` |
| 17 | #' and `ylim` are used in `spec_line`. |
Hao Zhu | 5fe235c | 2020-08-26 00:26:49 -0400 | [diff] [blame] | 18 | #' @param xaxt On/Off for xaxis text |
| 19 | #' @param yaxt On/Off for yaxis text |
| 20 | #' @param ann On/Off for annotations (titles and axis titles) |
| 21 | #' @param col Color for the fill of the histogram bar/boxplot box. |
| 22 | #' @param border Color for the border. |
Bill Evans | cebc971 | 2020-08-30 19:55:24 -0700 | [diff] [blame^] | 23 | #' @param frame.plot On/Off for surrounding box (`spec_line` only). Default |
| 24 | #' is False. |
| 25 | #' @param lwd Line width for `spec_line`; within `spec_line`, the `minmax` |
| 26 | #' argument defaults to use this value for `cex` for points. Default is 2. |
| 27 | #' @param minmax,min,max Arguments passed to `points` to highlight minimum |
| 28 | #' and maximum values in `spec_line`. If `min` or `max` are `NULL`, they |
| 29 | #' default to the value of `minmax`. Set to an empty `list()` to disable. |
Hao Zhu | 5fe235c | 2020-08-26 00:26:49 -0400 | [diff] [blame] | 30 | #' @param dir Directory of where the images will be saved. |
| 31 | #' @param file File name. If not provided, a random name will be used |
| 32 | #' @param file_type Graphic device. Support `png` or `svg`. SVG is recommended |
| 33 | #' for HTML output |
| 34 | #' @param add_label For boxplot. T/F to add labels for min, mean and max. |
| 35 | #' @param label_digits If T for add_label, rounding digits for the label. |
| 36 | #' Default is 2. |
| 37 | #' @param boxlty Boxplot - box boarder type |
| 38 | #' @param medcol Boxplot - median line color |
| 39 | #' @param medlwd Boxplot - median line width |
| 40 | #' |
| 41 | #' @inheritParams graphics::hist |
| 42 | #' @inheritParams graphics::boxplot |
| 43 | #' @export |
| 44 | spec_hist <- function(x, width = 200, height = 50, res = 300, |
| 45 | breaks = "Sturges", |
| 46 | same_lim = TRUE, lim = NULL, |
| 47 | xaxt = 'n', yaxt = 'n', ann = FALSE, |
| 48 | col = "lightgray", border = NULL, |
| 49 | dir = if (is_latex()) rmd_files_dir() else tempdir(), |
| 50 | file = NULL, |
| 51 | file_type = if (is_latex()) "png" else "svg", ...) { |
| 52 | if (is.list(x)) { |
| 53 | if (same_lim & is.null(lim)) { |
| 54 | lim <- base::range(unlist(x)) |
| 55 | } |
| 56 | return(lapply(x, function(x_) {spec_hist( |
| 57 | x = x_, width = width, height = height, |
| 58 | breaks = breaks, same_lim = same_lim, lim = lim, |
| 59 | xaxt = xaxt, yaxt = yaxt, ann = ann, col = col, border = border, |
| 60 | dir = dir, file = file, file_type = file_type, ... |
| 61 | )})) |
| 62 | } |
| 63 | |
| 64 | if (is.null(lim)) { |
| 65 | lim <- base::range(x) |
| 66 | } |
| 67 | |
| 68 | file_type <- match.arg(file_type, c("svg", "png")) |
| 69 | |
| 70 | if (!dir.exists(dir)) { |
| 71 | dir.create(dir) |
| 72 | } |
| 73 | |
| 74 | if (is.null(file)) { |
Hao Zhu | cd9a581 | 2020-08-26 15:35:30 -0400 | [diff] [blame] | 75 | file <- file.path(dir, paste0( |
| 76 | "hist_", round(as.numeric(Sys.time()) * 1000), ".", file_type)) |
Hao Zhu | 5fe235c | 2020-08-26 00:26:49 -0400 | [diff] [blame] | 77 | } |
| 78 | |
| 79 | if (file_type == "svg") { |
| 80 | grDevices::svg(filename = file, width = width / res, height = height / res, |
| 81 | bg = 'transparent') |
| 82 | } else { |
| 83 | grDevices::png(filename = file, width = width, height = height, res = res, |
| 84 | bg = 'transparent') |
| 85 | } |
| 86 | |
| 87 | graphics::par(mar = c(0, 0, 0.2, 0), lwd=0.5) |
| 88 | graphics::hist(x, breaks = breaks, xlim = lim, border = border, |
| 89 | xaxt = xaxt, yaxt = yaxt, ann = ann, col = col, ...) |
| 90 | grDevices::dev.off() |
| 91 | |
| 92 | if (file_type == "svg") { |
| 93 | svg_xml <- xml2::read_xml(file) |
| 94 | svg_text <- as.character(svg_xml) |
| 95 | unlink(file) |
| 96 | } else { |
| 97 | svg_text <- NULL |
| 98 | } |
| 99 | out <- list(path = file, dev = file_type, type = "hist", |
| 100 | width = width, height = height, res = res, |
| 101 | svg_text = svg_text) |
| 102 | |
| 103 | class(out) <- "kableExtraInlinePlots" |
| 104 | return(out) |
| 105 | } |
| 106 | |
| 107 | #' @rdname spec_hist |
| 108 | #' @export |
| 109 | spec_boxplot <- function(x, width = 200, height = 50, res = 300, |
| 110 | add_label = FALSE, label_digits = 2, |
| 111 | same_lim = TRUE, lim = NULL, |
| 112 | xaxt = 'n', yaxt = 'n', ann = FALSE, |
| 113 | col = "lightgray", border = NULL, |
| 114 | boxlty = 0, medcol = "red", medlwd = 1, |
| 115 | dir = if (is_latex()) rmd_files_dir() else tempdir(), |
| 116 | file = NULL, |
| 117 | file_type = if (is_latex()) "png" else "svg", ...) { |
| 118 | if (is.list(x)) { |
| 119 | if (same_lim & is.null(lim)) { |
| 120 | lim <- base::range(unlist(x)) |
| 121 | } |
| 122 | return(lapply(x, function(x_) {spec_boxplot( |
| 123 | x = x_, width = width, height = height, |
| 124 | add_label = add_label, same_lim = same_lim, lim = lim, |
| 125 | xaxt = xaxt, yaxt = yaxt, ann = ann, |
| 126 | col = col, border = border, |
| 127 | boxlty = boxlty, medcol = medcol, medlwd = medlwd, |
| 128 | dir = dir, file = file, file_type = file_type, ... |
| 129 | )})) |
| 130 | } |
| 131 | |
| 132 | if (is.null(lim)) { |
| 133 | lim <- base::range(x) |
| 134 | lim[1] <- lim[1] - (lim[2] - lim[1]) / 10 |
| 135 | lim[2] <- (lim[2] - lim[1]) / 10 + lim[2] |
| 136 | } |
| 137 | |
| 138 | file_type <- match.arg(file_type, c("svg", "png")) |
| 139 | |
| 140 | if (!dir.exists(dir)) { |
| 141 | dir.create(dir) |
| 142 | } |
| 143 | |
| 144 | if (is.null(file)) { |
Hao Zhu | cd9a581 | 2020-08-26 15:35:30 -0400 | [diff] [blame] | 145 | file <- file.path(dir, paste0( |
| 146 | "hist_", round(as.numeric(Sys.time()) * 1000), ".", file_type)) |
Hao Zhu | 5fe235c | 2020-08-26 00:26:49 -0400 | [diff] [blame] | 147 | } |
| 148 | |
| 149 | if (file_type == "svg") { |
| 150 | grDevices::svg(filename = file, width = width / res, height = height / res, |
| 151 | bg = 'transparent') |
| 152 | } else { |
| 153 | grDevices::png(filename = file, width = width, height = height, res = res, |
| 154 | bg = 'transparent') |
| 155 | } |
| 156 | |
| 157 | graphics::par(mar = c(0, 0, 0, 0)) |
| 158 | |
| 159 | graphics::boxplot(x, horizontal = TRUE, ann = ann, frame = FALSE, bty = 'n', ylim = lim, |
| 160 | col = col, border = border, |
| 161 | boxlty = boxlty, medcol = medcol, medlwd = medlwd, |
| 162 | axes = FALSE, outcex = 0.2, whisklty = 1, |
| 163 | ...) |
| 164 | if (add_label) { |
| 165 | x_median <- round(median(x, na.rm = T), label_digits) |
| 166 | x_min <- round(min(x, na.rm = T), label_digits) |
| 167 | x_max <- round(max(x, na.rm = T), label_digits) |
| 168 | graphics::text(x_median, y = 1.4, labels = x_median, cex = 0.5) |
| 169 | graphics::text(x_min, y = 0.6, labels = x_min, cex = 0.5) |
| 170 | graphics::text(x_max, y = 0.6, labels = x_max, cex = 0.5) |
| 171 | } |
| 172 | grDevices::dev.off() |
| 173 | |
| 174 | if (file_type == "svg") { |
| 175 | svg_xml <- xml2::read_xml(file) |
| 176 | svg_text <- as.character(svg_xml) |
| 177 | unlink(file) |
| 178 | } else { |
| 179 | svg_text <- NULL |
| 180 | } |
| 181 | out <- list(path = file, dev = file_type, type = "boxplot", |
| 182 | width = width, height = height, res = res, |
| 183 | svg_text = svg_text) |
| 184 | class(out) <- "kableExtraInlinePlots" |
| 185 | return(out) |
| 186 | } |
| 187 | |
| 188 | is_latex <- knitr::is_latex_output |
| 189 | |
| 190 | rmd_files_dir <- function(create = TRUE) { |
| 191 | curr_file_name <- sub("\\.[^\\.]*$", "", knitr::current_input()) |
| 192 | dir_name <- paste0(curr_file_name, "_files") |
| 193 | if (!dir.exists(dir_name) & create) dir.create(dir_name) |
Hao Zhu | 7f3fa85 | 2020-08-26 13:55:38 -0400 | [diff] [blame] | 194 | fig_dir_name <- file.path(dir_name, "figure-latex/") |
Hao Zhu | 5fe235c | 2020-08-26 00:26:49 -0400 | [diff] [blame] | 195 | if (!dir.exists(fig_dir_name) & create) dir.create(fig_dir_name) |
| 196 | return(fig_dir_name) |
| 197 | } |
| 198 | |
Bill Evans | cebc971 | 2020-08-30 19:55:24 -0700 | [diff] [blame^] | 199 | #' @rdname spec_hist |
| 200 | #' @export |
| 201 | spec_line <- function(x, y = NULL, width = 200, height = 50, res = 300, |
| 202 | same_lim = TRUE, xlim = NULL, ylim = NULL, |
| 203 | xaxt = 'n', yaxt = 'n', ann = FALSE, |
| 204 | col = "lightgray", border = NULL, |
| 205 | frame.plot = FALSE, lwd = 2, |
| 206 | minmax = list(pch = ".", cex = lwd, col = "red"), |
| 207 | min = minmax, max = minmax, |
| 208 | dir = if (is_latex()) rmd_files_dir() else tempdir(), |
| 209 | file = NULL, |
| 210 | file_type = if (is_latex()) "png" else "svg", ...) { |
| 211 | if (is.list(x)) { |
| 212 | if (same_lim) { |
| 213 | if (is.null(xlim)) { |
| 214 | xlim <- base::range(unlist(x)) |
| 215 | } |
| 216 | if (is.null(ylim) && !is.null(y)) { |
| 217 | ylim <- base::range(unlist(y)) |
| 218 | } |
| 219 | } |
| 220 | if (is.null(y)) { |
| 221 | y <- replicate(length(x), NULL, simplify = FALSE) |
| 222 | } else if (!is.list(y) || length(x) != length(y)) { |
| 223 | stop("'x' and 'y' are not the same length") |
| 224 | } |
| 225 | return(Map(function(x_, y_) { |
| 226 | spec_line(x = x_, y = y_, |
| 227 | width = width, height = height, |
| 228 | same_lim = same_lim, xlim = xlim, ylim = ylim, |
| 229 | xaxt = xaxt, yaxt = yaxt, ann = ann, col = col, border = border, |
| 230 | frame.plot = frame.plot, lwd = lwd, |
| 231 | minmax = minmax, min = min, max = max, |
| 232 | dir = dir, file = file, file_type = file_type, ...) |
| 233 | }, x, y)) |
| 234 | } |
| 235 | |
| 236 | if (is.null(y) || !length(y)) { |
| 237 | y <- x |
| 238 | x <- seq(0, 1, length.out = length(y)) |
| 239 | tmp <- ylim |
| 240 | ylim <- xlim |
| 241 | xlim <- tmp |
| 242 | } |
| 243 | |
| 244 | if (is.null(xlim)) { |
| 245 | xlim <- base::range(x) |
| 246 | } |
| 247 | |
| 248 | if (is.null(ylim) && !is.null(y)) { |
| 249 | ylim <- base::range(y) |
| 250 | } |
| 251 | |
| 252 | if (is.null(min)) min <- minmax |
| 253 | if (is.null(max)) max <- minmax |
| 254 | |
| 255 | expand <- c( |
| 256 | if (!is.null(min) && length(min)) 0.96 else 1, |
| 257 | if (!is.null(max) && length(max)) 1.04 else 1) |
| 258 | xlim <- xlim * expand |
| 259 | ylim <- ylim * expand |
| 260 | |
| 261 | file_type <- match.arg(file_type, c("svg", "png")) |
| 262 | |
| 263 | if (!dir.exists(dir)) { |
| 264 | dir.create(dir) |
| 265 | } |
| 266 | |
| 267 | if (is.null(file)) { |
| 268 | file <- file.path(dir, paste0( |
| 269 | "hist_", round(as.numeric(Sys.time()) * 1000), ".", file_type)) |
| 270 | } |
| 271 | |
| 272 | if (file_type == "svg") { |
| 273 | grDevices::svg(filename = file, width = width / res, height = height / res, |
| 274 | bg = 'transparent') |
| 275 | } else { |
| 276 | grDevices::png(filename = file, width = width, height = height, res = res, |
| 277 | bg = 'transparent') |
| 278 | } |
| 279 | curdev <- grDevices::dev.cur() |
| 280 | on.exit(grDevices::dev.off(curdev), add = TRUE) |
| 281 | |
| 282 | graphics::par(mar = c(0, 0, 0.2, 0), lwd = lwd) |
| 283 | graphics::plot(x, y, type = "l", xlim = xlim, ylim = ylim, border = border, |
| 284 | xaxt = xaxt, yaxt = yaxt, ann = ann, col = col, |
| 285 | frame.plot = frame.plot, ...) |
| 286 | |
| 287 | if (!is.null(min) && length(min)) { |
| 288 | ind <- which.min(y) |
| 289 | do.call(graphics::points, c(list(x[ind], y[ind]), min)) |
| 290 | } |
| 291 | |
| 292 | if (!is.null(max) && length(max)) { |
| 293 | ind <- which.max(y) |
| 294 | do.call(graphics::points, c(list(x[ind], y[ind]), max)) |
| 295 | } |
| 296 | |
| 297 | grDevices::dev.off(curdev) |
| 298 | |
| 299 | if (file_type == "svg") { |
| 300 | svg_xml <- xml2::read_xml(file) |
| 301 | svg_text <- as.character(svg_xml) |
| 302 | unlink(file) |
| 303 | } else { |
| 304 | svg_text <- NULL |
| 305 | } |
| 306 | out <- list(path = file, dev = file_type, type = "line", |
| 307 | width = width, height = height, res = res, |
| 308 | svg_text = svg_text) |
| 309 | |
| 310 | class(out) <- "kableExtraInlinePlots" |
| 311 | return(out) |
| 312 | } |