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 | } |
Jebu | 38a2724 | 2021-12-16 19:25:33 +0100 | [diff] [blame] | 199 | #' Helper functions to generate inline sparklines |
| 200 | #' |
| 201 | #' @description These functions helps you quickly generate sets of sparkline |
| 202 | #' style plots using base R plotting system. Currently, we support histogram, |
| 203 | #' boxplot, line, scatter, pointrange, barplot plots. You can use them together with |
| 204 | #' `column_spec` to generate inline plot in tables. By default, this function |
| 205 | #' will save images in a folder called "kableExtra" and return the address of |
| 206 | #' the file. |
| 207 | #' |
| 208 | #' @param x Vector of values or List of vectors of values. |
| 209 | #' @param width The width of the plot in pixel |
| 210 | #' @param height The height of the plot in pixel |
| 211 | #' @param res The resolution of the plot. Default is 300. |
| 212 | #' @param add_label For boxplot. T/F to add labels for min, mean and max. |
| 213 | #' @param label_digits If T for add_label, rounding digits for the label. |
| 214 | #' Default is 2. |
| 215 | #' @param same_lim T/F. If x is a list of vectors, should all the plots be |
| 216 | #' plotted in the same range? Default is True. |
| 217 | #' @param lim Manually specify plotting range in the form of |
| 218 | #' `c(0, 10)`. |
| 219 | #' @param xaxt On/Off for xaxis text |
| 220 | #' @param yaxt On/Off for yaxis text |
| 221 | #' @param ann On/Off for annotations (titles and axis titles) |
| 222 | #' @param col Color for the fill of the histogram bar/boxplot box. |
| 223 | #' @param border Color for the border. |
| 224 | #' @param boxlty Boxplot - box boarder type |
| 225 | #' @param medcol Boxplot - median line color |
| 226 | #' @param medlwd Boxplot - median line width |
| 227 | #' @param dir Directory of where the images will be saved. |
| 228 | #' @param file File name. If not provided, a random name will be used |
| 229 | #' @param file_type Graphic device. Can be character (e.g., `"pdf"`) |
| 230 | #' or a graphics device function (`grDevices::pdf`). This defaults |
| 231 | #' to `"pdf"` if the rendering is in LaTeX and `"svg"` otherwise. |
| 232 | #' @param ... extraparameters passing to boxplot |
| 233 | #' |
| 234 | #' @export |
| 235 | spec_barplot <- function(x, devwidth = 200, devheight = 40, res = 300, |
| 236 | beside = F, |
| 237 | horiz = F, |
| 238 | same_lim = TRUE, lim = NULL, |
| 239 | xaxt = 'n', yaxt = 'n', ann = FALSE, |
| 240 | col = NULL, border = NA, |
| 241 | dir = if (is_latex()) rmd_files_dir() else tempdir(), |
| 242 | file = NULL, |
| 243 | file_type = if (is_latex()) "pdf" else svglite::svglite, |
| 244 | ...) { |
| 245 | if (is.list(x)) { |
| 246 | if (same_lim & is.null(lim)) { |
| 247 | lim <- base::range(unlist(x), na.rm=TRUE) |
| 248 | } |
| 249 | |
| 250 | dots <- listify_args(x, devwidth, devheight, res, beside,horiz, |
| 251 | lim, xaxt, yaxt, ann, col, border, |
| 252 | dir, file, file_type, |
| 253 | lengths = c(1, length(x))) |
| 254 | return(do.call(Map, c(list(f = spec_barplot), dots))) |
| 255 | } |
| 256 | |
| 257 | if (is.null(x)) return(NULL) |
| 258 | |
| 259 | if (is.null(lim)) { |
| 260 | lim <- base::range(x, na.rm=TRUE) |
| 261 | } |
| 262 | |
| 263 | if (!dir.exists(dir)) { |
| 264 | dir.create(dir) |
| 265 | } |
| 266 | height<-matrix(x) |
| 267 | height<-cbind(height,0) |
| 268 | file_ext <- dev_chr(file_type) |
| 269 | if (is.null(file)) { |
| 270 | file <- normalizePath( |
| 271 | tempfile(pattern = "barplot_", tmpdir = dir, fileext = paste0(".", file_ext)), |
| 272 | winslash = "/", mustWork = FALSE) |
| 273 | } |
| 274 | |
| 275 | graphics_dev(filename = file, dev = file_type, |
| 276 | width = devwidth, height = devheight, res = res, |
| 277 | bg = "transparent") |
| 278 | curdev <- grDevices::dev.cur() |
| 279 | on.exit(grDevices::dev.off(curdev), add = TRUE) |
| 280 | |
| 281 | graphics::par(mar = c(0, 0, 0, 0), lwd=0.5) |
| 282 | graphics::barplot(height=height, beside = beside,horiz = horiz, col = col, border = border,xaxt = xaxt, yaxt = yaxt, ann = ann)#,xlim = lim, ann = ann, ...) |
| 283 | |
| 284 | grDevices::dev.off(curdev) |
| 285 | |
| 286 | out <- make_inline_plot( |
| 287 | file, file_ext, file_type, |
| 288 | devwidth, devheight, res, |
| 289 | del = TRUE) |
| 290 | return(out) |
| 291 | } |
Hao Zhu | 5fe235c | 2020-08-26 00:26:49 -0400 | [diff] [blame] | 292 | |
| 293 | is_latex <- knitr::is_latex_output |
| 294 | |
| 295 | rmd_files_dir <- function(create = TRUE) { |
| 296 | curr_file_name <- sub("\\.[^\\.]*$", "", knitr::current_input()) |
| 297 | dir_name <- paste0(curr_file_name, "_files") |
| 298 | if (!dir.exists(dir_name) & create) dir.create(dir_name) |
Hao Zhu | 7f3fa85 | 2020-08-26 13:55:38 -0400 | [diff] [blame] | 299 | fig_dir_name <- file.path(dir_name, "figure-latex/") |
Hao Zhu | 5fe235c | 2020-08-26 00:26:49 -0400 | [diff] [blame] | 300 | if (!dir.exists(fig_dir_name) & create) dir.create(fig_dir_name) |
| 301 | return(fig_dir_name) |
| 302 | } |
| 303 | |
Hao Zhu | defd189 | 2020-09-09 00:08:09 -0400 | [diff] [blame] | 304 | #' Helper functions to generate inline sparklines |
| 305 | #' |
| 306 | #' @description These functions helps you quickly generate sets of sparkline |
| 307 | #' style plots using base R plotting system. Currently, we support histogram, |
Hao Zhu | f6b60e8 | 2020-10-21 18:58:19 -0400 | [diff] [blame] | 308 | #' boxplot, line, scatter and pointrange plots. You can use them together with |
| 309 | #' `column_spec` to generate inline plot in tables. By default, this function |
| 310 | #' will save images in a folder called "kableExtra" and return the address of |
| 311 | #' the file. |
Hao Zhu | defd189 | 2020-09-09 00:08:09 -0400 | [diff] [blame] | 312 | #' |
| 313 | #' @param x,y Vector of values or List of vectors of values. y is optional. |
| 314 | #' @param width The width of the plot in pixel |
| 315 | #' @param height The height of the plot in pixel |
| 316 | #' @param res The resolution of the plot. Default is 300. |
| 317 | #' @param same_lim T/F. If x is a list of vectors, should all the plots be |
| 318 | #' plotted in the same range? Default is True. |
| 319 | #' @param xlim,ylim Manually specify plotting range in the form of |
| 320 | #' `c(0, 10)`. |
| 321 | #' @param xaxt On/Off for xaxis text |
| 322 | #' @param yaxt On/Off for yaxis text |
| 323 | #' @param ann On/Off for annotations (titles and axis titles) |
| 324 | #' @param col Color for the fill of the histogram bar/boxplot box. |
| 325 | #' @param border Color for the border. |
Bill Evans | 548d715 | 2020-09-13 21:44:24 -0700 | [diff] [blame] | 326 | #' @param frame.plot On/Off for surrounding box (`spec_plot` only). Default |
Hao Zhu | defd189 | 2020-09-09 00:08:09 -0400 | [diff] [blame] | 327 | #' is False. |
Bill Evans | 548d715 | 2020-09-13 21:44:24 -0700 | [diff] [blame] | 328 | #' @param lwd Line width for `spec_plot`; within `spec_plot`, the `minmax` |
Hao Zhu | defd189 | 2020-09-09 00:08:09 -0400 | [diff] [blame] | 329 | #' 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] | 330 | #' @param pch,cex Shape and size for points (if type is other than "l"). |
| 331 | #' @param type Passed to `plot`, often one of "l", "p", or "b", see |
| 332 | #' [graphics::plot.default()] for more details. Ignored when 'polymin' is |
| 333 | #' not 'NA'. |
| 334 | #' @param polymin Special argument that converts a "line" to a polygon, |
| 335 | #' where the flat portion is this value, and the other side of the polygon |
| 336 | #' 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] | 337 | #' this is ignored; otherwise if this is numeric then a polygon is |
| 338 | #' created (and 'type' is ignored). Note that if 'polymin' is in the middle |
| 339 | #' 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] | 340 | #' @param minmax,min,max Arguments passed to `points` to highlight minimum |
Bill Evans | 548d715 | 2020-09-13 21:44:24 -0700 | [diff] [blame] | 341 | #' 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] | 342 | #' default to the value of `minmax`. Set to an empty `list()` to disable. |
| 343 | #' @param dir Directory of where the images will be saved. |
| 344 | #' @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] | 345 | #' @param file_type Graphic device. Can be character (e.g., `"pdf"`) |
| 346 | #' or a graphics device function (`grDevices::pdf`). This defaults |
| 347 | #' to `"pdf"` if the rendering is in LaTeX and `"svg"` otherwise. |
Hao Zhu | defd189 | 2020-09-09 00:08:09 -0400 | [diff] [blame] | 348 | #' @param ... extra parameters passing to `plot` |
| 349 | #' |
Bill Evans | cebc971 | 2020-08-30 19:55:24 -0700 | [diff] [blame] | 350 | #' @export |
Bill Evans | 548d715 | 2020-09-13 21:44:24 -0700 | [diff] [blame] | 351 | spec_plot <- function(x, y = NULL, width = 200, height = 50, res = 300, |
Bill Evans | cebc971 | 2020-08-30 19:55:24 -0700 | [diff] [blame] | 352 | same_lim = TRUE, xlim = NULL, ylim = NULL, |
| 353 | xaxt = 'n', yaxt = 'n', ann = FALSE, |
| 354 | col = "lightgray", border = NULL, |
| 355 | frame.plot = FALSE, lwd = 2, |
Bill Evans | ad86c07 | 2020-09-13 21:54:52 -0700 | [diff] [blame] | 356 | pch = ".", cex = 2, type = "l", polymin = NA, |
| 357 | minmax = list(pch = ".", cex = cex, col = "red"), |
Bill Evans | cebc971 | 2020-08-30 19:55:24 -0700 | [diff] [blame] | 358 | min = minmax, max = minmax, |
| 359 | dir = if (is_latex()) rmd_files_dir() else tempdir(), |
Hao Zhu | 30db027 | 2021-02-19 13:02:29 -0500 | [diff] [blame] | 360 | file = NULL, file_type = if (is_latex()) "pdf" else svglite::svglite, |
| 361 | ...) { |
Bill Evans | cebc971 | 2020-08-30 19:55:24 -0700 | [diff] [blame] | 362 | if (is.list(x)) { |
Bill Evans | 60fd80b | 2020-09-11 10:40:25 -0700 | [diff] [blame] | 363 | lenx <- length(x) |
| 364 | |
Bill Evans | cebc971 | 2020-08-30 19:55:24 -0700 | [diff] [blame] | 365 | if (same_lim) { |
| 366 | if (is.null(xlim)) { |
Bill Evans | ad86c07 | 2020-09-13 21:54:52 -0700 | [diff] [blame] | 367 | xlim <- base::range(unlist(x), na.rm = TRUE) |
Bill Evans | cebc971 | 2020-08-30 19:55:24 -0700 | [diff] [blame] | 368 | } |
| 369 | if (is.null(ylim) && !is.null(y)) { |
Bill Evans | ad86c07 | 2020-09-13 21:54:52 -0700 | [diff] [blame] | 370 | ylim <- base::range(c(unlist(y), polymin), na.rm = TRUE) |
Bill Evans | cebc971 | 2020-08-30 19:55:24 -0700 | [diff] [blame] | 371 | } |
| 372 | } |
Bill Evans | ad86c07 | 2020-09-13 21:54:52 -0700 | [diff] [blame] | 373 | |
Bill Evans | cebc971 | 2020-08-30 19:55:24 -0700 | [diff] [blame] | 374 | if (is.null(y)) { |
Bill Evans | ad86c07 | 2020-09-13 21:54:52 -0700 | [diff] [blame] | 375 | y <- list(y) |
| 376 | } else if (length(y) != lenx) { |
Bill Evans | cebc971 | 2020-08-30 19:55:24 -0700 | [diff] [blame] | 377 | stop("'x' and 'y' are not the same length") |
| 378 | } |
Bill Evans | 60fd80b | 2020-09-11 10:40:25 -0700 | [diff] [blame] | 379 | |
Bill Evans | b62414a | 2020-09-14 12:33:38 -0700 | [diff] [blame] | 380 | dots <- listify_args(x, y = y, width, height, res, |
| 381 | xlim, ylim, xaxt, yaxt, ann, col, border, frame.plot, |
| 382 | lwd, pch, cex, type, polymin, minmax, min, max, |
| 383 | dir, file, file_type, |
| 384 | lengths = c(1, lenx)) |
Bill Evans | 60fd80b | 2020-09-11 10:40:25 -0700 | [diff] [blame] | 385 | |
Bill Evans | 548d715 | 2020-09-13 21:44:24 -0700 | [diff] [blame] | 386 | return(do.call(Map, c(list(f = spec_plot), dots))) |
Bill Evans | 60fd80b | 2020-09-11 10:40:25 -0700 | [diff] [blame] | 387 | |
Bill Evans | cebc971 | 2020-08-30 19:55:24 -0700 | [diff] [blame] | 388 | } |
| 389 | |
Bill Evans | 8def4da | 2020-09-11 09:58:26 -0700 | [diff] [blame] | 390 | if (is.null(x)) return(NULL) |
| 391 | |
Bill Evans | cebc971 | 2020-08-30 19:55:24 -0700 | [diff] [blame] | 392 | if (is.null(y) || !length(y)) { |
| 393 | y <- x |
Bill Evans | a8ef1fb | 2020-09-12 22:55:46 -0700 | [diff] [blame] | 394 | x <- seq_along(y) |
| 395 | if (!is.null(xlim) && is.null(ylim)) { |
Bill Evans | ad86c07 | 2020-09-13 21:54:52 -0700 | [diff] [blame] | 396 | ylim <- range(c(xlim, polymin), na.rm = TRUE) |
Bill Evans | a8ef1fb | 2020-09-12 22:55:46 -0700 | [diff] [blame] | 397 | xlim <- range(x) |
| 398 | } |
Bill Evans | cebc971 | 2020-08-30 19:55:24 -0700 | [diff] [blame] | 399 | } |
| 400 | |
| 401 | if (is.null(xlim)) { |
Bill Evans | ad86c07 | 2020-09-13 21:54:52 -0700 | [diff] [blame] | 402 | xlim <- base::range(x, na.rm = TRUE) |
Bill Evans | cebc971 | 2020-08-30 19:55:24 -0700 | [diff] [blame] | 403 | } |
| 404 | |
| 405 | if (is.null(ylim) && !is.null(y)) { |
Bill Evans | a8ef1fb | 2020-09-12 22:55:46 -0700 | [diff] [blame] | 406 | ylim <- base::range(c(y, polymin), na.rm = TRUE) |
Bill Evans | cebc971 | 2020-08-30 19:55:24 -0700 | [diff] [blame] | 407 | } |
| 408 | |
| 409 | if (is.null(min)) min <- minmax |
| 410 | if (is.null(max)) max <- minmax |
| 411 | |
| 412 | expand <- c( |
Bill Evans | 60fd80b | 2020-09-11 10:40:25 -0700 | [diff] [blame] | 413 | if (!is.null(min) && length(min)) -0.04 else 0, |
| 414 | if (!is.null(max) && length(max)) +0.04 else 0) |
| 415 | xlim <- xlim + diff(xlim) * expand |
| 416 | ylim <- ylim + diff(ylim) * expand |
Bill Evans | cebc971 | 2020-08-30 19:55:24 -0700 | [diff] [blame] | 417 | |
Bill Evans | cebc971 | 2020-08-30 19:55:24 -0700 | [diff] [blame] | 418 | if (!dir.exists(dir)) { |
| 419 | dir.create(dir) |
| 420 | } |
| 421 | |
Bill Evans | b62414a | 2020-09-14 12:33:38 -0700 | [diff] [blame] | 422 | file_ext <- dev_chr(file_type) |
Bill Evans | cebc971 | 2020-08-30 19:55:24 -0700 | [diff] [blame] | 423 | if (is.null(file)) { |
Bill Evans | b62414a | 2020-09-14 12:33:38 -0700 | [diff] [blame] | 424 | file <- normalizePath( |
| 425 | tempfile(pattern = "plot_", tmpdir = dir, fileext = paste0(".", file_ext)), |
| 426 | winslash = "/", mustWork = FALSE) |
Bill Evans | cebc971 | 2020-08-30 19:55:24 -0700 | [diff] [blame] | 427 | } |
| 428 | |
Bill Evans | b62414a | 2020-09-14 12:33:38 -0700 | [diff] [blame] | 429 | graphics_dev(filename = file, dev = file_type, |
| 430 | width = width, height = height, res = res, |
| 431 | bg = "transparent") |
Bill Evans | cebc971 | 2020-08-30 19:55:24 -0700 | [diff] [blame] | 432 | curdev <- grDevices::dev.cur() |
| 433 | on.exit(grDevices::dev.off(curdev), add = TRUE) |
| 434 | |
Bill Evans | a8ef1fb | 2020-09-12 22:55:46 -0700 | [diff] [blame] | 435 | graphics::par(mar = c(0, 0, 0, 0), lwd = lwd) |
| 436 | |
| 437 | dots <- list(...) |
| 438 | if (!is.na(polymin) && "angle" %in% names(dots)) { |
| 439 | angle <- dots$angle |
| 440 | dots$angle <- NULL |
| 441 | } else angle <- 45 |
| 442 | |
| 443 | do.call(graphics::plot, |
| 444 | c(list(x, y, type = if (is.na(polymin)) type else "n", |
| 445 | xlim = xlim, ylim = ylim, |
Bill Evans | cebc971 | 2020-08-30 19:55:24 -0700 | [diff] [blame] | 446 | xaxt = xaxt, yaxt = yaxt, ann = ann, col = col, |
Bill Evans | ad86c07 | 2020-09-13 21:54:52 -0700 | [diff] [blame] | 447 | frame.plot = frame.plot, cex = cex, pch = pch), |
| 448 | dots)) |
Bill Evans | a8ef1fb | 2020-09-12 22:55:46 -0700 | [diff] [blame] | 449 | |
| 450 | if (!is.na(polymin)) { |
| 451 | lty <- if ("lty" %in% names(dots)) dots$lty else graphics::par("lty") |
Hao Zhu | f6b60e8 | 2020-10-21 18:58:19 -0400 | [diff] [blame] | 452 | 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] | 453 | border = NA, col = col, angle = angle, lty = lty, |
| 454 | xpd = if ("xpd" %in% names(dots)) dots$xpd else NA) |
Bill Evans | a8ef1fb | 2020-09-12 22:55:46 -0700 | [diff] [blame] | 455 | } |
Bill Evans | cebc971 | 2020-08-30 19:55:24 -0700 | [diff] [blame] | 456 | |
| 457 | if (!is.null(min) && length(min)) { |
Bill Evans | ad86c07 | 2020-09-13 21:54:52 -0700 | [diff] [blame] | 458 | if (!"xpd" %in% names(min)) min$xpd <- NA |
Bill Evans | cebc971 | 2020-08-30 19:55:24 -0700 | [diff] [blame] | 459 | ind <- which.min(y) |
Bill Evans | ad86c07 | 2020-09-13 21:54:52 -0700 | [diff] [blame] | 460 | do.call(graphics::points, c(list(x[ind], y[ind]), min)) |
Bill Evans | cebc971 | 2020-08-30 19:55:24 -0700 | [diff] [blame] | 461 | } |
| 462 | |
| 463 | if (!is.null(max) && length(max)) { |
Bill Evans | ad86c07 | 2020-09-13 21:54:52 -0700 | [diff] [blame] | 464 | if (!"xpd" %in% names(max)) max$xpd <- NA |
Bill Evans | cebc971 | 2020-08-30 19:55:24 -0700 | [diff] [blame] | 465 | ind <- which.max(y) |
Bill Evans | ad86c07 | 2020-09-13 21:54:52 -0700 | [diff] [blame] | 466 | do.call(graphics::points, c(list(x[ind], y[ind]), max)) |
Bill Evans | cebc971 | 2020-08-30 19:55:24 -0700 | [diff] [blame] | 467 | } |
| 468 | |
| 469 | grDevices::dev.off(curdev) |
| 470 | |
Bill Evans | b62414a | 2020-09-14 12:33:38 -0700 | [diff] [blame] | 471 | out <- make_inline_plot( |
| 472 | file, file_ext, file_type, |
| 473 | width, height, res, |
| 474 | del = TRUE) |
Bill Evans | cebc971 | 2020-08-30 19:55:24 -0700 | [diff] [blame] | 475 | return(out) |
| 476 | } |
Hao Zhu | f6b60e8 | 2020-10-21 18:58:19 -0400 | [diff] [blame] | 477 | |
| 478 | |
| 479 | #' Helper functions to generate inline sparklines |
| 480 | #' |
| 481 | #' @description These functions helps you quickly generate sets of sparkline |
| 482 | #' style plots using base R plotting system. Currently, we support histogram, |
| 483 | #' boxplot, line, scatter and pointrange plots. You can use them together with |
| 484 | #' `column_spec` to generate inline plot in tables. By default, this function |
| 485 | #' will save images in a folder called "kableExtra" and return the address of |
| 486 | #' the file. |
| 487 | #' |
| 488 | #' @param x,xmin,xmax A scalar value or List of scalar values for dot, left |
| 489 | #' and right errorbar. |
| 490 | #' @param vline A scalar value for where to draw a vertical line. |
| 491 | #' @param width The width of the plot in pixel |
| 492 | #' @param height The height of the plot in pixel |
| 493 | #' @param res The resolution of the plot. Default is 300. |
| 494 | #' @param same_lim T/F. If x is a list of vectors, should all the plots be |
| 495 | #' plotted in the same range? Default is True. |
| 496 | #' @param lim Manually specify plotting range in the form of |
| 497 | #' `c(0, 10)`. |
| 498 | #' @param xaxt On/Off for xaxis text |
| 499 | #' @param yaxt On/Off for yaxis text |
| 500 | #' @param ann On/Off for annotations (titles and axis titles) |
Hao Zhu | 7898891 | 2021-02-23 11:00:56 -0500 | [diff] [blame] | 501 | #' @param col Color for mean dot. |
| 502 | #' @param line_col Color for the line and the error bar. |
Hao Zhu | f6b60e8 | 2020-10-21 18:58:19 -0400 | [diff] [blame] | 503 | #' @param cex size of the mean dot and error bar size. |
| 504 | #' @param frame.plot T/F for whether to plot the plot frames. |
| 505 | #' @param dir Directory of where the images will be saved. |
| 506 | #' @param file File name. If not provided, a random name will be used |
| 507 | #' @param file_type Graphic device. Can be character (e.g., `"pdf"`) |
| 508 | #' or a graphics device function (`grDevices::pdf`). This defaults |
| 509 | #' to `"pdf"` if the rendering is in LaTeX and `"svg"` otherwise. |
| 510 | #' for HTML output |
| 511 | #' @param ... extra parameters sending to `hist()` |
| 512 | #' |
| 513 | #' @export |
| 514 | spec_pointrange <- function( |
| 515 | x, xmin, xmax, vline = NULL, |
| 516 | width = 200, height = 50, res = 300, |
| 517 | same_lim = TRUE, lim = NULL, |
| 518 | xaxt = 'n', yaxt = 'n', ann = FALSE, |
Hao Zhu | 7898891 | 2021-02-23 11:00:56 -0500 | [diff] [blame] | 519 | col = "red", line_col = "black", cex = 0.3, frame.plot = FALSE, |
Hao Zhu | f6b60e8 | 2020-10-21 18:58:19 -0400 | [diff] [blame] | 520 | dir = if (is_latex()) rmd_files_dir() else tempdir(), |
| 521 | file = NULL, |
Hao Zhu | 30db027 | 2021-02-19 13:02:29 -0500 | [diff] [blame] | 522 | file_type = if (is_latex()) "pdf" else svglite::svglite, ...) { |
Hao Zhu | f6b60e8 | 2020-10-21 18:58:19 -0400 | [diff] [blame] | 523 | if (length(x) > 1) { |
| 524 | if (same_lim & is.null(lim)) { |
| 525 | all_range <- c(unlist(xmin), unlist(xmax)) |
| 526 | lim <- base::range(all_range, na.rm=TRUE) |
| 527 | lim <- lim + c(-0.04 * diff(lim), 0.04 * diff(lim)) |
| 528 | } |
| 529 | |
| 530 | dots <- listify_args( |
| 531 | x = as.list(x), xmin = as.list(xmin), xmax = as.list(xmax), vline, |
| 532 | width, height, res, |
Hao Zhu | 7898891 | 2021-02-23 11:00:56 -0500 | [diff] [blame] | 533 | lim, xaxt, yaxt, ann, col, line_col, cex, frame.plot, |
Hao Zhu | f6b60e8 | 2020-10-21 18:58:19 -0400 | [diff] [blame] | 534 | dir, file, file_type, |
| 535 | lengths = c(1, length(x)), |
| 536 | passthru = c("x", "xmin", "xmax")) |
| 537 | return(do.call(Map, c(list(f = spec_pointrange), dots))) |
| 538 | } |
| 539 | |
| 540 | if (is.null(x)) return(NULL) |
| 541 | |
| 542 | if (is.null(lim)) { |
| 543 | one_range <- unlist(c(xmin, xmax)) |
| 544 | lim <- base::range(one_range, na.rm=TRUE) |
| 545 | lim <- lim + c(-0.04 * diff(lim), 0.04 * diff(lim)) |
| 546 | } |
| 547 | |
| 548 | if (!dir.exists(dir)) { |
| 549 | dir.create(dir) |
| 550 | } |
| 551 | |
| 552 | file_ext <- dev_chr(file_type) |
| 553 | if (is.null(file)) { |
| 554 | file <- normalizePath( |
| 555 | tempfile(pattern = "pointrange_", tmpdir = dir, fileext = paste0(".", file_ext)), |
| 556 | winslash = "/", mustWork = FALSE) |
| 557 | } |
| 558 | |
| 559 | graphics_dev(filename = file, dev = file_type, |
| 560 | width = width, height = height, res = res, |
| 561 | bg = "transparent") |
| 562 | curdev <- grDevices::dev.cur() |
| 563 | on.exit(grDevices::dev.off(curdev), add = TRUE) |
| 564 | |
| 565 | graphics::par(mar = c(0, 0, 0.2, 0), lwd=1, |
| 566 | ann = ann, xaxt = xaxt, yaxt = yaxt) |
| 567 | |
| 568 | graphics::plot(x, 0, type = "p", pch = ".", |
| 569 | xlim = lim, frame.plot = frame.plot) |
Hao Zhu | 7898891 | 2021-02-23 11:00:56 -0500 | [diff] [blame] | 570 | graphics::arrows(xmin, 0, xmax, 0, cex / 15, angle = 90, code = 3, |
| 571 | col = line_col) |
Hao Zhu | f6b60e8 | 2020-10-21 18:58:19 -0400 | [diff] [blame] | 572 | graphics::points(x, 0, col = col, type = "p", pch = 15, cex = cex) |
| 573 | if (!is.null(vline)) { |
| 574 | graphics::abline(v = vline, lty = 3) |
| 575 | } |
| 576 | |
| 577 | grDevices::dev.off(curdev) |
| 578 | |
| 579 | out <- make_inline_plot( |
| 580 | file, file_ext, file_type, |
| 581 | width, height, res, |
| 582 | del = TRUE) |
| 583 | return(out) |
| 584 | } |