diff --git a/DESCRIPTION b/DESCRIPTION index 357518a..7a550e1 100644 --- a/DESCRIPTION +++ b/DESCRIPTION @@ -1,6 +1,6 @@ Package: console.plot Title: Plots for the R console -Version: 0.0.3 +Version: 0.0.4 Authors@R: person(given = "Benjamin", family = "Tremblay", diff --git a/NAMESPACE b/NAMESPACE index 74d74f5..52d1199 100644 --- a/NAMESPACE +++ b/NAMESPACE @@ -1,3 +1,4 @@ # Generated by roxygen2: do not edit by hand export(console.plot) +importFrom(stats,approx) diff --git a/NEWS b/NEWS index 3f21fa6..1c874b7 100644 --- a/NEWS +++ b/NEWS @@ -1,3 +1,16 @@ +# console.plot 0.0.4 # + +## User-facing ## + +- plots with `plot.height = 5` now display correctly +- xaxis tick labels now better align with ticks + +## Internal ## + +- got rid of hex codes +- added stats import +- fixed approx errors from duplicate x values + # console.plot 0.0.3 # ## User-facing ## diff --git a/R/console_plot_axis.R b/R/console_plot_axis.R index b5edbda..8968dcb 100644 --- a/R/console_plot_axis.R +++ b/R/console_plot_axis.R @@ -5,14 +5,15 @@ console.plot.axis <- function(plot.lines, plot.width, plot.height, ylim, xlim, plot.lines[1] <- paste0(" ", s$hori, s$vert.left, " ", plot.lines[1]) - plot.lines[length(plot.lines)] <- paste0(" ", s$hori, s$vert.left, " ", + plot.lines[length(plot.lines)] <- paste0(" ", s$hori, s$vert.left, " ", plot.lines[length(plot.lines)]) - mid.y <- round(plot.height / 2) + mid.y <- round(plot.height / 2 + 0.01) - plot.lines[mid.y] <- paste0(" ", s$hori, s$vert, " ", plot.lines[mid.y]) + plot.lines[mid.y] <- paste0(" ", s$hori, s$vert.left, " ", plot.lines[mid.y]) mid.y1 <- round(plot.height * 0.25) + if (mid.y1 == 1) mid.y1 <- 2 mid.y3 <- round(plot.height * 0.75) plot.lines[mid.y1] <- paste0(" ", s$vert.left, " ", plot.lines[mid.y1]) @@ -24,7 +25,7 @@ console.plot.axis <- function(plot.lines, plot.width, plot.height, ylim, xlim, plot.lines[-yaxis.skip] <- paste0(paste0(" ", s$vert, " "), plot.lines[-yaxis.skip]) - mid.x <- round(plot.width / 2) + mid.x <- round(plot.width / 2 + 0.01) mid.x1 <- round(plot.width * 0.25) mid.x3 <- round(plot.width * 0.75) @@ -53,9 +54,9 @@ console.plot.axis <- function(plot.lines, plot.width, plot.height, ylim, xlim, # add axis scales - xaxis.scale <- c(gsub(" ", "", formatC(xlim[1], digits = 4, width = 5)), - gsub(" ", "", formatC(mean(xlim), digits = 4, width = 5)), - gsub(" ", "", formatC(xlim[2], digits = 4, width = 5))) + xaxis.scale <- c(format(as.character(xlim[1]), digits = 4, width = 5, justify = "centre"), + format(as.character(mean(xlim)), digits = 4, width = 5, justify = "centre"), + format(as.character(xlim[2]), digits = 4, width = 5, justify = "centre")) yaxis.scale <- c(gsub(" ", "", formatC(ylim[1], digits = 4, width = 5)), gsub(" ", "", formatC(mean(ylim), digits = 4, width = 5)), gsub(" ", "", formatC(ylim[2], digits = 4, width = 5))) @@ -63,11 +64,10 @@ console.plot.axis <- function(plot.lines, plot.width, plot.height, ylim, xlim, y.offset <- 8 y.offset.scale <- y.offset - nchar(yaxis.scale) - xaxis.scale.filler <- c(" ", - paste(rep(" ", mid.x - sum(nchar(xaxis.scale[1:2])) + - round(nchar(xaxis.scale[2]) / 2)), + xaxis.scale.filler <- c(" ", + paste(rep(" ", mid.x - 5), collapse = ""), - paste(rep(" ", mid.x - nchar(xaxis.scale[3])), + paste(rep(" ", mid.x - 6), collapse = "")) xaxis.scale <- paste0(xaxis.scale.filler[1], xaxis.scale[1], xaxis.scale.filler[2], xaxis.scale[2], diff --git a/R/console_plot_types.R b/R/console_plot_types.R index baa425b..9a51670 100644 --- a/R/console_plot_types.R +++ b/R/console_plot_types.R @@ -118,6 +118,7 @@ console.plot.types <- function(x, y, groups, plot.width, plot.height, point, y.i.1 <- y.i[j] y.i.2 <- y.i[j + 1] + if (x.i.1 == x.i.2) x.i.2 <- x.i.2 + 0.1 # approx won't work otherwise x.y.new <- approx(c(x.i.1, x.i.2), c(y.i.1, y.i.2), n = plot.width * plot.height) x.y.new <- unique(data.frame(x.y.new[[1]], x.y.new[[2]])) diff --git a/R/utils.R b/R/utils.R new file mode 100644 index 0000000..2fe9be0 --- /dev/null +++ b/R/utils.R @@ -0,0 +1,57 @@ +get_symbols <- function(ascii = FALSE) { + + 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(9475, 9479, 9483, 9551, 9553, 9474, 9478, 9482, 9550) + all.lines <- sapply(all.lines, intToUtf8) + all.lines <- c(all.lines, "|", "-", "/", "\\", ".", ",", "~") + + vert <- intToUtf8(9474) + vert.left <- intToUtf8(9508) + vert.right <- intToUtf8(9500) + hori <- intToUtf8(9472) + hori.down <- intToUtf8(9516) + hori.up <- intToUtf8(9524) + corner.bot.left <- intToUtf8(9492) + corner.bot.right <- intToUtf8(9496) + corner.top.left <- intToUtf8(9484) + corner.top.right <- intToUtf8(9488) + tick.x.minor <- intToUtf8(9589) + cross <- intToUtf8(9532) + + } else { + + all.symbols <- c("o", "@", "#", "$", "%", "&", ">", "<", "?", + letters[-15]) + + all.lines <- c("|", "-", "/", "\\", ".", ",", "~") + + vert <- "|" + vert.left <- "|" + vert.right <- "|" + hori <- "-" + hori.down <- "-" + hori.up <- "-" + corner.bot.left <- "-" + corner.bot.right <- "-" + corner.top.left <- "-" + corner.top.right <- "-" + tick.x.minor <- "|" + cross <- "+" + + } + + list(all.symbols = all.symbols, all.lines = all.lines, vert.right = vert.right, + vert = vert, vert.left = vert.left, hori = hori, hori.down = hori.down, + hori.up = hori.up, corner.bot.left = corner.bot.left, + corner.bot.right = corner.bot.right, corner.top.left = corner.top.left, + corner.top.right = corner.top.right, tick.x.minor = tick.x.minor, + cross = cross) + +} diff --git a/R/zzz.R b/R/zzz.R index c07ca6f..89272cd 100644 --- a/R/zzz.R +++ b/R/zzz.R @@ -1 +1,12 @@ -options(ascii = FALSE) \ No newline at end of file +#' console.plot: View R plots in the console +#' +#' @details +#' Create text-based plots. +#' +#' @docType package +#' @name console.plot-pkg +#' +#' @importFrom stats approx +NULL + +options(ascii = FALSE) diff --git a/man/console.plot-pkg.Rd b/man/console.plot-pkg.Rd new file mode 100644 index 0000000..af3bec5 --- /dev/null +++ b/man/console.plot-pkg.Rd @@ -0,0 +1,13 @@ +% Generated by roxygen2: do not edit by hand +% Please edit documentation in R/zzz.R +\docType{package} +\name{console.plot-pkg} +\alias{console.plot-pkg} +\alias{console.plot-pkg-package} +\title{console.plot: View R plots in the console} +\description{ +console.plot: View R plots in the console +} +\details{ +Create text-based plots. +} diff --git a/man/console.plot.Rd b/man/console.plot.Rd index 94d33ea..c0192fe 100644 --- a/man/console.plot.Rd +++ b/man/console.plot.Rd @@ -7,9 +7,9 @@ console.plot(x, y = NULL, groups = NULL, main = NULL, file = "", xlab = NULL, ylab = NULL, type = "p", point = NULL, line = NULL, abline.x = NULL, abline.y = NULL, - abline.overlay = FALSE, xlim = NULL, ylim = NULL, - plot.width = NULL, plot.height = NULL, legend = NULL, - ASCII = FALSE) + abline.overlay = FALSE, horizontal = FALSE, xlim = NULL, + ylim = NULL, plot.width = NULL, plot.height = NULL, + legend = NULL, ascii = getOption("ascii"), output = "cat") } \arguments{ \item{x}{Numeric vector of data.} @@ -38,6 +38,8 @@ console.plot(x, y = NULL, groups = NULL, main = NULL, file = "", \item{abline.overlay}{Allow abline to hide data points/line.} +\item{horizontal}{Flip axes.} + \item{xlim}{Min and max for x axis.} \item{ylim}{Min and max for y axis.} @@ -48,9 +50,9 @@ console.plot(x, y = NULL, groups = NULL, main = NULL, file = "", \item{legend}{Show legend.} -\item{ASCII}{Whether to draw plot using only ASCII characters.} +\item{ascii}{Whether to draw plot using only ASCII characters.} -\item{horizontal}{Flip axes.} +\item{output}{How to draw plot.} } \value{ Returns \code{NULL}, invisibly.