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 |
Bill Evans | 5a383e5 | 2020-08-30 20:09:52 -0700 | [diff] [blame] | 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. |
Hao Zhu | defd189 | 2020-09-09 00:08:09 -0400 | [diff] [blame] | 15 | #' @param lim Manually specify plotting range in the form of |
| 16 | #' `c(0, 10)`. |
Hao Zhu | 5fe235c | 2020-08-26 00:26:49 -0400 | [diff] [blame] | 17 | #' @param xaxt On/Off for xaxis text |
| 18 | #' @param yaxt On/Off for yaxis text |
| 19 | #' @param ann On/Off for annotations (titles and axis titles) |
| 20 | #' @param col Color for the fill of the histogram bar/boxplot box. |
| 21 | #' @param border Color for the border. |
| 22 | #' @param dir Directory of where the images will be saved. |
| 23 | #' @param file File name. If not provided, a random name will be used |
| 24 | #' @param file_type Graphic device. Support `png` or `svg`. SVG is recommended |
| 25 | #' for HTML output |
Hao Zhu | defd189 | 2020-09-09 00:08:09 -0400 | [diff] [blame] | 26 | #' @param ... extra parameters sending to `hist()` |
Hao Zhu | 5fe235c | 2020-08-26 00:26:49 -0400 | [diff] [blame] | 27 | #' |
Hao Zhu | 5fe235c | 2020-08-26 00:26:49 -0400 | [diff] [blame] | 28 | #' @export |
| 29 | spec_hist <- function(x, width = 200, height = 50, res = 300, |
| 30 | breaks = "Sturges", |
| 31 | same_lim = TRUE, lim = NULL, |
| 32 | xaxt = 'n', yaxt = 'n', ann = FALSE, |
| 33 | col = "lightgray", border = NULL, |
| 34 | dir = if (is_latex()) rmd_files_dir() else tempdir(), |
| 35 | file = NULL, |
| 36 | file_type = if (is_latex()) "png" else "svg", ...) { |
| 37 | if (is.list(x)) { |
| 38 | if (same_lim & is.null(lim)) { |
| 39 | lim <- base::range(unlist(x)) |
| 40 | } |
| 41 | return(lapply(x, function(x_) {spec_hist( |
| 42 | x = x_, width = width, height = height, |
| 43 | breaks = breaks, same_lim = same_lim, lim = lim, |
| 44 | xaxt = xaxt, yaxt = yaxt, ann = ann, col = col, border = border, |
| 45 | dir = dir, file = file, file_type = file_type, ... |
| 46 | )})) |
| 47 | } |
| 48 | |
Bill Evans | 8def4da | 2020-09-11 09:58:26 -0700 | [diff] [blame] | 49 | if (is.null(x)) return(NULL) |
| 50 | |
Hao Zhu | 5fe235c | 2020-08-26 00:26:49 -0400 | [diff] [blame] | 51 | if (is.null(lim)) { |
| 52 | lim <- base::range(x) |
| 53 | } |
| 54 | |
| 55 | file_type <- match.arg(file_type, c("svg", "png")) |
| 56 | |
| 57 | if (!dir.exists(dir)) { |
| 58 | dir.create(dir) |
| 59 | } |
| 60 | |
| 61 | if (is.null(file)) { |
Hao Zhu | cd9a581 | 2020-08-26 15:35:30 -0400 | [diff] [blame] | 62 | file <- file.path(dir, paste0( |
| 63 | "hist_", round(as.numeric(Sys.time()) * 1000), ".", file_type)) |
Hao Zhu | 5fe235c | 2020-08-26 00:26:49 -0400 | [diff] [blame] | 64 | } |
| 65 | |
| 66 | if (file_type == "svg") { |
| 67 | grDevices::svg(filename = file, width = width / res, height = height / res, |
| 68 | bg = 'transparent') |
| 69 | } else { |
| 70 | grDevices::png(filename = file, width = width, height = height, res = res, |
| 71 | bg = 'transparent') |
| 72 | } |
| 73 | |
| 74 | graphics::par(mar = c(0, 0, 0.2, 0), lwd=0.5) |
| 75 | graphics::hist(x, breaks = breaks, xlim = lim, border = border, |
| 76 | xaxt = xaxt, yaxt = yaxt, ann = ann, col = col, ...) |
| 77 | grDevices::dev.off() |
| 78 | |
| 79 | if (file_type == "svg") { |
| 80 | svg_xml <- xml2::read_xml(file) |
| 81 | svg_text <- as.character(svg_xml) |
| 82 | unlink(file) |
| 83 | } else { |
| 84 | svg_text <- NULL |
| 85 | } |
| 86 | out <- list(path = file, dev = file_type, type = "hist", |
| 87 | width = width, height = height, res = res, |
| 88 | svg_text = svg_text) |
| 89 | |
| 90 | class(out) <- "kableExtraInlinePlots" |
| 91 | return(out) |
| 92 | } |
| 93 | |
Hao Zhu | defd189 | 2020-09-09 00:08:09 -0400 | [diff] [blame] | 94 | #' Helper functions to generate inline sparklines |
| 95 | #' |
| 96 | #' @description These functions helps you quickly generate sets of sparkline |
| 97 | #' style plots using base R plotting system. Currently, we support histogram, |
| 98 | #' boxplot, and line. You can use them together with `column_spec` to |
| 99 | #' generate inline plot in tables. By default, this function will save images |
| 100 | #' in a folder called "kableExtra" and return the address of the file. |
| 101 | #' |
| 102 | #' @param x Vector of values or List of vectors of values. |
| 103 | #' @param width The width of the plot in pixel |
| 104 | #' @param height The height of the plot in pixel |
| 105 | #' @param res The resolution of the plot. Default is 300. |
| 106 | #' @param add_label For boxplot. T/F to add labels for min, mean and max. |
| 107 | #' @param label_digits If T for add_label, rounding digits for the label. |
| 108 | #' Default is 2. |
| 109 | #' @param same_lim T/F. If x is a list of vectors, should all the plots be |
| 110 | #' plotted in the same range? Default is True. |
| 111 | #' @param lim,xlim,ylim Manually specify plotting range in the form of |
| 112 | #' `c(0, 10)`. `lim` is used in `spec_hist` and `spec_boxplot`; `xlim` |
Bill Evans | 548d715 | 2020-09-13 21:44:24 -0700 | [diff] [blame] | 113 | #' and `ylim` are used in `spec_plot`. |
Hao Zhu | defd189 | 2020-09-09 00:08:09 -0400 | [diff] [blame] | 114 | #' @param xaxt On/Off for xaxis text |
| 115 | #' @param yaxt On/Off for yaxis text |
| 116 | #' @param ann On/Off for annotations (titles and axis titles) |
| 117 | #' @param col Color for the fill of the histogram bar/boxplot box. |
| 118 | #' @param border Color for the border. |
| 119 | #' @param boxlty Boxplot - box boarder type |
| 120 | #' @param medcol Boxplot - median line color |
| 121 | #' @param medlwd Boxplot - median line width |
| 122 | #' @param dir Directory of where the images will be saved. |
| 123 | #' @param file File name. If not provided, a random name will be used |
| 124 | #' @param file_type Graphic device. Support `png` or `svg`. SVG is recommended |
| 125 | #' for HTML output |
| 126 | #' @param ... extraparameters passing to boxplot |
| 127 | #' |
Hao Zhu | 5fe235c | 2020-08-26 00:26:49 -0400 | [diff] [blame] | 128 | #' @export |
| 129 | spec_boxplot <- function(x, width = 200, height = 50, res = 300, |
| 130 | add_label = FALSE, label_digits = 2, |
| 131 | same_lim = TRUE, lim = NULL, |
| 132 | xaxt = 'n', yaxt = 'n', ann = FALSE, |
| 133 | col = "lightgray", border = NULL, |
| 134 | boxlty = 0, medcol = "red", medlwd = 1, |
| 135 | dir = if (is_latex()) rmd_files_dir() else tempdir(), |
| 136 | file = NULL, |
| 137 | file_type = if (is_latex()) "png" else "svg", ...) { |
| 138 | if (is.list(x)) { |
| 139 | if (same_lim & is.null(lim)) { |
| 140 | lim <- base::range(unlist(x)) |
| 141 | } |
| 142 | return(lapply(x, function(x_) {spec_boxplot( |
| 143 | x = x_, width = width, height = height, |
| 144 | add_label = add_label, same_lim = same_lim, lim = lim, |
| 145 | xaxt = xaxt, yaxt = yaxt, ann = ann, |
| 146 | col = col, border = border, |
| 147 | boxlty = boxlty, medcol = medcol, medlwd = medlwd, |
| 148 | dir = dir, file = file, file_type = file_type, ... |
| 149 | )})) |
| 150 | } |
| 151 | |
Bill Evans | 8def4da | 2020-09-11 09:58:26 -0700 | [diff] [blame] | 152 | if (is.null(x)) return(NULL) |
| 153 | |
Hao Zhu | 5fe235c | 2020-08-26 00:26:49 -0400 | [diff] [blame] | 154 | if (is.null(lim)) { |
| 155 | lim <- base::range(x) |
| 156 | lim[1] <- lim[1] - (lim[2] - lim[1]) / 10 |
| 157 | lim[2] <- (lim[2] - lim[1]) / 10 + lim[2] |
| 158 | } |
| 159 | |
| 160 | file_type <- match.arg(file_type, c("svg", "png")) |
| 161 | |
| 162 | if (!dir.exists(dir)) { |
| 163 | dir.create(dir) |
| 164 | } |
| 165 | |
| 166 | if (is.null(file)) { |
Hao Zhu | cd9a581 | 2020-08-26 15:35:30 -0400 | [diff] [blame] | 167 | file <- file.path(dir, paste0( |
| 168 | "hist_", round(as.numeric(Sys.time()) * 1000), ".", file_type)) |
Hao Zhu | 5fe235c | 2020-08-26 00:26:49 -0400 | [diff] [blame] | 169 | } |
| 170 | |
| 171 | if (file_type == "svg") { |
| 172 | grDevices::svg(filename = file, width = width / res, height = height / res, |
| 173 | bg = 'transparent') |
| 174 | } else { |
| 175 | grDevices::png(filename = file, width = width, height = height, res = res, |
| 176 | bg = 'transparent') |
| 177 | } |
| 178 | |
| 179 | graphics::par(mar = c(0, 0, 0, 0)) |
| 180 | |
| 181 | graphics::boxplot(x, horizontal = TRUE, ann = ann, frame = FALSE, bty = 'n', ylim = lim, |
| 182 | col = col, border = border, |
| 183 | boxlty = boxlty, medcol = medcol, medlwd = medlwd, |
| 184 | axes = FALSE, outcex = 0.2, whisklty = 1, |
| 185 | ...) |
| 186 | if (add_label) { |
| 187 | x_median <- round(median(x, na.rm = T), label_digits) |
| 188 | x_min <- round(min(x, na.rm = T), label_digits) |
| 189 | x_max <- round(max(x, na.rm = T), label_digits) |
| 190 | graphics::text(x_median, y = 1.4, labels = x_median, cex = 0.5) |
| 191 | graphics::text(x_min, y = 0.6, labels = x_min, cex = 0.5) |
| 192 | graphics::text(x_max, y = 0.6, labels = x_max, cex = 0.5) |
| 193 | } |
| 194 | grDevices::dev.off() |
| 195 | |
| 196 | if (file_type == "svg") { |
| 197 | svg_xml <- xml2::read_xml(file) |
| 198 | svg_text <- as.character(svg_xml) |
| 199 | unlink(file) |
| 200 | } else { |
| 201 | svg_text <- NULL |
| 202 | } |
| 203 | out <- list(path = file, dev = file_type, type = "boxplot", |
| 204 | width = width, height = height, res = res, |
| 205 | svg_text = svg_text) |
| 206 | class(out) <- "kableExtraInlinePlots" |
| 207 | return(out) |
| 208 | } |
| 209 | |
| 210 | is_latex <- knitr::is_latex_output |
| 211 | |
| 212 | rmd_files_dir <- function(create = TRUE) { |
| 213 | curr_file_name <- sub("\\.[^\\.]*$", "", knitr::current_input()) |
| 214 | dir_name <- paste0(curr_file_name, "_files") |
| 215 | if (!dir.exists(dir_name) & create) dir.create(dir_name) |
Hao Zhu | 7f3fa85 | 2020-08-26 13:55:38 -0400 | [diff] [blame] | 216 | fig_dir_name <- file.path(dir_name, "figure-latex/") |
Hao Zhu | 5fe235c | 2020-08-26 00:26:49 -0400 | [diff] [blame] | 217 | if (!dir.exists(fig_dir_name) & create) dir.create(fig_dir_name) |
| 218 | return(fig_dir_name) |
| 219 | } |
| 220 | |
Hao Zhu | defd189 | 2020-09-09 00:08:09 -0400 | [diff] [blame] | 221 | #' Helper functions to generate inline sparklines |
| 222 | #' |
| 223 | #' @description These functions helps you quickly generate sets of sparkline |
| 224 | #' style plots using base R plotting system. Currently, we support histogram, |
| 225 | #' boxplot, and line. You can use them together with `column_spec` to |
| 226 | #' generate inline plot in tables. By default, this function will save images |
| 227 | #' in a folder called "kableExtra" and return the address of the file. |
| 228 | #' |
| 229 | #' @param x,y Vector of values or List of vectors of values. y is optional. |
| 230 | #' @param width The width of the plot in pixel |
| 231 | #' @param height The height of the plot in pixel |
| 232 | #' @param res The resolution of the plot. Default is 300. |
| 233 | #' @param same_lim T/F. If x is a list of vectors, should all the plots be |
| 234 | #' plotted in the same range? Default is True. |
| 235 | #' @param xlim,ylim Manually specify plotting range in the form of |
| 236 | #' `c(0, 10)`. |
| 237 | #' @param xaxt On/Off for xaxis text |
| 238 | #' @param yaxt On/Off for yaxis text |
| 239 | #' @param ann On/Off for annotations (titles and axis titles) |
| 240 | #' @param col Color for the fill of the histogram bar/boxplot box. |
| 241 | #' @param border Color for the border. |
Bill Evans | 548d715 | 2020-09-13 21:44:24 -0700 | [diff] [blame] | 242 | #' @param frame.plot On/Off for surrounding box (`spec_plot` only). Default |
Hao Zhu | defd189 | 2020-09-09 00:08:09 -0400 | [diff] [blame] | 243 | #' is False. |
Bill Evans | 548d715 | 2020-09-13 21:44:24 -0700 | [diff] [blame] | 244 | #' @param lwd Line width for `spec_plot`; within `spec_plot`, the `minmax` |
Hao Zhu | defd189 | 2020-09-09 00:08:09 -0400 | [diff] [blame] | 245 | #' argument defaults to use this value for `cex` for points. Default is 2. |
Bill Evans | a8ef1fb | 2020-09-12 22:55:46 -0700 | [diff] [blame] | 246 | #' @param pch,cex Shape and size for points (if type is other than "l"). |
| 247 | #' @param type Passed to `plot`, often one of "l", "p", or "b", see |
| 248 | #' [graphics::plot.default()] for more details. Ignored when 'polymin' is |
| 249 | #' not 'NA'. |
| 250 | #' @param polymin Special argument that converts a "line" to a polygon, |
| 251 | #' where the flat portion is this value, and the other side of the polygon |
| 252 | #' is the 'y' value ('x' if no 'y' provided). If 'NA' (the default), then |
Bill Evans | ad86c07 | 2020-09-13 21:54:52 -0700 | [diff] [blame^] | 253 | #' this is ignored; otherwise if this is numeric then a polygon is |
| 254 | #' created (and 'type' is ignored). Note that if 'polymin' is in the middle |
| 255 | #' of the 'y' values, it will generate up/down polygons around this value. |
Hao Zhu | defd189 | 2020-09-09 00:08:09 -0400 | [diff] [blame] | 256 | #' @param minmax,min,max Arguments passed to `points` to highlight minimum |
Bill Evans | 548d715 | 2020-09-13 21:44:24 -0700 | [diff] [blame] | 257 | #' and maximum values in `spec_plot`. If `min` or `max` are `NULL`, they |
Hao Zhu | defd189 | 2020-09-09 00:08:09 -0400 | [diff] [blame] | 258 | #' default to the value of `minmax`. Set to an empty `list()` to disable. |
| 259 | #' @param dir Directory of where the images will be saved. |
| 260 | #' @param file File name. If not provided, a random name will be used |
| 261 | #' @param file_type Graphic device. Support `png` or `svg`. SVG is recommended |
| 262 | #' for HTML output. |
| 263 | #' @param ... extra parameters passing to `plot` |
| 264 | #' |
Bill Evans | cebc971 | 2020-08-30 19:55:24 -0700 | [diff] [blame] | 265 | #' @export |
Bill Evans | 548d715 | 2020-09-13 21:44:24 -0700 | [diff] [blame] | 266 | spec_plot <- function(x, y = NULL, width = 200, height = 50, res = 300, |
Bill Evans | cebc971 | 2020-08-30 19:55:24 -0700 | [diff] [blame] | 267 | same_lim = TRUE, xlim = NULL, ylim = NULL, |
| 268 | xaxt = 'n', yaxt = 'n', ann = FALSE, |
| 269 | col = "lightgray", border = NULL, |
| 270 | frame.plot = FALSE, lwd = 2, |
Bill Evans | ad86c07 | 2020-09-13 21:54:52 -0700 | [diff] [blame^] | 271 | pch = ".", cex = 2, type = "l", polymin = NA, |
| 272 | minmax = list(pch = ".", cex = cex, col = "red"), |
Bill Evans | cebc971 | 2020-08-30 19:55:24 -0700 | [diff] [blame] | 273 | min = minmax, max = minmax, |
| 274 | dir = if (is_latex()) rmd_files_dir() else tempdir(), |
Bill Evans | a8ef1fb | 2020-09-12 22:55:46 -0700 | [diff] [blame] | 275 | file = NULL, file_type = if (is_latex()) "png" else "svg", ...) { |
Bill Evans | cebc971 | 2020-08-30 19:55:24 -0700 | [diff] [blame] | 276 | if (is.list(x)) { |
Bill Evans | 60fd80b | 2020-09-11 10:40:25 -0700 | [diff] [blame] | 277 | lenx <- length(x) |
| 278 | |
Bill Evans | cebc971 | 2020-08-30 19:55:24 -0700 | [diff] [blame] | 279 | if (same_lim) { |
| 280 | if (is.null(xlim)) { |
Bill Evans | ad86c07 | 2020-09-13 21:54:52 -0700 | [diff] [blame^] | 281 | xlim <- base::range(unlist(x), na.rm = TRUE) |
Bill Evans | cebc971 | 2020-08-30 19:55:24 -0700 | [diff] [blame] | 282 | } |
| 283 | if (is.null(ylim) && !is.null(y)) { |
Bill Evans | ad86c07 | 2020-09-13 21:54:52 -0700 | [diff] [blame^] | 284 | ylim <- base::range(c(unlist(y), polymin), na.rm = TRUE) |
Bill Evans | cebc971 | 2020-08-30 19:55:24 -0700 | [diff] [blame] | 285 | } |
| 286 | } |
Bill Evans | ad86c07 | 2020-09-13 21:54:52 -0700 | [diff] [blame^] | 287 | |
Bill Evans | cebc971 | 2020-08-30 19:55:24 -0700 | [diff] [blame] | 288 | if (is.null(y)) { |
Bill Evans | ad86c07 | 2020-09-13 21:54:52 -0700 | [diff] [blame^] | 289 | y <- list(y) |
| 290 | } else if (length(y) != lenx) { |
Bill Evans | cebc971 | 2020-08-30 19:55:24 -0700 | [diff] [blame] | 291 | stop("'x' and 'y' are not the same length") |
| 292 | } |
Bill Evans | 60fd80b | 2020-09-11 10:40:25 -0700 | [diff] [blame] | 293 | |
| 294 | # any of the arguments can be per-plot controlling, but an arg |
| 295 | # that is normally not length-1 may be recycled (incorrectly) by |
| 296 | # Map, so we have to listify them if not already lists; |
| 297 | # enforce a restriction of recycling only length 1 or lenx |
| 298 | |
| 299 | # first, start with the literals (x,y) |
Bill Evans | a8ef1fb | 2020-09-12 22:55:46 -0700 | [diff] [blame] | 300 | # (same_lim is not a factor anymore) |
Bill Evans | 60fd80b | 2020-09-11 10:40:25 -0700 | [diff] [blame] | 301 | dots <- list(x = x, y = y) |
| 302 | |
| 303 | # second, we know these args are likely to be vectors (length > 1) |
| 304 | # or lists, so we have to handle them carefully and double-list if |
| 305 | # present |
Bill Evans | a8ef1fb | 2020-09-12 22:55:46 -0700 | [diff] [blame] | 306 | notlen1vec <- list(xlim = xlim, ylim = ylim) |
Bill Evans | 60fd80b | 2020-09-11 10:40:25 -0700 | [diff] [blame] | 307 | dots <- c(dots, Map( |
| 308 | function(L, nm) { |
| 309 | if (is.null(L)) return(list(NULL)) |
Bill Evans | a8ef1fb | 2020-09-12 22:55:46 -0700 | [diff] [blame] | 310 | if (!is.list(L)) return(list(L)) |
| 311 | if (length(L) == lenx) return(L) |
| 312 | stop("length of '", nm, "' must be 1 or the same length as 'x'") |
| 313 | }, notlen1vec, names(notlen1vec))) |
| 314 | |
| 315 | # these are special ... they are lists which may need to be |
| 316 | # nested, and we can't pass NULL since that may default to the |
| 317 | # actual values instead of the intended |
| 318 | notlen1lst <- list(minmax = minmax, min = min, max = max) |
| 319 | dots <- c(dots, Map( |
| 320 | function(L, nm) { |
| 321 | if (is.null(L)) return(list(NULL)) |
| 322 | if (!length(L)) return(list(list())) |
| 323 | if (!is.list(L[[1]])) return (list(L)) |
| 324 | if (length(L) == lenx) return(L) |
| 325 | stop("length of '", nm, "' must be 1 or the same length as 'x'") |
| 326 | }, notlen1lst, names(notlen1lst))) |
Bill Evans | 60fd80b | 2020-09-11 10:40:25 -0700 | [diff] [blame] | 327 | |
| 328 | # last, all remaining arguments that we don't already know about |
| 329 | # are length-1, so can be easily listified; using 'formals()' |
Bill Evans | a8ef1fb | 2020-09-12 22:55:46 -0700 | [diff] [blame] | 330 | # allows us to not hard-code all params |
Bill Evans | 60fd80b | 2020-09-11 10:40:25 -0700 | [diff] [blame] | 331 | len1args <- mget(setdiff(names(formals()), |
| 332 | c(names(dots), "same_lim", "x", "y", "..."))) |
| 333 | dots <- c(dots, Map( |
| 334 | function(V, nm) { |
| 335 | if (is.null(V)) return(list(NULL)) |
| 336 | if (!length(V) %in% c(1L, lenx)) { |
| 337 | stop("length of '", nm, "' must be 1 or the same length as 'x'") |
| 338 | } |
| 339 | V |
| 340 | }, len1args, names(len1args))) |
| 341 | |
Bill Evans | 548d715 | 2020-09-13 21:44:24 -0700 | [diff] [blame] | 342 | return(do.call(Map, c(list(f = spec_plot), dots))) |
Bill Evans | 60fd80b | 2020-09-11 10:40:25 -0700 | [diff] [blame] | 343 | |
Bill Evans | cebc971 | 2020-08-30 19:55:24 -0700 | [diff] [blame] | 344 | } |
| 345 | |
Bill Evans | 8def4da | 2020-09-11 09:58:26 -0700 | [diff] [blame] | 346 | if (is.null(x)) return(NULL) |
| 347 | |
Bill Evans | cebc971 | 2020-08-30 19:55:24 -0700 | [diff] [blame] | 348 | if (is.null(y) || !length(y)) { |
| 349 | y <- x |
Bill Evans | a8ef1fb | 2020-09-12 22:55:46 -0700 | [diff] [blame] | 350 | x <- seq_along(y) |
| 351 | if (!is.null(xlim) && is.null(ylim)) { |
Bill Evans | ad86c07 | 2020-09-13 21:54:52 -0700 | [diff] [blame^] | 352 | ylim <- range(c(xlim, polymin), na.rm = TRUE) |
Bill Evans | a8ef1fb | 2020-09-12 22:55:46 -0700 | [diff] [blame] | 353 | xlim <- range(x) |
| 354 | } |
Bill Evans | cebc971 | 2020-08-30 19:55:24 -0700 | [diff] [blame] | 355 | } |
| 356 | |
| 357 | if (is.null(xlim)) { |
Bill Evans | ad86c07 | 2020-09-13 21:54:52 -0700 | [diff] [blame^] | 358 | xlim <- base::range(x, na.rm = TRUE) |
Bill Evans | cebc971 | 2020-08-30 19:55:24 -0700 | [diff] [blame] | 359 | } |
| 360 | |
| 361 | if (is.null(ylim) && !is.null(y)) { |
Bill Evans | a8ef1fb | 2020-09-12 22:55:46 -0700 | [diff] [blame] | 362 | ylim <- base::range(c(y, polymin), na.rm = TRUE) |
Bill Evans | cebc971 | 2020-08-30 19:55:24 -0700 | [diff] [blame] | 363 | } |
| 364 | |
| 365 | if (is.null(min)) min <- minmax |
| 366 | if (is.null(max)) max <- minmax |
| 367 | |
| 368 | expand <- c( |
Bill Evans | 60fd80b | 2020-09-11 10:40:25 -0700 | [diff] [blame] | 369 | if (!is.null(min) && length(min)) -0.04 else 0, |
| 370 | if (!is.null(max) && length(max)) +0.04 else 0) |
| 371 | xlim <- xlim + diff(xlim) * expand |
| 372 | ylim <- ylim + diff(ylim) * expand |
Bill Evans | cebc971 | 2020-08-30 19:55:24 -0700 | [diff] [blame] | 373 | |
| 374 | file_type <- match.arg(file_type, c("svg", "png")) |
| 375 | |
| 376 | if (!dir.exists(dir)) { |
| 377 | dir.create(dir) |
| 378 | } |
| 379 | |
| 380 | if (is.null(file)) { |
| 381 | file <- file.path(dir, paste0( |
| 382 | "hist_", round(as.numeric(Sys.time()) * 1000), ".", file_type)) |
| 383 | } |
| 384 | |
| 385 | if (file_type == "svg") { |
| 386 | grDevices::svg(filename = file, width = width / res, height = height / res, |
| 387 | bg = 'transparent') |
| 388 | } else { |
| 389 | grDevices::png(filename = file, width = width, height = height, res = res, |
| 390 | bg = 'transparent') |
| 391 | } |
| 392 | curdev <- grDevices::dev.cur() |
| 393 | on.exit(grDevices::dev.off(curdev), add = TRUE) |
| 394 | |
Bill Evans | a8ef1fb | 2020-09-12 22:55:46 -0700 | [diff] [blame] | 395 | graphics::par(mar = c(0, 0, 0, 0), lwd = lwd) |
| 396 | |
| 397 | dots <- list(...) |
| 398 | if (!is.na(polymin) && "angle" %in% names(dots)) { |
| 399 | angle <- dots$angle |
| 400 | dots$angle <- NULL |
| 401 | } else angle <- 45 |
| 402 | |
| 403 | do.call(graphics::plot, |
| 404 | c(list(x, y, type = if (is.na(polymin)) type else "n", |
| 405 | xlim = xlim, ylim = ylim, |
Bill Evans | cebc971 | 2020-08-30 19:55:24 -0700 | [diff] [blame] | 406 | xaxt = xaxt, yaxt = yaxt, ann = ann, col = col, |
Bill Evans | ad86c07 | 2020-09-13 21:54:52 -0700 | [diff] [blame^] | 407 | frame.plot = frame.plot, cex = cex, pch = pch), |
| 408 | dots)) |
Bill Evans | a8ef1fb | 2020-09-12 22:55:46 -0700 | [diff] [blame] | 409 | |
| 410 | if (!is.na(polymin)) { |
| 411 | lty <- if ("lty" %in% names(dots)) dots$lty else graphics::par("lty") |
| 412 | polygon(c(x[1], x, x[length(x)]), c(polymin, y, polymin), |
Bill Evans | ad86c07 | 2020-09-13 21:54:52 -0700 | [diff] [blame^] | 413 | border = NA, col = col, angle = angle, lty = lty, |
| 414 | xpd = if ("xpd" %in% names(dots)) dots$xpd else NA) |
Bill Evans | a8ef1fb | 2020-09-12 22:55:46 -0700 | [diff] [blame] | 415 | } |
Bill Evans | cebc971 | 2020-08-30 19:55:24 -0700 | [diff] [blame] | 416 | |
| 417 | if (!is.null(min) && length(min)) { |
Bill Evans | ad86c07 | 2020-09-13 21:54:52 -0700 | [diff] [blame^] | 418 | if (!"xpd" %in% names(min)) min$xpd <- NA |
Bill Evans | cebc971 | 2020-08-30 19:55:24 -0700 | [diff] [blame] | 419 | ind <- which.min(y) |
Bill Evans | ad86c07 | 2020-09-13 21:54:52 -0700 | [diff] [blame^] | 420 | do.call(graphics::points, c(list(x[ind], y[ind]), min)) |
Bill Evans | cebc971 | 2020-08-30 19:55:24 -0700 | [diff] [blame] | 421 | } |
| 422 | |
| 423 | if (!is.null(max) && length(max)) { |
Bill Evans | ad86c07 | 2020-09-13 21:54:52 -0700 | [diff] [blame^] | 424 | if (!"xpd" %in% names(max)) max$xpd <- NA |
Bill Evans | cebc971 | 2020-08-30 19:55:24 -0700 | [diff] [blame] | 425 | ind <- which.max(y) |
Bill Evans | ad86c07 | 2020-09-13 21:54:52 -0700 | [diff] [blame^] | 426 | do.call(graphics::points, c(list(x[ind], y[ind]), max)) |
Bill Evans | cebc971 | 2020-08-30 19:55:24 -0700 | [diff] [blame] | 427 | } |
| 428 | |
| 429 | grDevices::dev.off(curdev) |
| 430 | |
| 431 | if (file_type == "svg") { |
| 432 | svg_xml <- xml2::read_xml(file) |
| 433 | svg_text <- as.character(svg_xml) |
| 434 | unlink(file) |
| 435 | } else { |
| 436 | svg_text <- NULL |
| 437 | } |
| 438 | out <- list(path = file, dev = file_type, type = "line", |
| 439 | width = width, height = height, res = res, |
| 440 | svg_text = svg_text) |
| 441 | |
| 442 | class(out) <- "kableExtraInlinePlots" |
| 443 | return(out) |
| 444 | } |