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, |
Hao Zhu | f6b60e8 | 2020-10-21 18:58:19 -0400 | [diff] [blame] | 5 | #' 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 Zhu | 5fe235c | 2020-08-26 00:26:49 -0400 | [diff] [blame] | 9 | #' |
| 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 Zhu | f6b60e8 | 2020-10-21 18:58:19 -0400 | [diff] [blame] | 14 | #' @param breaks The `break` option in `hist`. Default is "Sturges" but you can |
| 15 | #' also provide a vector to manually specify break points. |
Hao Zhu | 5fe235c | 2020-08-26 00:26:49 -0400 | [diff] [blame] | 16 | #' @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 Zhu | defd189 | 2020-09-09 00:08:09 -0400 | [diff] [blame] | 18 | #' @param lim Manually specify plotting range in the form of |
| 19 | #' `c(0, 10)`. |
Hao Zhu | 5fe235c | 2020-08-26 00:26:49 -0400 | [diff] [blame] | 20 | #' @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 Evans | 95a0428 | 2020-09-14 12:39:25 -0700 | [diff] [blame] | 27 | #' @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 Zhu | 5fe235c | 2020-08-26 00:26:49 -0400 | [diff] [blame] | 30 | #' for HTML output |
Hao Zhu | defd189 | 2020-09-09 00:08:09 -0400 | [diff] [blame] | 31 | #' @param ... extra parameters sending to `hist()` |
Hao Zhu | 5fe235c | 2020-08-26 00:26:49 -0400 | [diff] [blame] | 32 | #' |
Hao Zhu | 5fe235c | 2020-08-26 00:26:49 -0400 | [diff] [blame] | 33 | #' @export |
| 34 | spec_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 Zhu | 30db027 | 2021-02-19 13:02:29 -0500 | [diff] [blame] | 41 | file_type = if (is_latex()) "pdf" else svglite::svglite, |
| 42 | ...) { |
Hao Zhu | 5fe235c | 2020-08-26 00:26:49 -0400 | [diff] [blame] | 43 | if (is.list(x)) { |
| 44 | if (same_lim & is.null(lim)) { |
Vincent Arel-Bundock | 4bdbdb8 | 2020-10-07 07:58:52 -0400 | [diff] [blame] | 45 | lim <- base::range(unlist(x), na.rm=TRUE) |
Hao Zhu | 5fe235c | 2020-08-26 00:26:49 -0400 | [diff] [blame] | 46 | } |
Bill Evans | 95a0428 | 2020-09-14 12:39:25 -0700 | [diff] [blame] | 47 | |
| 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 Zhu | 5fe235c | 2020-08-26 00:26:49 -0400 | [diff] [blame] | 53 | } |
| 54 | |
Bill Evans | 8def4da | 2020-09-11 09:58:26 -0700 | [diff] [blame] | 55 | if (is.null(x)) return(NULL) |
| 56 | |
Hao Zhu | 5fe235c | 2020-08-26 00:26:49 -0400 | [diff] [blame] | 57 | if (is.null(lim)) { |
Vincent Arel-Bundock | 4bdbdb8 | 2020-10-07 07:58:52 -0400 | [diff] [blame] | 58 | lim <- base::range(x, na.rm=TRUE) |
Hao Zhu | 5fe235c | 2020-08-26 00:26:49 -0400 | [diff] [blame] | 59 | } |
| 60 | |
Hao Zhu | 5fe235c | 2020-08-26 00:26:49 -0400 | [diff] [blame] | 61 | if (!dir.exists(dir)) { |
| 62 | dir.create(dir) |
| 63 | } |
| 64 | |
Bill Evans | 95a0428 | 2020-09-14 12:39:25 -0700 | [diff] [blame] | 65 | file_ext <- dev_chr(file_type) |
Hao Zhu | 5fe235c | 2020-08-26 00:26:49 -0400 | [diff] [blame] | 66 | if (is.null(file)) { |
Bill Evans | 95a0428 | 2020-09-14 12:39:25 -0700 | [diff] [blame] | 67 | file <- normalizePath( |
| 68 | tempfile(pattern = "hist_", tmpdir = dir, fileext = paste0(".", file_ext)), |
| 69 | winslash = "/", mustWork = FALSE) |
Hao Zhu | 5fe235c | 2020-08-26 00:26:49 -0400 | [diff] [blame] | 70 | } |
| 71 | |
Bill Evans | 95a0428 | 2020-09-14 12:39:25 -0700 | [diff] [blame] | 72 | 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 Zhu | 5fe235c | 2020-08-26 00:26:49 -0400 | [diff] [blame] | 77 | |
| 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 Zhu | 5fe235c | 2020-08-26 00:26:49 -0400 | [diff] [blame] | 81 | |
Bill Evans | 95a0428 | 2020-09-14 12:39:25 -0700 | [diff] [blame] | 82 | grDevices::dev.off(curdev) |
Hao Zhu | 5fe235c | 2020-08-26 00:26:49 -0400 | [diff] [blame] | 83 | |
Bill Evans | 95a0428 | 2020-09-14 12:39:25 -0700 | [diff] [blame] | 84 | out <- make_inline_plot( |
| 85 | file, file_ext, file_type, |
| 86 | width, height, res, |
| 87 | del = TRUE) |
Hao Zhu | 5fe235c | 2020-08-26 00:26:49 -0400 | [diff] [blame] | 88 | return(out) |
| 89 | } |
| 90 | |
Hao Zhu | defd189 | 2020-09-09 00:08:09 -0400 | [diff] [blame] | 91 | #' 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 Zhu | f6b60e8 | 2020-10-21 18:58:19 -0400 | [diff] [blame] | 95 | #' 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 Zhu | defd189 | 2020-09-09 00:08:09 -0400 | [diff] [blame] | 99 | #' |
| 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 Zhu | f6b60e8 | 2020-10-21 18:58:19 -0400 | [diff] [blame] | 109 | #' @param lim Manually specify plotting range in the form of |
| 110 | #' `c(0, 10)`. |
Hao Zhu | defd189 | 2020-09-09 00:08:09 -0400 | [diff] [blame] | 111 | #' @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 Evans | 95a0428 | 2020-09-14 12:39:25 -0700 | [diff] [blame] | 121 | #' @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 Zhu | defd189 | 2020-09-09 00:08:09 -0400 | [diff] [blame] | 124 | #' @param ... extraparameters passing to boxplot |
| 125 | #' |
Hao Zhu | 5fe235c | 2020-08-26 00:26:49 -0400 | [diff] [blame] | 126 | #' @export |
| 127 | spec_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 Zhu | 30db027 | 2021-02-19 13:02:29 -0500 | [diff] [blame] | 135 | file_type = if (is_latex()) "pdf" else svglite::svglite, |
| 136 | ...) { |
Hao Zhu | 5fe235c | 2020-08-26 00:26:49 -0400 | [diff] [blame] | 137 | if (is.list(x)) { |
| 138 | if (same_lim & is.null(lim)) { |
Vincent Arel-Bundock | 4bdbdb8 | 2020-10-07 07:58:52 -0400 | [diff] [blame] | 139 | lim <- base::range(unlist(x), na.rm=TRUE) |
Hao Zhu | 5fe235c | 2020-08-26 00:26:49 -0400 | [diff] [blame] | 140 | } |
Bill Evans | 95a0428 | 2020-09-14 12:39:25 -0700 | [diff] [blame] | 141 | |
| 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 Zhu | 5fe235c | 2020-08-26 00:26:49 -0400 | [diff] [blame] | 148 | } |
| 149 | |
Bill Evans | 8def4da | 2020-09-11 09:58:26 -0700 | [diff] [blame] | 150 | if (is.null(x)) return(NULL) |
| 151 | |
Hao Zhu | 5fe235c | 2020-08-26 00:26:49 -0400 | [diff] [blame] | 152 | if (is.null(lim)) { |
Vincent Arel-Bundock | 4bdbdb8 | 2020-10-07 07:58:52 -0400 | [diff] [blame] | 153 | lim <- base::range(x, na.rm=TRUE) |
Hao Zhu | 5fe235c | 2020-08-26 00:26:49 -0400 | [diff] [blame] | 154 | lim[1] <- lim[1] - (lim[2] - lim[1]) / 10 |
| 155 | lim[2] <- (lim[2] - lim[1]) / 10 + lim[2] |
| 156 | } |
| 157 | |
Hao Zhu | 5fe235c | 2020-08-26 00:26:49 -0400 | [diff] [blame] | 158 | if (!dir.exists(dir)) { |
| 159 | dir.create(dir) |
| 160 | } |
| 161 | |
Bill Evans | 95a0428 | 2020-09-14 12:39:25 -0700 | [diff] [blame] | 162 | file_ext <- dev_chr(file_type) |
Hao Zhu | 5fe235c | 2020-08-26 00:26:49 -0400 | [diff] [blame] | 163 | if (is.null(file)) { |
Bill Evans | 95a0428 | 2020-09-14 12:39:25 -0700 | [diff] [blame] | 164 | file <- normalizePath( |
| 165 | tempfile(pattern = "boxplot_", tmpdir = dir, fileext = paste0(".", file_ext)), |
| 166 | winslash = "/", mustWork = FALSE) |
Hao Zhu | 5fe235c | 2020-08-26 00:26:49 -0400 | [diff] [blame] | 167 | } |
| 168 | |
Bill Evans | 95a0428 | 2020-09-14 12:39:25 -0700 | [diff] [blame] | 169 | 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 Zhu | 5fe235c | 2020-08-26 00:26:49 -0400 | [diff] [blame] | 174 | |
| 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 Zhu | f6b60e8 | 2020-10-21 18:58:19 -0400 | [diff] [blame] | 178 | col = col, border = border, |
| 179 | boxlty = boxlty, medcol = medcol, medlwd = medlwd, |
| 180 | axes = FALSE, outcex = 0.2, whisklty = 1, |
| 181 | ...) |
Hao Zhu | 5fe235c | 2020-08-26 00:26:49 -0400 | [diff] [blame] | 182 | 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 Zhu | 5fe235c | 2020-08-26 00:26:49 -0400 | [diff] [blame] | 190 | |
Bill Evans | 95a0428 | 2020-09-14 12:39:25 -0700 | [diff] [blame] | 191 | grDevices::dev.off(curdev) |
| 192 | |
| 193 | out <- make_inline_plot( |
| 194 | file, file_ext, file_type, |
| 195 | width, height, res, |
| 196 | del = TRUE) |
Hao Zhu | 5fe235c | 2020-08-26 00:26:49 -0400 | [diff] [blame] | 197 | return(out) |
| 198 | } |
| 199 | |
| 200 | is_latex <- knitr::is_latex_output |
| 201 | |
| 202 | rmd_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 Zhu | 7f3fa85 | 2020-08-26 13:55:38 -0400 | [diff] [blame] | 206 | fig_dir_name <- file.path(dir_name, "figure-latex/") |
Hao Zhu | 5fe235c | 2020-08-26 00:26:49 -0400 | [diff] [blame] | 207 | if (!dir.exists(fig_dir_name) & create) dir.create(fig_dir_name) |
| 208 | return(fig_dir_name) |
| 209 | } |
| 210 | |
Hao Zhu | defd189 | 2020-09-09 00:08:09 -0400 | [diff] [blame] | 211 | #' 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 Zhu | f6b60e8 | 2020-10-21 18:58:19 -0400 | [diff] [blame] | 215 | #' 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 Zhu | defd189 | 2020-09-09 00:08:09 -0400 | [diff] [blame] | 219 | #' |
| 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 Evans | 548d715 | 2020-09-13 21:44:24 -0700 | [diff] [blame] | 233 | #' @param frame.plot On/Off for surrounding box (`spec_plot` only). Default |
Hao Zhu | defd189 | 2020-09-09 00:08:09 -0400 | [diff] [blame] | 234 | #' is False. |
Bill Evans | 548d715 | 2020-09-13 21:44:24 -0700 | [diff] [blame] | 235 | #' @param lwd Line width for `spec_plot`; within `spec_plot`, the `minmax` |
Hao Zhu | defd189 | 2020-09-09 00:08:09 -0400 | [diff] [blame] | 236 | #' 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] | 237 | #' @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 Evans | ad86c07 | 2020-09-13 21:54:52 -0700 | [diff] [blame] | 244 | #' 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 Zhu | defd189 | 2020-09-09 00:08:09 -0400 | [diff] [blame] | 247 | #' @param minmax,min,max Arguments passed to `points` to highlight minimum |
Bill Evans | 548d715 | 2020-09-13 21:44:24 -0700 | [diff] [blame] | 248 | #' 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] | 249 | #' 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 Evans | b62414a | 2020-09-14 12:33:38 -0700 | [diff] [blame] | 252 | #' @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 Zhu | defd189 | 2020-09-09 00:08:09 -0400 | [diff] [blame] | 255 | #' @param ... extra parameters passing to `plot` |
| 256 | #' |
Bill Evans | cebc971 | 2020-08-30 19:55:24 -0700 | [diff] [blame] | 257 | #' @export |
Bill Evans | 548d715 | 2020-09-13 21:44:24 -0700 | [diff] [blame] | 258 | spec_plot <- function(x, y = NULL, width = 200, height = 50, res = 300, |
Bill Evans | cebc971 | 2020-08-30 19:55:24 -0700 | [diff] [blame] | 259 | 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 Evans | ad86c07 | 2020-09-13 21:54:52 -0700 | [diff] [blame] | 263 | pch = ".", cex = 2, type = "l", polymin = NA, |
| 264 | minmax = list(pch = ".", cex = cex, col = "red"), |
Bill Evans | cebc971 | 2020-08-30 19:55:24 -0700 | [diff] [blame] | 265 | min = minmax, max = minmax, |
| 266 | dir = if (is_latex()) rmd_files_dir() else tempdir(), |
Hao Zhu | 30db027 | 2021-02-19 13:02:29 -0500 | [diff] [blame] | 267 | file = NULL, file_type = if (is_latex()) "pdf" else svglite::svglite, |
| 268 | ...) { |
Bill Evans | cebc971 | 2020-08-30 19:55:24 -0700 | [diff] [blame] | 269 | if (is.list(x)) { |
Bill Evans | 60fd80b | 2020-09-11 10:40:25 -0700 | [diff] [blame] | 270 | lenx <- length(x) |
| 271 | |
Bill Evans | cebc971 | 2020-08-30 19:55:24 -0700 | [diff] [blame] | 272 | if (same_lim) { |
| 273 | if (is.null(xlim)) { |
Bill Evans | ad86c07 | 2020-09-13 21:54:52 -0700 | [diff] [blame] | 274 | xlim <- base::range(unlist(x), na.rm = TRUE) |
Bill Evans | cebc971 | 2020-08-30 19:55:24 -0700 | [diff] [blame] | 275 | } |
| 276 | if (is.null(ylim) && !is.null(y)) { |
Bill Evans | ad86c07 | 2020-09-13 21:54:52 -0700 | [diff] [blame] | 277 | ylim <- base::range(c(unlist(y), polymin), na.rm = TRUE) |
Bill Evans | cebc971 | 2020-08-30 19:55:24 -0700 | [diff] [blame] | 278 | } |
| 279 | } |
Bill Evans | ad86c07 | 2020-09-13 21:54:52 -0700 | [diff] [blame] | 280 | |
Bill Evans | cebc971 | 2020-08-30 19:55:24 -0700 | [diff] [blame] | 281 | if (is.null(y)) { |
Bill Evans | ad86c07 | 2020-09-13 21:54:52 -0700 | [diff] [blame] | 282 | y <- list(y) |
| 283 | } else if (length(y) != lenx) { |
Bill Evans | cebc971 | 2020-08-30 19:55:24 -0700 | [diff] [blame] | 284 | stop("'x' and 'y' are not the same length") |
| 285 | } |
Bill Evans | 60fd80b | 2020-09-11 10:40:25 -0700 | [diff] [blame] | 286 | |
Bill Evans | b62414a | 2020-09-14 12:33:38 -0700 | [diff] [blame] | 287 | 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 Evans | 60fd80b | 2020-09-11 10:40:25 -0700 | [diff] [blame] | 292 | |
Bill Evans | 548d715 | 2020-09-13 21:44:24 -0700 | [diff] [blame] | 293 | return(do.call(Map, c(list(f = spec_plot), dots))) |
Bill Evans | 60fd80b | 2020-09-11 10:40:25 -0700 | [diff] [blame] | 294 | |
Bill Evans | cebc971 | 2020-08-30 19:55:24 -0700 | [diff] [blame] | 295 | } |
| 296 | |
Bill Evans | 8def4da | 2020-09-11 09:58:26 -0700 | [diff] [blame] | 297 | if (is.null(x)) return(NULL) |
| 298 | |
Bill Evans | cebc971 | 2020-08-30 19:55:24 -0700 | [diff] [blame] | 299 | if (is.null(y) || !length(y)) { |
| 300 | y <- x |
Bill Evans | a8ef1fb | 2020-09-12 22:55:46 -0700 | [diff] [blame] | 301 | x <- seq_along(y) |
| 302 | if (!is.null(xlim) && is.null(ylim)) { |
Bill Evans | ad86c07 | 2020-09-13 21:54:52 -0700 | [diff] [blame] | 303 | ylim <- range(c(xlim, polymin), na.rm = TRUE) |
Bill Evans | a8ef1fb | 2020-09-12 22:55:46 -0700 | [diff] [blame] | 304 | xlim <- range(x) |
| 305 | } |
Bill Evans | cebc971 | 2020-08-30 19:55:24 -0700 | [diff] [blame] | 306 | } |
| 307 | |
| 308 | if (is.null(xlim)) { |
Bill Evans | ad86c07 | 2020-09-13 21:54:52 -0700 | [diff] [blame] | 309 | xlim <- base::range(x, na.rm = TRUE) |
Bill Evans | cebc971 | 2020-08-30 19:55:24 -0700 | [diff] [blame] | 310 | } |
| 311 | |
| 312 | if (is.null(ylim) && !is.null(y)) { |
Bill Evans | a8ef1fb | 2020-09-12 22:55:46 -0700 | [diff] [blame] | 313 | ylim <- base::range(c(y, polymin), na.rm = TRUE) |
Bill Evans | cebc971 | 2020-08-30 19:55:24 -0700 | [diff] [blame] | 314 | } |
| 315 | |
| 316 | if (is.null(min)) min <- minmax |
| 317 | if (is.null(max)) max <- minmax |
| 318 | |
| 319 | expand <- c( |
Bill Evans | 60fd80b | 2020-09-11 10:40:25 -0700 | [diff] [blame] | 320 | 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 Evans | cebc971 | 2020-08-30 19:55:24 -0700 | [diff] [blame] | 324 | |
Bill Evans | cebc971 | 2020-08-30 19:55:24 -0700 | [diff] [blame] | 325 | if (!dir.exists(dir)) { |
| 326 | dir.create(dir) |
| 327 | } |
| 328 | |
Bill Evans | b62414a | 2020-09-14 12:33:38 -0700 | [diff] [blame] | 329 | file_ext <- dev_chr(file_type) |
Bill Evans | cebc971 | 2020-08-30 19:55:24 -0700 | [diff] [blame] | 330 | if (is.null(file)) { |
Bill Evans | b62414a | 2020-09-14 12:33:38 -0700 | [diff] [blame] | 331 | file <- normalizePath( |
| 332 | tempfile(pattern = "plot_", tmpdir = dir, fileext = paste0(".", file_ext)), |
| 333 | winslash = "/", mustWork = FALSE) |
Bill Evans | cebc971 | 2020-08-30 19:55:24 -0700 | [diff] [blame] | 334 | } |
| 335 | |
Bill Evans | b62414a | 2020-09-14 12:33:38 -0700 | [diff] [blame] | 336 | graphics_dev(filename = file, dev = file_type, |
| 337 | width = width, height = height, res = res, |
| 338 | bg = "transparent") |
Bill Evans | cebc971 | 2020-08-30 19:55:24 -0700 | [diff] [blame] | 339 | curdev <- grDevices::dev.cur() |
| 340 | on.exit(grDevices::dev.off(curdev), add = TRUE) |
| 341 | |
Bill Evans | a8ef1fb | 2020-09-12 22:55:46 -0700 | [diff] [blame] | 342 | 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 Evans | cebc971 | 2020-08-30 19:55:24 -0700 | [diff] [blame] | 353 | xaxt = xaxt, yaxt = yaxt, ann = ann, col = col, |
Bill Evans | ad86c07 | 2020-09-13 21:54:52 -0700 | [diff] [blame] | 354 | frame.plot = frame.plot, cex = cex, pch = pch), |
| 355 | dots)) |
Bill Evans | a8ef1fb | 2020-09-12 22:55:46 -0700 | [diff] [blame] | 356 | |
| 357 | if (!is.na(polymin)) { |
| 358 | lty <- if ("lty" %in% names(dots)) dots$lty else graphics::par("lty") |
Hao Zhu | f6b60e8 | 2020-10-21 18:58:19 -0400 | [diff] [blame] | 359 | graphics::polygon(c(x[1], x, x[length(x)]), c(polymin, y, polymin), |
Bill Evans | ad86c07 | 2020-09-13 21:54:52 -0700 | [diff] [blame] | 360 | border = NA, col = col, angle = angle, lty = lty, |
| 361 | xpd = if ("xpd" %in% names(dots)) dots$xpd else NA) |
Bill Evans | a8ef1fb | 2020-09-12 22:55:46 -0700 | [diff] [blame] | 362 | } |
Bill Evans | cebc971 | 2020-08-30 19:55:24 -0700 | [diff] [blame] | 363 | |
| 364 | if (!is.null(min) && length(min)) { |
Bill Evans | ad86c07 | 2020-09-13 21:54:52 -0700 | [diff] [blame] | 365 | if (!"xpd" %in% names(min)) min$xpd <- NA |
Bill Evans | cebc971 | 2020-08-30 19:55:24 -0700 | [diff] [blame] | 366 | ind <- which.min(y) |
Bill Evans | ad86c07 | 2020-09-13 21:54:52 -0700 | [diff] [blame] | 367 | do.call(graphics::points, c(list(x[ind], y[ind]), min)) |
Bill Evans | cebc971 | 2020-08-30 19:55:24 -0700 | [diff] [blame] | 368 | } |
| 369 | |
| 370 | if (!is.null(max) && length(max)) { |
Bill Evans | ad86c07 | 2020-09-13 21:54:52 -0700 | [diff] [blame] | 371 | if (!"xpd" %in% names(max)) max$xpd <- NA |
Bill Evans | cebc971 | 2020-08-30 19:55:24 -0700 | [diff] [blame] | 372 | ind <- which.max(y) |
Bill Evans | ad86c07 | 2020-09-13 21:54:52 -0700 | [diff] [blame] | 373 | do.call(graphics::points, c(list(x[ind], y[ind]), max)) |
Bill Evans | cebc971 | 2020-08-30 19:55:24 -0700 | [diff] [blame] | 374 | } |
| 375 | |
| 376 | grDevices::dev.off(curdev) |
| 377 | |
Bill Evans | b62414a | 2020-09-14 12:33:38 -0700 | [diff] [blame] | 378 | out <- make_inline_plot( |
| 379 | file, file_ext, file_type, |
| 380 | width, height, res, |
| 381 | del = TRUE) |
Bill Evans | cebc971 | 2020-08-30 19:55:24 -0700 | [diff] [blame] | 382 | return(out) |
| 383 | } |
Hao Zhu | f6b60e8 | 2020-10-21 18:58:19 -0400 | [diff] [blame] | 384 | |
| 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) |
Hao Zhu | 7898891 | 2021-02-23 11:00:56 -0500 | [diff] [blame^] | 408 | #' @param col Color for mean dot. |
| 409 | #' @param line_col Color for the line and the error bar. |
Hao Zhu | f6b60e8 | 2020-10-21 18:58:19 -0400 | [diff] [blame] | 410 | #' @param cex size of the mean dot and error bar size. |
| 411 | #' @param frame.plot T/F for whether to plot the plot frames. |
| 412 | #' @param dir Directory of where the images will be saved. |
| 413 | #' @param file File name. If not provided, a random name will be used |
| 414 | #' @param file_type Graphic device. Can be character (e.g., `"pdf"`) |
| 415 | #' or a graphics device function (`grDevices::pdf`). This defaults |
| 416 | #' to `"pdf"` if the rendering is in LaTeX and `"svg"` otherwise. |
| 417 | #' for HTML output |
| 418 | #' @param ... extra parameters sending to `hist()` |
| 419 | #' |
| 420 | #' @export |
| 421 | spec_pointrange <- function( |
| 422 | x, xmin, xmax, vline = NULL, |
| 423 | width = 200, height = 50, res = 300, |
| 424 | same_lim = TRUE, lim = NULL, |
| 425 | xaxt = 'n', yaxt = 'n', ann = FALSE, |
Hao Zhu | 7898891 | 2021-02-23 11:00:56 -0500 | [diff] [blame^] | 426 | col = "red", line_col = "black", cex = 0.3, frame.plot = FALSE, |
Hao Zhu | f6b60e8 | 2020-10-21 18:58:19 -0400 | [diff] [blame] | 427 | dir = if (is_latex()) rmd_files_dir() else tempdir(), |
| 428 | file = NULL, |
Hao Zhu | 30db027 | 2021-02-19 13:02:29 -0500 | [diff] [blame] | 429 | file_type = if (is_latex()) "pdf" else svglite::svglite, ...) { |
Hao Zhu | f6b60e8 | 2020-10-21 18:58:19 -0400 | [diff] [blame] | 430 | if (length(x) > 1) { |
| 431 | if (same_lim & is.null(lim)) { |
| 432 | all_range <- c(unlist(xmin), unlist(xmax)) |
| 433 | lim <- base::range(all_range, na.rm=TRUE) |
| 434 | lim <- lim + c(-0.04 * diff(lim), 0.04 * diff(lim)) |
| 435 | } |
| 436 | |
| 437 | dots <- listify_args( |
| 438 | x = as.list(x), xmin = as.list(xmin), xmax = as.list(xmax), vline, |
| 439 | width, height, res, |
Hao Zhu | 7898891 | 2021-02-23 11:00:56 -0500 | [diff] [blame^] | 440 | lim, xaxt, yaxt, ann, col, line_col, cex, frame.plot, |
Hao Zhu | f6b60e8 | 2020-10-21 18:58:19 -0400 | [diff] [blame] | 441 | dir, file, file_type, |
| 442 | lengths = c(1, length(x)), |
| 443 | passthru = c("x", "xmin", "xmax")) |
| 444 | return(do.call(Map, c(list(f = spec_pointrange), dots))) |
| 445 | } |
| 446 | |
| 447 | if (is.null(x)) return(NULL) |
| 448 | |
| 449 | if (is.null(lim)) { |
| 450 | one_range <- unlist(c(xmin, xmax)) |
| 451 | lim <- base::range(one_range, na.rm=TRUE) |
| 452 | lim <- lim + c(-0.04 * diff(lim), 0.04 * diff(lim)) |
| 453 | } |
| 454 | |
| 455 | if (!dir.exists(dir)) { |
| 456 | dir.create(dir) |
| 457 | } |
| 458 | |
| 459 | file_ext <- dev_chr(file_type) |
| 460 | if (is.null(file)) { |
| 461 | file <- normalizePath( |
| 462 | tempfile(pattern = "pointrange_", tmpdir = dir, fileext = paste0(".", file_ext)), |
| 463 | winslash = "/", mustWork = FALSE) |
| 464 | } |
| 465 | |
| 466 | graphics_dev(filename = file, dev = file_type, |
| 467 | width = width, height = height, res = res, |
| 468 | bg = "transparent") |
| 469 | curdev <- grDevices::dev.cur() |
| 470 | on.exit(grDevices::dev.off(curdev), add = TRUE) |
| 471 | |
| 472 | graphics::par(mar = c(0, 0, 0.2, 0), lwd=1, |
| 473 | ann = ann, xaxt = xaxt, yaxt = yaxt) |
| 474 | |
| 475 | graphics::plot(x, 0, type = "p", pch = ".", |
| 476 | xlim = lim, frame.plot = frame.plot) |
Hao Zhu | 7898891 | 2021-02-23 11:00:56 -0500 | [diff] [blame^] | 477 | graphics::arrows(xmin, 0, xmax, 0, cex / 15, angle = 90, code = 3, |
| 478 | col = line_col) |
Hao Zhu | f6b60e8 | 2020-10-21 18:58:19 -0400 | [diff] [blame] | 479 | graphics::points(x, 0, col = col, type = "p", pch = 15, cex = cex) |
| 480 | if (!is.null(vline)) { |
| 481 | graphics::abline(v = vline, lty = 3) |
| 482 | } |
| 483 | |
| 484 | grDevices::dev.off(curdev) |
| 485 | |
| 486 | out <- make_inline_plot( |
| 487 | file, file_ext, file_type, |
| 488 | width, height, res, |
| 489 | del = TRUE) |
| 490 | return(out) |
| 491 | } |