From 2acb80f5e783fc07eea5d0c19f33c559e8700837 Mon Sep 17 00:00:00 2001 From: bjmt Date: Sun, 9 Sep 2018 19:14:40 -0400 Subject: [PATCH] v0.0.3 --- DESCRIPTION | 2 +- NEWS | 21 ++++++++- R/console_plot.R | 64 ++++++++++----------------- R/console_plot_axis.R | 99 +++++++++++------------------------------- R/console_plot_types.R | 86 ++++++++++++++---------------------- 5 files changed, 103 insertions(+), 169 deletions(-) diff --git a/DESCRIPTION b/DESCRIPTION index 0561d11..357518a 100644 --- a/DESCRIPTION +++ b/DESCRIPTION @@ -1,6 +1,6 @@ Package: console.plot Title: Plots for the R console -Version: 0.0.2 +Version: 0.0.3 Authors@R: person(given = "Benjamin", family = "Tremblay", diff --git a/NEWS b/NEWS index 7b8d34c..3f21fa6 100644 --- a/NEWS +++ b/NEWS @@ -1,9 +1,26 @@ +# console.plot 0.0.3 # + +## User-facing ## + +- new option: `options(ascii = FALSE)` + +## Internal ## + +- fixed bug where `substr` reported `character(0)` +- code re-organisation: no longer have to write code for both ASCII and + non-ASCII symbols separately + # console.plot 0.0.2 # -- fixed bug where abline would appear twice +## User-facing ## + - finished adding ASCII-only plots -- improved appearance of abline with non-ASCII characters - added `horizontal` param (does nothing as of yet) +- improved appearance of abline with non-ASCII characters + +## Internal ## + +- fixed bug where abline would appear twice - fixed data not respecting user-set xlim, ylim # console.plot 0.0.1 # diff --git a/R/console_plot.R b/R/console_plot.R index 7030695..555d49c 100644 --- a/R/console_plot.R +++ b/R/console_plot.R @@ -37,32 +37,20 @@ console.plot <- function(x, y = NULL, groups = NULL, main = NULL, file = "", # types: p=point, l=line, b=line+point, h=point with vertical downward line, # s=staircase, S=inverase staircase + s <- get_symbols(ascii) + all.symbols <- s$all.symbols + all.lines <- s$all.lines + if (!type %in% c("p", "l", "b", "h", "s", "S")) warning("unknown 'type'") if (is.null(plot.width)) plot.width <- as.integer(options("width")$width / 1.35) if (is.null(plot.height)) plot.height <- as.integer(plot.width / 2) - all.symbols <- c() - if (!ascii) { - all.symbols <- c(8226, 215, 43, 8718, 9670, 9650, 9744, 9671, 9651, 9737, - 9733, 9734, 10035, 9746, 8865, 8857, 8853, 10023) - all.symbols <- sapply(all.symbols, intToUtf8) - } - all.symbols <- c(all.symbols, "o", "@", "#", "$", "%", "&", ">", "<", "?", - letters[-15]) - - all.lines <- c() - if (!ascii) { - all.lines <- c(9475, 9479, 9483, 9551, 9553, 9474, 9478, 9482, 9550) - all.lines <- sapply(all.lines, intToUtf8) - } - all.lines <- c(all.lines, "|", "-", "/", "\\", ".", ",", "~") - if (is.null(xlab)) xlab <- deparse(substitute(x)) if (is.null(ylab)) ylab <- deparse(substitute(y)) - if (plot.height < 12) stop("'plot.height' must be greater than 11") - if (plot.width < 30) stop("'plot.width' must be greater than 29") + if (plot.height < 5) stop("'plot.height' must be at least 5") + if (plot.width < 20) stop("'plot.width' must be at least 20") groups.original <- groups if (is.null(groups)) groups <- rep(1, length(x)) else { @@ -82,7 +70,7 @@ console.plot <- function(x, y = NULL, groups = NULL, main = NULL, file = "", } } ylab <- xlab - xlab <- "Index" + if (is.null(xlab)) xlab <- "Index" } else if (length(y) != length(x)) stop("x and y must have the same number of observations") @@ -166,40 +154,36 @@ console.plot <- function(x, y = NULL, groups = NULL, main = NULL, file = "", # generate plot plot.lines <- console.plot.types(x, y, groups, plot.width, plot.height, point, - type, line, abline.x, abline.y, ascii, - abline.overlay) + type, line, abline.x, abline.y, + abline.overlay, s) # add axis lines plot.lines <- console.plot.axis(plot.lines, plot.width, plot.height, - ylim, xlim, ascii) + ylim, xlim, s) # fix abline - if (!is.null(abline.x) && !ascii) { - substr(plot.lines[1], abline.x + 14, abline.x + 14) <- intToUtf8(0x252C) + if (!is.null(abline.x)) { + substr(plot.lines[1], abline.x + 14, abline.x + 14) <- s$hori.down substr(plot.lines[length(plot.lines) - 2], abline.x + 14, - abline.x + 14) <- intToUtf8(0x2534) + abline.x + 14) <- s$hori.up if (substr(plot.lines[length(plot.lines) - 1], abline.x + 14, - abline.x + 14) == intToUtf8(0x2575)) { + abline.x + 14) == s$tick.x.minor) { substr(plot.lines[length(plot.lines) - 2], abline.x + 14, - abline.x + 14) <- intToUtf8(0x253C) + abline.x + 14) <- s$cross } } + if (!is.null(abline.y)) { - if (!ascii) { - substr(plot.lines[abline.y + 1], 14, 14) <- intToUtf8(0x2500) - substr(plot.lines[abline.y + 1], 13, 13) <- intToUtf8(0x251C) - substr(plot.lines[abline.y + 1], plot.width + 15, - plot.width + 15) <- intToUtf8(0x2500) - substr(plot.lines[abline.y + 1], plot.width + 16, - plot.width + 16) <- intToUtf8(0x2524) - if (substr(plot.lines[abline.y + 1], 12, 12) == intToUtf8(0x2500)) { - substr(plot.lines[abline.y + 1], 13, 13) <- intToUtf8(0x253C) - } - } else { - substr(plot.lines[abline.y + 1], 14, 14) <- "-" - substr(plot.lines[abline.y + 1], plot.width + 15, plot.width + 15) <- "-" + substr(plot.lines[abline.y + 1], 14, 14) <- s$hori + substr(plot.lines[abline.y + 1], 13, 13) <- s$vert.right + substr(plot.lines[abline.y + 1], plot.width + 15, + plot.width + 15) <- s$hori + substr(plot.lines[abline.y + 1], plot.width + 16, + plot.width + 16) <- s$vert.left + if (substr(plot.lines[abline.y + 1], 12, 12) == s$hori) { + substr(plot.lines[abline.y + 1], 13, 13) <- s$cross } } diff --git a/R/console_plot_axis.R b/R/console_plot_axis.R index 38f1070..b5edbda 100644 --- a/R/console_plot_axis.R +++ b/R/console_plot_axis.R @@ -1,101 +1,54 @@ console.plot.axis <- function(plot.lines, plot.width, plot.height, ylim, xlim, - ASCII) { + s) { - if (ASCII) plot.lines <- paste0(plot.lines, " ", "|") - if (!ASCII) plot.lines <- paste0(plot.lines, " ", intToUtf8(0x2502)) + plot.lines <- paste0(plot.lines, " ", s$vert) - if (!ASCII) plot.lines[1] <- paste0(" ", intToUtf8(0x2500), intToUtf8(0x2524), - " ", plot.lines[1]) - if (ASCII) plot.lines[1] <- paste0(" ", "-", "|", - " ", plot.lines[1]) + plot.lines[1] <- paste0(" ", s$hori, s$vert.left, " ", plot.lines[1]) - if (!ASCII) plot.lines[length(plot.lines)] <- paste0(" ", intToUtf8(0x2500), - intToUtf8(0x2524), - " ", - plot.lines[length(plot.lines)]) - if (ASCII) plot.lines[length(plot.lines)] <- paste0(" ", "-", - "|", - " ", - plot.lines[length(plot.lines)]) + plot.lines[length(plot.lines)] <- paste0(" ", s$hori, s$vert.left, " ", + plot.lines[length(plot.lines)]) mid.y <- round(plot.height / 2) - if (!ASCII) plot.lines[mid.y] <- paste0(" ", intToUtf8(0x2500), - intToUtf8(0x2524), " ", - plot.lines[mid.y]) - if (ASCII) plot.lines[mid.y] <- paste0(" ", "-", - "|", " ", - plot.lines[mid.y]) + plot.lines[mid.y] <- paste0(" ", s$hori, s$vert, " ", plot.lines[mid.y]) mid.y1 <- round(plot.height * 0.25) mid.y3 <- round(plot.height * 0.75) - if (!ASCII) plot.lines[mid.y1] <- paste0(" ", intToUtf8(0x2524), " ", - plot.lines[mid.y1]) - if (ASCII) plot.lines[mid.y1] <- paste0(" ", "|", " ", - plot.lines[mid.y1]) + plot.lines[mid.y1] <- paste0(" ", s$vert.left, " ", plot.lines[mid.y1]) - if (!ASCII) plot.lines[mid.y3] <- paste0(" ", intToUtf8(0x2524), " ", - plot.lines[mid.y3]) - if (ASCII) plot.lines[mid.y3] <- paste0(" ", "|", " ", - plot.lines[mid.y3]) + plot.lines[mid.y3] <- paste0(" ", s$vert.left, " ", plot.lines[mid.y3]) yaxis.skip <- c(1, length(plot.lines), mid.y, mid.y1, mid.y3) - if (!ASCII) plot.lines[-yaxis.skip] <- paste0(paste0(" ", - intToUtf8(0x2502), " "), - plot.lines[-yaxis.skip]) - if (ASCII) plot.lines[-yaxis.skip] <- paste0(paste0(" ", - "|", " "), - plot.lines[-yaxis.skip]) + plot.lines[-yaxis.skip] <- paste0(paste0(" ", s$vert, " "), + plot.lines[-yaxis.skip]) mid.x <- round(plot.width / 2) mid.x1 <- round(plot.width * 0.25) mid.x3 <- round(plot.width * 0.75) - if (!ASCII) xaxis <- paste0(paste0(" ", intToUtf8(0x2514), intToUtf8(0x2500), - intToUtf8(0x252c)), - paste(rep(intToUtf8(0x2500), mid.x1 - 1), collapse = ""), - intToUtf8(0x252c), - paste(rep(intToUtf8(0x2500), mid.x - mid.x1 - 1), collapse = ""), - intToUtf8(0x252c), - paste(rep(intToUtf8(0x2500), mid.x3 - mid.x - 1), collapse = ""), - intToUtf8(0x252c), - paste(rep(intToUtf8(0x2500), plot.width - mid.x3 - 2), collapse = ""), - paste0(intToUtf8(0x252c), intToUtf8(0x2500), intToUtf8(0x2518))) - if (ASCII) xaxis <- paste0(paste0(" ", "+", "-", - "-"), - paste(rep("-", mid.x1 - 1), collapse = ""), - "-", - paste(rep("-", mid.x - mid.x1 - 1), collapse = ""), - "-", - paste(rep("-", mid.x3 - mid.x - 1), collapse = ""), - "-", - paste(rep("-", plot.width - mid.x3 - 2), collapse = ""), - paste0("-", "-", "+")) - - if (!ASCII) xaxis2 <- paste0(" ", intToUtf8(0x2575), + xaxis <- paste0(paste0(" ", s$corner.bot.left, s$hori, s$hori.down), + paste(rep(s$hori, mid.x1 - 1), collapse = ""), + s$hori.down, + paste(rep(s$hori, mid.x - mid.x1 - 1), collapse = ""), + s$hori.down, + paste(rep(s$hori, mid.x3 - mid.x - 1), collapse = ""), + s$hori.down, + paste(rep(s$hori, plot.width - mid.x3 - 2), collapse = ""), + paste0(s$hori.down, s$hori, s$corner.bot.right)) + + xaxis2 <- paste0(" ", s$tick.x.minor, paste(rep(" ", mid.x1 - 1), collapse = ""), " ", - paste(rep(" ", mid.x - mid.x1 - 1), collapse = ""), - intToUtf8(0x2575), + paste(rep(" ", mid.x - mid.x1 - 1), collapse = ""), + s$tick.x.minor, paste(rep(" ", mid.x3 - mid.x - 1), collapse = ""), " ", paste(rep(" ", plot.width - mid.x3 - 2), collapse = ""), - intToUtf8(0x2575)) - if (ASCII) xaxis2 <- paste0(" ", "|", - paste(rep(" ", mid.x1 - 1), collapse = ""), " ", - paste(rep(" ", mid.x - mid.x1 - 1), collapse = ""), - "|", - paste(rep(" ", mid.x3 - mid.x - 1), collapse = ""), " ", - paste(rep(" ", plot.width - mid.x3 - 2), collapse = ""), - "|") + s$tick.x.minor) plot.lines <- c(plot.lines, xaxis, xaxis2) - if (!ASCII) plot.top <- paste0(" ", intToUtf8(0x250c), - paste(rep(intToUtf8(0x2500), plot.width + 2), collapse = ""), - intToUtf8(0x2510)) - if (ASCII) plot.top <- paste0(" ", "+", - paste(rep("-", plot.width + 2), collapse = ""), - "+") + plot.top <- paste0(" ", s$corner.top.left, paste(rep(s$hori, plot.width + 2), + collapse = ""), s$corner.top.right) plot.lines <- c(plot.top, plot.lines) # add axis scales diff --git a/R/console_plot_types.R b/R/console_plot_types.R index fffd036..baa425b 100644 --- a/R/console_plot_types.R +++ b/R/console_plot_types.R @@ -1,6 +1,6 @@ console.plot.types <- function(x, y, groups, plot.width, plot.height, point, - type, line, abline.x, abline.y, ASCII, - abline.overlay) { + type, line, abline.x, abline.y, + abline.overlay, s) { plot.lines <- paste(rep(" ", plot.width), collapse = "") plot.lines <- rep(plot.lines, plot.height) @@ -23,9 +23,9 @@ console.plot.types <- function(x, y, groups, plot.width, plot.height, point, if (length(substr(plot.lines[j], h.index.i[1], h.index.i[1])) > 0) { if (substr(plot.lines[j], h.index.i[1], h.index.i[1]) == " ") { - if (ASCII) substr(plot.lines[j], h.index.i[1], h.index.i[1]) <- "|" - if (!ASCII) substr(plot.lines[j], h.index.i[1], - h.index.i[1]) <- intToUtf8(0x2502) + + substr(plot.lines[j], h.index.i[1], h.index.i[1]) <- s$vert + } } @@ -51,62 +51,47 @@ console.plot.types <- function(x, y, groups, plot.width, plot.height, point, if (type == "s") { if (abs(x.i.1 - x.i.2) > 0) { - if (ASCII) { - substr(plot.lines[y.i.1], x.i.1, - x.i.2) <- paste(rep("-", abs(x.i.1 - x.i.2)), - collapse = "") - } - if (!ASCII) { - substr(plot.lines[y.i.1], x.i.1, - x.i.2) <- paste(rep(intToUtf8(0x2500), abs(x.i.1 - x.i.2)), - collapse = "") - if (x.i.1 != 1 && y.i.1 > y.i.2) substr(plot.lines[y.i.1], x.i.1, - x.i.1) <- intToUtf8(0x250c) - if (x.i.1 != 1 && y.i.1 < y.i.2) substr(plot.lines[y.i.1], x.i.1, - x.i.1) <- intToUtf8(0x2514) - } + substr(plot.lines[y.i.1], x.i.1, + x.i.2) <- paste(rep(s$hori, abs(x.i.1 - x.i.2)), + collapse = "") + if (x.i.1 != 1 && y.i.1 > y.i.2) substr(plot.lines[y.i.1], x.i.1, + x.i.1) <- s$corner.top.left + if (x.i.1 != 1 && y.i.1 < y.i.2) substr(plot.lines[y.i.1], x.i.1, + x.i.1) <- s$corner.bot.left } if (abs(y.i.1 - y.i.2) > 0) { - if (!ASCII && y.i.1 > y.i.2) substr(plot.lines[y.i.1], x.i.2, - x.i.2) <- intToUtf8(0x2518) - else if (!ASCII) substr(plot.lines[y.i.1], x.i.2, - x.i.2) <- intToUtf8(0x2510) + if (y.i.1 > y.i.2) substr(plot.lines[y.i.1], x.i.2, + x.i.2) <- s$corner.bot.right + else substr(plot.lines[y.i.1], x.i.2, + x.i.2) <- s$corner.top.right for (m in seq(y.i.1, y.i.2)[-c(1, y.i.1 + y.i.2)]) { - if (ASCII) substr(plot.lines[m], x.i.2, x.i.2) <- "|" - if (!ASCII) substr(plot.lines[m], x.i.2, x.i.2) <- intToUtf8(0x2502) + substr(plot.lines[m], x.i.2, x.i.2) <- s$vert } } } else if (type == "S") { if (abs(x.i.1 - x.i.2) > 0) { - if (ASCII) substr(plot.lines[y.i.2], x.i.1, - x.i.2) <- paste(rep("-", abs(x.i.1 - x.i.2)), - collapse = "") - if (!ASCII) substr(plot.lines[y.i.2], x.i.1, - x.i.2) <- paste(rep(intToUtf8(0x2500), abs(x.i.1 - x.i.2)), - collapse = "") + substr(plot.lines[y.i.2], x.i.1, x.i.2) <- paste(rep(s$hori, abs(x.i.1 - x.i.2)), + collapse = "") } if (abs(y.i.1 - y.i.2) > 0) { for (m in seq(y.i.1, y.i.2)[-c(1, y.i.1 + y.i.2)]) { - if (ASCII) substr(plot.lines[m], x.i.1, x.i.1) <- "|" - if (!ASCII) substr(plot.lines[m], x.i.1, x.i.1) <- intToUtf8(0x2502) + substr(plot.lines[m], x.i.1, x.i.1) <- s$vert } } - if (!ASCII) { - if (x.i.1 == 1 && y.i.1 < y.i.2) substr(plot.lines[y.i.2], x.i.1, - x.i.1) <- intToUtf8(0x2514) - if (x.i.1 == 1 && y.i.1 > y.i.2) substr(plot.lines[y.i.2], x.i.1, - x.i.1) <- intToUtf8(0x250c) - if (x.i.1 != 1 && y.i.1 > y.i.2) substr(plot.lines[y.i.2], x.i.1, - x.i.1) <- intToUtf8(0x250c) - if (x.i.1 != 1 && y.i.1 < y.i.2) substr(plot.lines[y.i.2], x.i.1, - x.i.1) <- intToUtf8(0x2514) - } + if (x.i.1 == 1 && y.i.1 < y.i.2) substr(plot.lines[y.i.2], x.i.1, + x.i.1) <- s$corner.bot.left + if (x.i.1 == 1 && y.i.1 > y.i.2) substr(plot.lines[y.i.2], x.i.1, + x.i.1) <- s$corner.top.left + if (x.i.1 != 1 && y.i.1 > y.i.2) substr(plot.lines[y.i.2], x.i.1, + x.i.1) <- s$corner.top.left + if (x.i.1 != 1 && y.i.1 < y.i.2) substr(plot.lines[y.i.2], x.i.1, + x.i.1) <- s$corner.bot.left } @@ -188,14 +173,11 @@ console.plot.types <- function(x, y, groups, plot.width, plot.height, point, if (!is.null(abline.y)) { if (abline.overlay) { - if (ASCII) plot.lines[abline.y] <- paste(rep("-", plot.width), collapse = "") - if (!ASCII) plot.lines[abline.y] <- paste(rep(intToUtf8(0x2500), plot.width), - collapse = "") + plot.lines[abline.y] <- paste(rep(s$hori, plot.width), collapse = "") } else { for (i in seq_len(plot.width)) { if (substr(plot.lines[abline.y], i, i) == " ") { - if (ASCII) substr(plot.lines[abline.y], i, i) <- "-" - if (!ASCII) substr(plot.lines[abline.y], i, i) <- intToUtf8(0x2500) + substr(plot.lines[abline.y], i, i) <- s$hori } } } @@ -204,14 +186,12 @@ console.plot.types <- function(x, y, groups, plot.width, plot.height, point, if (!is.null(abline.x)) { for (i in seq_len(plot.height)) { if (!abline.overlay) { - if (!ASCII) { - if (substr(plot.lines[i], abline.x, abline.x) == intToUtf8(0x2500)) - substr(plot.lines[i], abline.x, abline.x) <- intToUtf8(0x253C) + if (substr(plot.lines[i], abline.x, abline.x) == s$hori) { + substr(plot.lines[i], abline.x, abline.x) <- s$cross } if (substr(plot.lines[i], abline.x, abline.x) != " ") next } - if (ASCII) substr(plot.lines[i], abline.x, abline.x) <- "|" - if (!ASCII) substr(plot.lines[i], abline.x, abline.x) <- intToUtf8(0x2502) + substr(plot.lines[i], abline.x, abline.x) <- s$vert } }