| 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 | 
| Bill Evans | 95a0428 | 2020-09-14 12:39:25 -0700 | [diff] [blame] | 24 | #' @param file_type Graphic device. Can be character (e.g., `"pdf"`) | 
|  | 25 | #'   or a graphics device function (`grDevices::pdf`). This defaults | 
|  | 26 | #'   to `"pdf"` if the rendering is in LaTeX and `"svg"` otherwise. | 
| Hao Zhu | 5fe235c | 2020-08-26 00:26:49 -0400 | [diff] [blame] | 27 | #' for HTML output | 
| Hao Zhu | defd189 | 2020-09-09 00:08:09 -0400 | [diff] [blame] | 28 | #' @param ... extra parameters sending to `hist()` | 
| Hao Zhu | 5fe235c | 2020-08-26 00:26:49 -0400 | [diff] [blame] | 29 | #' | 
| Hao Zhu | 5fe235c | 2020-08-26 00:26:49 -0400 | [diff] [blame] | 30 | #' @export | 
|  | 31 | spec_hist <- function(x, width = 200, height = 50, res = 300, | 
|  | 32 | breaks = "Sturges", | 
|  | 33 | same_lim = TRUE, lim = NULL, | 
|  | 34 | xaxt = 'n', yaxt = 'n', ann = FALSE, | 
|  | 35 | col = "lightgray", border = NULL, | 
|  | 36 | dir = if (is_latex()) rmd_files_dir() else tempdir(), | 
|  | 37 | file = NULL, | 
| Bill Evans | 95a0428 | 2020-09-14 12:39:25 -0700 | [diff] [blame] | 38 | file_type = if (is_latex()) "pdf" else "svg", ...) { | 
| Hao Zhu | 5fe235c | 2020-08-26 00:26:49 -0400 | [diff] [blame] | 39 | if (is.list(x)) { | 
|  | 40 | if (same_lim & is.null(lim)) { | 
| Vincent Arel-Bundock | 4bdbdb8 | 2020-10-07 07:58:52 -0400 | [diff] [blame] | 41 | lim <- base::range(unlist(x), na.rm=TRUE) | 
| Hao Zhu | 5fe235c | 2020-08-26 00:26:49 -0400 | [diff] [blame] | 42 | } | 
| Bill Evans | 95a0428 | 2020-09-14 12:39:25 -0700 | [diff] [blame] | 43 |  | 
|  | 44 | dots <- listify_args(x, width, height, res, breaks, | 
|  | 45 | lim, xaxt, yaxt, ann, col, border, | 
|  | 46 | dir, file, file_type, | 
|  | 47 | lengths = c(1, length(x))) | 
|  | 48 | return(do.call(Map, c(list(f = spec_hist), dots))) | 
| Hao Zhu | 5fe235c | 2020-08-26 00:26:49 -0400 | [diff] [blame] | 49 | } | 
|  | 50 |  | 
| Bill Evans | 8def4da | 2020-09-11 09:58:26 -0700 | [diff] [blame] | 51 | if (is.null(x)) return(NULL) | 
|  | 52 |  | 
| Hao Zhu | 5fe235c | 2020-08-26 00:26:49 -0400 | [diff] [blame] | 53 | if (is.null(lim)) { | 
| Vincent Arel-Bundock | 4bdbdb8 | 2020-10-07 07:58:52 -0400 | [diff] [blame] | 54 | lim <- base::range(x, na.rm=TRUE) | 
| Hao Zhu | 5fe235c | 2020-08-26 00:26:49 -0400 | [diff] [blame] | 55 | } | 
|  | 56 |  | 
| Hao Zhu | 5fe235c | 2020-08-26 00:26:49 -0400 | [diff] [blame] | 57 | if (!dir.exists(dir)) { | 
|  | 58 | dir.create(dir) | 
|  | 59 | } | 
|  | 60 |  | 
| Bill Evans | 95a0428 | 2020-09-14 12:39:25 -0700 | [diff] [blame] | 61 | file_ext <- dev_chr(file_type) | 
| Hao Zhu | 5fe235c | 2020-08-26 00:26:49 -0400 | [diff] [blame] | 62 | if (is.null(file)) { | 
| Bill Evans | 95a0428 | 2020-09-14 12:39:25 -0700 | [diff] [blame] | 63 | file <- normalizePath( | 
|  | 64 | tempfile(pattern = "hist_", tmpdir = dir, fileext = paste0(".", file_ext)), | 
|  | 65 | winslash = "/", mustWork = FALSE) | 
| Hao Zhu | 5fe235c | 2020-08-26 00:26:49 -0400 | [diff] [blame] | 66 | } | 
|  | 67 |  | 
| Bill Evans | 95a0428 | 2020-09-14 12:39:25 -0700 | [diff] [blame] | 68 | graphics_dev(filename = file, dev = file_type, | 
|  | 69 | width = width, height = height, res = res, | 
|  | 70 | bg = "transparent") | 
|  | 71 | curdev <- grDevices::dev.cur() | 
|  | 72 | on.exit(grDevices::dev.off(curdev), add = TRUE) | 
| Hao Zhu | 5fe235c | 2020-08-26 00:26:49 -0400 | [diff] [blame] | 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, ...) | 
| Hao Zhu | 5fe235c | 2020-08-26 00:26:49 -0400 | [diff] [blame] | 77 |  | 
| Bill Evans | 95a0428 | 2020-09-14 12:39:25 -0700 | [diff] [blame] | 78 | grDevices::dev.off(curdev) | 
| Hao Zhu | 5fe235c | 2020-08-26 00:26:49 -0400 | [diff] [blame] | 79 |  | 
| Bill Evans | 95a0428 | 2020-09-14 12:39:25 -0700 | [diff] [blame] | 80 | out <- make_inline_plot( | 
|  | 81 | file, file_ext, file_type, | 
|  | 82 | width, height, res, | 
|  | 83 | del = TRUE) | 
| Hao Zhu | 5fe235c | 2020-08-26 00:26:49 -0400 | [diff] [blame] | 84 | return(out) | 
|  | 85 | } | 
|  | 86 |  | 
| Hao Zhu | defd189 | 2020-09-09 00:08:09 -0400 | [diff] [blame] | 87 | #' Helper functions to generate inline sparklines | 
|  | 88 | #' | 
|  | 89 | #' @description These functions helps you quickly generate sets of sparkline | 
|  | 90 | #' style plots using base R plotting system. Currently, we support histogram, | 
|  | 91 | #' boxplot, and line. You can use them together with `column_spec` to | 
|  | 92 | #' generate inline plot in tables. By default, this function will save images | 
|  | 93 | #' in a folder called "kableExtra" and return the address of the file. | 
|  | 94 | #' | 
|  | 95 | #' @param x Vector of values or List of vectors of values. | 
|  | 96 | #' @param width The width of the plot in pixel | 
|  | 97 | #' @param height The height of the plot in pixel | 
|  | 98 | #' @param res The resolution of the plot. Default is 300. | 
|  | 99 | #' @param add_label For boxplot. T/F to add labels for min, mean and max. | 
|  | 100 | #' @param label_digits If T for add_label, rounding digits for the label. | 
|  | 101 | #' Default is 2. | 
|  | 102 | #' @param same_lim T/F. If x is a list of vectors, should all the plots be | 
|  | 103 | #' plotted in the same range? Default is True. | 
|  | 104 | #' @param lim,xlim,ylim Manually specify plotting range in the form of | 
|  | 105 | #' `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] | 106 | #' and `ylim` are used in `spec_plot`. | 
| Hao Zhu | defd189 | 2020-09-09 00:08:09 -0400 | [diff] [blame] | 107 | #' @param xaxt On/Off for xaxis text | 
|  | 108 | #' @param yaxt On/Off for yaxis text | 
|  | 109 | #' @param ann On/Off for annotations (titles and axis titles) | 
|  | 110 | #' @param col Color for the fill of the histogram bar/boxplot box. | 
|  | 111 | #' @param border Color for the border. | 
|  | 112 | #' @param boxlty Boxplot - box boarder type | 
|  | 113 | #' @param medcol Boxplot - median line color | 
|  | 114 | #' @param medlwd Boxplot - median line width | 
|  | 115 | #' @param dir Directory of where the images will be saved. | 
|  | 116 | #' @param file File name. If not provided, a random name will be used | 
| Bill Evans | 95a0428 | 2020-09-14 12:39:25 -0700 | [diff] [blame] | 117 | #' @param file_type Graphic device. Can be character (e.g., `"pdf"`) | 
|  | 118 | #'   or a graphics device function (`grDevices::pdf`). This defaults | 
|  | 119 | #'   to `"pdf"` if the rendering is in LaTeX and `"svg"` otherwise. | 
| Hao Zhu | defd189 | 2020-09-09 00:08:09 -0400 | [diff] [blame] | 120 | #' @param ... extraparameters passing to boxplot | 
|  | 121 | #' | 
| Hao Zhu | 5fe235c | 2020-08-26 00:26:49 -0400 | [diff] [blame] | 122 | #' @export | 
|  | 123 | spec_boxplot <- function(x, width = 200, height = 50, res = 300, | 
|  | 124 | add_label = FALSE, label_digits = 2, | 
|  | 125 | same_lim = TRUE, lim = NULL, | 
|  | 126 | xaxt = 'n', yaxt = 'n', ann = FALSE, | 
|  | 127 | col = "lightgray", border = NULL, | 
|  | 128 | boxlty = 0, medcol = "red", medlwd = 1, | 
|  | 129 | dir = if (is_latex()) rmd_files_dir() else tempdir(), | 
|  | 130 | file = NULL, | 
| Bill Evans | 95a0428 | 2020-09-14 12:39:25 -0700 | [diff] [blame] | 131 | file_type = if (is_latex()) "pdf" else "svg", ...) { | 
| Hao Zhu | 5fe235c | 2020-08-26 00:26:49 -0400 | [diff] [blame] | 132 | if (is.list(x)) { | 
|  | 133 | if (same_lim & is.null(lim)) { | 
| Vincent Arel-Bundock | 4bdbdb8 | 2020-10-07 07:58:52 -0400 | [diff] [blame] | 134 | lim <- base::range(unlist(x), na.rm=TRUE) | 
| Hao Zhu | 5fe235c | 2020-08-26 00:26:49 -0400 | [diff] [blame] | 135 | } | 
| Bill Evans | 95a0428 | 2020-09-14 12:39:25 -0700 | [diff] [blame] | 136 |  | 
|  | 137 | dots <- listify_args(x, width, height, res, | 
|  | 138 | add_label, label_digits, | 
|  | 139 | lim, xaxt, yaxt, ann, col, border, | 
|  | 140 | dir, file, file_type, | 
|  | 141 | lengths = c(1, length(x))) | 
|  | 142 | return(do.call(Map, c(list(f = spec_boxplot), dots))) | 
| Hao Zhu | 5fe235c | 2020-08-26 00:26:49 -0400 | [diff] [blame] | 143 | } | 
|  | 144 |  | 
| Bill Evans | 8def4da | 2020-09-11 09:58:26 -0700 | [diff] [blame] | 145 | if (is.null(x)) return(NULL) | 
|  | 146 |  | 
| Hao Zhu | 5fe235c | 2020-08-26 00:26:49 -0400 | [diff] [blame] | 147 | if (is.null(lim)) { | 
| Vincent Arel-Bundock | 4bdbdb8 | 2020-10-07 07:58:52 -0400 | [diff] [blame] | 148 | lim <- base::range(x, na.rm=TRUE) | 
| Hao Zhu | 5fe235c | 2020-08-26 00:26:49 -0400 | [diff] [blame] | 149 | lim[1] <- lim[1] - (lim[2] - lim[1]) / 10 | 
|  | 150 | lim[2] <- (lim[2] - lim[1]) / 10 + lim[2] | 
|  | 151 | } | 
|  | 152 |  | 
| Hao Zhu | 5fe235c | 2020-08-26 00:26:49 -0400 | [diff] [blame] | 153 | if (!dir.exists(dir)) { | 
|  | 154 | dir.create(dir) | 
|  | 155 | } | 
|  | 156 |  | 
| Bill Evans | 95a0428 | 2020-09-14 12:39:25 -0700 | [diff] [blame] | 157 | file_ext <- dev_chr(file_type) | 
| Hao Zhu | 5fe235c | 2020-08-26 00:26:49 -0400 | [diff] [blame] | 158 | if (is.null(file)) { | 
| Bill Evans | 95a0428 | 2020-09-14 12:39:25 -0700 | [diff] [blame] | 159 | file <- normalizePath( | 
|  | 160 | tempfile(pattern = "boxplot_", tmpdir = dir, fileext = paste0(".", file_ext)), | 
|  | 161 | winslash = "/", mustWork = FALSE) | 
| Hao Zhu | 5fe235c | 2020-08-26 00:26:49 -0400 | [diff] [blame] | 162 | } | 
|  | 163 |  | 
| Bill Evans | 95a0428 | 2020-09-14 12:39:25 -0700 | [diff] [blame] | 164 | graphics_dev(filename = file, dev = file_type, | 
|  | 165 | width = width, height = height, res = res, | 
|  | 166 | bg = "transparent") | 
|  | 167 | curdev <- grDevices::dev.cur() | 
|  | 168 | on.exit(grDevices::dev.off(curdev), add = TRUE) | 
| Hao Zhu | 5fe235c | 2020-08-26 00:26:49 -0400 | [diff] [blame] | 169 |  | 
|  | 170 | graphics::par(mar = c(0, 0, 0, 0)) | 
|  | 171 |  | 
|  | 172 | graphics::boxplot(x, horizontal = TRUE, ann = ann, frame = FALSE, bty = 'n', ylim = lim, | 
|  | 173 | col = col, border = border, | 
|  | 174 | boxlty = boxlty, medcol = medcol, medlwd = medlwd, | 
|  | 175 | axes = FALSE, outcex = 0.2, whisklty = 1, | 
|  | 176 | ...) | 
|  | 177 | if (add_label) { | 
|  | 178 | x_median <- round(median(x, na.rm = T), label_digits) | 
|  | 179 | x_min <- round(min(x, na.rm = T), label_digits) | 
|  | 180 | x_max <- round(max(x, na.rm = T), label_digits) | 
|  | 181 | graphics::text(x_median, y = 1.4, labels = x_median, cex = 0.5) | 
|  | 182 | graphics::text(x_min, y = 0.6, labels = x_min, cex = 0.5) | 
|  | 183 | graphics::text(x_max, y = 0.6, labels = x_max, cex = 0.5) | 
|  | 184 | } | 
| Hao Zhu | 5fe235c | 2020-08-26 00:26:49 -0400 | [diff] [blame] | 185 |  | 
| Bill Evans | 95a0428 | 2020-09-14 12:39:25 -0700 | [diff] [blame] | 186 | grDevices::dev.off(curdev) | 
|  | 187 |  | 
|  | 188 | out <- make_inline_plot( | 
|  | 189 | file, file_ext, file_type, | 
|  | 190 | width, height, res, | 
|  | 191 | del = TRUE) | 
| Hao Zhu | 5fe235c | 2020-08-26 00:26:49 -0400 | [diff] [blame] | 192 | return(out) | 
|  | 193 | } | 
|  | 194 |  | 
|  | 195 | is_latex <- knitr::is_latex_output | 
|  | 196 |  | 
|  | 197 | rmd_files_dir <- function(create = TRUE) { | 
|  | 198 | curr_file_name <- sub("\\.[^\\.]*$", "", knitr::current_input()) | 
|  | 199 | dir_name <- paste0(curr_file_name, "_files") | 
|  | 200 | if (!dir.exists(dir_name) & create) dir.create(dir_name) | 
| Hao Zhu | 7f3fa85 | 2020-08-26 13:55:38 -0400 | [diff] [blame] | 201 | fig_dir_name <- file.path(dir_name, "figure-latex/") | 
| Hao Zhu | 5fe235c | 2020-08-26 00:26:49 -0400 | [diff] [blame] | 202 | if (!dir.exists(fig_dir_name) & create) dir.create(fig_dir_name) | 
|  | 203 | return(fig_dir_name) | 
|  | 204 | } | 
|  | 205 |  | 
| Hao Zhu | defd189 | 2020-09-09 00:08:09 -0400 | [diff] [blame] | 206 | #' Helper functions to generate inline sparklines | 
|  | 207 | #' | 
|  | 208 | #' @description These functions helps you quickly generate sets of sparkline | 
|  | 209 | #' style plots using base R plotting system. Currently, we support histogram, | 
|  | 210 | #' boxplot, and line. You can use them together with `column_spec` to | 
|  | 211 | #' generate inline plot in tables. By default, this function will save images | 
|  | 212 | #' in a folder called "kableExtra" and return the address of the file. | 
|  | 213 | #' | 
|  | 214 | #' @param x,y Vector of values or List of vectors of values. y is optional. | 
|  | 215 | #' @param width The width of the plot in pixel | 
|  | 216 | #' @param height The height of the plot in pixel | 
|  | 217 | #' @param res The resolution of the plot. Default is 300. | 
|  | 218 | #' @param same_lim T/F. If x is a list of vectors, should all the plots be | 
|  | 219 | #' plotted in the same range? Default is True. | 
|  | 220 | #' @param xlim,ylim Manually specify plotting range in the form of | 
|  | 221 | #' `c(0, 10)`. | 
|  | 222 | #' @param xaxt On/Off for xaxis text | 
|  | 223 | #' @param yaxt On/Off for yaxis text | 
|  | 224 | #' @param ann On/Off for annotations (titles and axis titles) | 
|  | 225 | #' @param col Color for the fill of the histogram bar/boxplot box. | 
|  | 226 | #' @param border Color for the border. | 
| Bill Evans | 548d715 | 2020-09-13 21:44:24 -0700 | [diff] [blame] | 227 | #' @param frame.plot On/Off for surrounding box (`spec_plot` only). Default | 
| Hao Zhu | defd189 | 2020-09-09 00:08:09 -0400 | [diff] [blame] | 228 | #' is False. | 
| Bill Evans | 548d715 | 2020-09-13 21:44:24 -0700 | [diff] [blame] | 229 | #' @param lwd Line width for `spec_plot`; within `spec_plot`, the `minmax` | 
| Hao Zhu | defd189 | 2020-09-09 00:08:09 -0400 | [diff] [blame] | 230 | #' 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] | 231 | #' @param pch,cex Shape and size for points (if type is other than "l"). | 
|  | 232 | #' @param type Passed to `plot`, often one of "l", "p", or "b", see | 
|  | 233 | #' [graphics::plot.default()] for more details. Ignored when 'polymin' is | 
|  | 234 | #' not 'NA'. | 
|  | 235 | #' @param polymin Special argument that converts a "line" to a polygon, | 
|  | 236 | #' where the flat portion is this value, and the other side of the polygon | 
|  | 237 | #' 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] | 238 | #' this is ignored; otherwise if this is numeric then a polygon is | 
|  | 239 | #' created (and 'type' is ignored). Note that if 'polymin' is in the middle | 
|  | 240 | #' 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] | 241 | #' @param minmax,min,max Arguments passed to `points` to highlight minimum | 
| Bill Evans | 548d715 | 2020-09-13 21:44:24 -0700 | [diff] [blame] | 242 | #' 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] | 243 | #' default to the value of `minmax`. Set to an empty `list()` to disable. | 
|  | 244 | #' @param dir Directory of where the images will be saved. | 
|  | 245 | #' @param file File name. If not provided, a random name will be used | 
| Bill Evans | b62414a | 2020-09-14 12:33:38 -0700 | [diff] [blame] | 246 | #' @param file_type Graphic device. Can be character (e.g., `"pdf"`) | 
|  | 247 | #'   or a graphics device function (`grDevices::pdf`). This defaults | 
|  | 248 | #'   to `"pdf"` if the rendering is in LaTeX and `"svg"` otherwise. | 
| Hao Zhu | defd189 | 2020-09-09 00:08:09 -0400 | [diff] [blame] | 249 | #' @param ... extra parameters passing to `plot` | 
|  | 250 | #' | 
| Bill Evans | cebc971 | 2020-08-30 19:55:24 -0700 | [diff] [blame] | 251 | #' @export | 
| Bill Evans | 548d715 | 2020-09-13 21:44:24 -0700 | [diff] [blame] | 252 | spec_plot <- function(x, y = NULL, width = 200, height = 50, res = 300, | 
| Bill Evans | cebc971 | 2020-08-30 19:55:24 -0700 | [diff] [blame] | 253 | same_lim = TRUE, xlim = NULL, ylim = NULL, | 
|  | 254 | xaxt = 'n', yaxt = 'n', ann = FALSE, | 
|  | 255 | col = "lightgray", border = NULL, | 
|  | 256 | frame.plot = FALSE, lwd = 2, | 
| Bill Evans | ad86c07 | 2020-09-13 21:54:52 -0700 | [diff] [blame] | 257 | pch = ".", cex = 2, type = "l", polymin = NA, | 
|  | 258 | minmax = list(pch = ".", cex = cex, col = "red"), | 
| Bill Evans | cebc971 | 2020-08-30 19:55:24 -0700 | [diff] [blame] | 259 | min = minmax, max = minmax, | 
|  | 260 | dir = if (is_latex()) rmd_files_dir() else tempdir(), | 
| Bill Evans | b62414a | 2020-09-14 12:33:38 -0700 | [diff] [blame] | 261 | file = NULL, file_type = if (is_latex()) "pdf" else "svg", ...) { | 
| Bill Evans | cebc971 | 2020-08-30 19:55:24 -0700 | [diff] [blame] | 262 | if (is.list(x)) { | 
| Bill Evans | 60fd80b | 2020-09-11 10:40:25 -0700 | [diff] [blame] | 263 | lenx <- length(x) | 
|  | 264 |  | 
| Bill Evans | cebc971 | 2020-08-30 19:55:24 -0700 | [diff] [blame] | 265 | if (same_lim) { | 
|  | 266 | if (is.null(xlim)) { | 
| Bill Evans | ad86c07 | 2020-09-13 21:54:52 -0700 | [diff] [blame] | 267 | xlim <- base::range(unlist(x), na.rm = TRUE) | 
| Bill Evans | cebc971 | 2020-08-30 19:55:24 -0700 | [diff] [blame] | 268 | } | 
|  | 269 | if (is.null(ylim) && !is.null(y)) { | 
| Bill Evans | ad86c07 | 2020-09-13 21:54:52 -0700 | [diff] [blame] | 270 | ylim <- base::range(c(unlist(y), polymin), na.rm = TRUE) | 
| Bill Evans | cebc971 | 2020-08-30 19:55:24 -0700 | [diff] [blame] | 271 | } | 
|  | 272 | } | 
| Bill Evans | ad86c07 | 2020-09-13 21:54:52 -0700 | [diff] [blame] | 273 |  | 
| Bill Evans | cebc971 | 2020-08-30 19:55:24 -0700 | [diff] [blame] | 274 | if (is.null(y)) { | 
| Bill Evans | ad86c07 | 2020-09-13 21:54:52 -0700 | [diff] [blame] | 275 | y <- list(y) | 
|  | 276 | } else if (length(y) != lenx) { | 
| Bill Evans | cebc971 | 2020-08-30 19:55:24 -0700 | [diff] [blame] | 277 | stop("'x' and 'y' are not the same length") | 
|  | 278 | } | 
| Bill Evans | 60fd80b | 2020-09-11 10:40:25 -0700 | [diff] [blame] | 279 |  | 
| Bill Evans | b62414a | 2020-09-14 12:33:38 -0700 | [diff] [blame] | 280 | dots <- listify_args(x, y = y, width, height, res, | 
|  | 281 | xlim, ylim, xaxt, yaxt, ann, col, border, frame.plot, | 
|  | 282 | lwd, pch, cex, type, polymin, minmax, min, max, | 
|  | 283 | dir, file, file_type, | 
|  | 284 | lengths = c(1, lenx)) | 
| Bill Evans | 60fd80b | 2020-09-11 10:40:25 -0700 | [diff] [blame] | 285 |  | 
| Bill Evans | 548d715 | 2020-09-13 21:44:24 -0700 | [diff] [blame] | 286 | return(do.call(Map, c(list(f = spec_plot), dots))) | 
| Bill Evans | 60fd80b | 2020-09-11 10:40:25 -0700 | [diff] [blame] | 287 |  | 
| Bill Evans | cebc971 | 2020-08-30 19:55:24 -0700 | [diff] [blame] | 288 | } | 
|  | 289 |  | 
| Bill Evans | 8def4da | 2020-09-11 09:58:26 -0700 | [diff] [blame] | 290 | if (is.null(x)) return(NULL) | 
|  | 291 |  | 
| Bill Evans | cebc971 | 2020-08-30 19:55:24 -0700 | [diff] [blame] | 292 | if (is.null(y) || !length(y)) { | 
|  | 293 | y <- x | 
| Bill Evans | a8ef1fb | 2020-09-12 22:55:46 -0700 | [diff] [blame] | 294 | x <- seq_along(y) | 
|  | 295 | if (!is.null(xlim) && is.null(ylim)) { | 
| Bill Evans | ad86c07 | 2020-09-13 21:54:52 -0700 | [diff] [blame] | 296 | ylim <- range(c(xlim, polymin), na.rm = TRUE) | 
| Bill Evans | a8ef1fb | 2020-09-12 22:55:46 -0700 | [diff] [blame] | 297 | xlim <- range(x) | 
|  | 298 | } | 
| Bill Evans | cebc971 | 2020-08-30 19:55:24 -0700 | [diff] [blame] | 299 | } | 
|  | 300 |  | 
|  | 301 | if (is.null(xlim)) { | 
| Bill Evans | ad86c07 | 2020-09-13 21:54:52 -0700 | [diff] [blame] | 302 | xlim <- base::range(x, na.rm = TRUE) | 
| Bill Evans | cebc971 | 2020-08-30 19:55:24 -0700 | [diff] [blame] | 303 | } | 
|  | 304 |  | 
|  | 305 | if (is.null(ylim) && !is.null(y)) { | 
| Bill Evans | a8ef1fb | 2020-09-12 22:55:46 -0700 | [diff] [blame] | 306 | ylim <- base::range(c(y, polymin), na.rm = TRUE) | 
| Bill Evans | cebc971 | 2020-08-30 19:55:24 -0700 | [diff] [blame] | 307 | } | 
|  | 308 |  | 
|  | 309 | if (is.null(min)) min <- minmax | 
|  | 310 | if (is.null(max)) max <- minmax | 
|  | 311 |  | 
|  | 312 | expand <- c( | 
| Bill Evans | 60fd80b | 2020-09-11 10:40:25 -0700 | [diff] [blame] | 313 | if (!is.null(min) && length(min)) -0.04 else 0, | 
|  | 314 | if (!is.null(max) && length(max)) +0.04 else 0) | 
|  | 315 | xlim <- xlim + diff(xlim) * expand | 
|  | 316 | ylim <- ylim + diff(ylim) * expand | 
| Bill Evans | cebc971 | 2020-08-30 19:55:24 -0700 | [diff] [blame] | 317 |  | 
| Bill Evans | cebc971 | 2020-08-30 19:55:24 -0700 | [diff] [blame] | 318 | if (!dir.exists(dir)) { | 
|  | 319 | dir.create(dir) | 
|  | 320 | } | 
|  | 321 |  | 
| Bill Evans | b62414a | 2020-09-14 12:33:38 -0700 | [diff] [blame] | 322 | file_ext <- dev_chr(file_type) | 
| Bill Evans | cebc971 | 2020-08-30 19:55:24 -0700 | [diff] [blame] | 323 | if (is.null(file)) { | 
| Bill Evans | b62414a | 2020-09-14 12:33:38 -0700 | [diff] [blame] | 324 | file <- normalizePath( | 
|  | 325 | tempfile(pattern = "plot_", tmpdir = dir, fileext = paste0(".", file_ext)), | 
|  | 326 | winslash = "/", mustWork = FALSE) | 
| Bill Evans | cebc971 | 2020-08-30 19:55:24 -0700 | [diff] [blame] | 327 | } | 
|  | 328 |  | 
| Bill Evans | b62414a | 2020-09-14 12:33:38 -0700 | [diff] [blame] | 329 | graphics_dev(filename = file, dev = file_type, | 
|  | 330 | width = width, height = height, res = res, | 
|  | 331 | bg = "transparent") | 
| Bill Evans | cebc971 | 2020-08-30 19:55:24 -0700 | [diff] [blame] | 332 | curdev <- grDevices::dev.cur() | 
|  | 333 | on.exit(grDevices::dev.off(curdev), add = TRUE) | 
|  | 334 |  | 
| Bill Evans | a8ef1fb | 2020-09-12 22:55:46 -0700 | [diff] [blame] | 335 | graphics::par(mar = c(0, 0, 0, 0), lwd = lwd) | 
|  | 336 |  | 
|  | 337 | dots <- list(...) | 
|  | 338 | if (!is.na(polymin) && "angle" %in% names(dots)) { | 
|  | 339 | angle <- dots$angle | 
|  | 340 | dots$angle <- NULL | 
|  | 341 | } else angle <- 45 | 
|  | 342 |  | 
|  | 343 | do.call(graphics::plot, | 
|  | 344 | c(list(x, y, type = if (is.na(polymin)) type else "n", | 
|  | 345 | xlim = xlim, ylim = ylim, | 
| Bill Evans | cebc971 | 2020-08-30 19:55:24 -0700 | [diff] [blame] | 346 | xaxt = xaxt, yaxt = yaxt, ann = ann, col = col, | 
| Bill Evans | ad86c07 | 2020-09-13 21:54:52 -0700 | [diff] [blame] | 347 | frame.plot = frame.plot, cex = cex, pch = pch), | 
|  | 348 | dots)) | 
| Bill Evans | a8ef1fb | 2020-09-12 22:55:46 -0700 | [diff] [blame] | 349 |  | 
|  | 350 | if (!is.na(polymin)) { | 
|  | 351 | lty <- if ("lty" %in% names(dots)) dots$lty else graphics::par("lty") | 
|  | 352 | polygon(c(x[1], x, x[length(x)]), c(polymin, y, polymin), | 
| Bill Evans | ad86c07 | 2020-09-13 21:54:52 -0700 | [diff] [blame] | 353 | border = NA, col = col, angle = angle, lty = lty, | 
|  | 354 | xpd = if ("xpd" %in% names(dots)) dots$xpd else NA) | 
| Bill Evans | a8ef1fb | 2020-09-12 22:55:46 -0700 | [diff] [blame] | 355 | } | 
| Bill Evans | cebc971 | 2020-08-30 19:55:24 -0700 | [diff] [blame] | 356 |  | 
|  | 357 | if (!is.null(min) && length(min)) { | 
| Bill Evans | ad86c07 | 2020-09-13 21:54:52 -0700 | [diff] [blame] | 358 | if (!"xpd" %in% names(min)) min$xpd <- NA | 
| Bill Evans | cebc971 | 2020-08-30 19:55:24 -0700 | [diff] [blame] | 359 | ind <- which.min(y) | 
| Bill Evans | ad86c07 | 2020-09-13 21:54:52 -0700 | [diff] [blame] | 360 | do.call(graphics::points, c(list(x[ind], y[ind]), min)) | 
| Bill Evans | cebc971 | 2020-08-30 19:55:24 -0700 | [diff] [blame] | 361 | } | 
|  | 362 |  | 
|  | 363 | if (!is.null(max) && length(max)) { | 
| Bill Evans | ad86c07 | 2020-09-13 21:54:52 -0700 | [diff] [blame] | 364 | if (!"xpd" %in% names(max)) max$xpd <- NA | 
| Bill Evans | cebc971 | 2020-08-30 19:55:24 -0700 | [diff] [blame] | 365 | ind <- which.max(y) | 
| Bill Evans | ad86c07 | 2020-09-13 21:54:52 -0700 | [diff] [blame] | 366 | do.call(graphics::points, c(list(x[ind], y[ind]), max)) | 
| Bill Evans | cebc971 | 2020-08-30 19:55:24 -0700 | [diff] [blame] | 367 | } | 
|  | 368 |  | 
|  | 369 | grDevices::dev.off(curdev) | 
|  | 370 |  | 
| Bill Evans | b62414a | 2020-09-14 12:33:38 -0700 | [diff] [blame] | 371 | out <- make_inline_plot( | 
|  | 372 | file, file_ext, file_type, | 
|  | 373 | width, height, res, | 
|  | 374 | del = TRUE) | 
| Bill Evans | cebc971 | 2020-08-30 19:55:24 -0700 | [diff] [blame] | 375 | return(out) | 
|  | 376 | } |