diff --git a/R/by_aesthetics.R b/R/by_aesthetics.R index 821d1ac4..d94bb139 100755 --- a/R/by_aesthetics.R +++ b/R/by_aesthetics.R @@ -152,7 +152,7 @@ by_pch = function(ngrps, type, pch = NULL) { by_lty = function(ngrps, type, lty = NULL) { # We only care about line types, otherwise return NULL - if (!type %in% c("l", "b", "o", "c", "h", "s", "S", "ribbon", "boxplot", "rect", "segments", "qq")) { + if (!type %in% c("l", "b", "o", "c", "h", "s", "S", "ribbon", "boxplot", "rect", "segments", "qq", "abline", "hline", "vline")) { out = NULL # special "by" convenience keyword diff --git a/R/tinyplot.R b/R/tinyplot.R index 228edba7..8da11c4e 100644 --- a/R/tinyplot.R +++ b/R/tinyplot.R @@ -49,8 +49,8 @@ #' precedence if both are specified together. Ignored if a two-sided formula #' is passed to the main `facet` argument, since the layout is arranged in a #' fixed grid. -#' - `free` a logical value indicating whether the axis limits (scales) for -#' each individual facet should adjust independently to match the range of +#' - `free` a logical value indicating whether the axis limits (scales) for +#' each individual facet should adjust independently to match the range of #' the data within that facet. Default is `FALSE`. Separate free scaling of #' the x- or y-axis (i.e., whilst holding the other axis fixed) is not #' currently supported. @@ -1101,7 +1101,7 @@ tinyplot.default = function( mfgj = ii %% nfacet_cols if (mfgj == 0) mfgj = nfacet_cols par(mfg = c(mfgi, mfgj)) - + # For free facets, we need to reset par(usr) based extent of that # particular facet... which we calculated and saved to the .fusr env var # (list) back in draw_facet_window() diff --git a/R/type_abline.R b/R/type_abline.R index e7b6e146..17ebb89f 100644 --- a/R/type_abline.R +++ b/R/type_abline.R @@ -1,7 +1,6 @@ #' Add straight lines to a plot #' #' @inheritParams graphics::abline -#' @inheritParams tinyplot #' @examples #' mod = lm(mpg ~ hp, data = mtcars) #' y = mtcars$mpg @@ -9,7 +8,7 @@ #' tinyplot(y, yhat, xlim = c(0, 40), ylim = c(0, 40)) #' tinyplot_add(type = type_abline(a = 0, b = 1)) #' @export -type_abline = function(a = 0, b = 1, col = NULL, lty = NULL, lwd = NULL) { +type_abline = function(a = 0, b = 1) { data_abline = function(datapoints, ...) { if (nrow(datapoints) == 0) { msg = "`type_abline() only works on existing plots with x and y data points." @@ -35,37 +34,7 @@ type_abline = function(a = 0, b = 1, col = NULL, lty = NULL, lwd = NULL) { stop(msg, call. = FALSE) } - if (is.null(col)) { - col = icol - } - if (length(col) == 1) { - col = rep(col, nfacets) - } else if (length(col) != nfacets) { - msg = "Length of 'col' must be 1 or equal to the number of facets" - stop(msg, call. = FALSE) - } - - if (is.null(lty)) { - lty = if (!is.null(ilty)) ilty else 1 - } - if (length(lty) == 1) { - lty = rep(lty, nfacets) - } else if (length(lty) != nfacets) { - msg = "Length of 'lty' must be 1 or equal to the number of facets" - stop(msg, call. = FALSE) - } - - if (is.null(lwd)) { - lwd = if (!is.null(ilwd)) ilwd else 1 - } - if (length(lwd) == 1) { - lwd = rep(lwd, nfacets) - } else if (length(lwd) != nfacets) { - msg = "Length of 'lwd' must be 1 or equal to the number of facets" - stop(msg, call. = FALSE) - } - - abline(a = a[ifacet], b = b[ifacet], col = col[ifacet], lty = lty[ifacet], lwd = lwd[ifacet]) + abline(a = a[ifacet], b = b[ifacet], col = icol, lty = ilty, lwd = ilwd) } return(fun) } diff --git a/R/type_hline.R b/R/type_hline.R index 77d81716..60cab4c2 100644 --- a/R/type_hline.R +++ b/R/type_hline.R @@ -1,13 +1,11 @@ #' Trace a horizontal line on the plot #' #' @param h y-value(s) for horizontal line(s). Numeric of length 1 or equal to the number of facets. -#' @inheritParams graphics::abline -#' @inheritParams tinyplot #' @examples #' tinyplot(mpg ~ hp | factor(cyl), facet = ~ factor(cyl), data = mtcars) -#' tinyplot_add(type = type_hline(h = 12, col = "pink", lty = 3, lwd = 3)) +#' tinyplot_add(type = type_hline(h = 12), col = "pink", lty = 3, lwd = 3) #' @export -type_hline = function(h = 0, col = NULL, lty = NULL, lwd = NULL) { +type_hline = function(h = 0) { data_hline = function(datapoints, ...) { if (nrow(datapoints) == 0) { msg = "`type_hline() only works on existing plots with x and y data points." @@ -26,37 +24,7 @@ type_hline = function(h = 0, col = NULL, lty = NULL, lwd = NULL) { stop(msg, call. = FALSE) } - if (is.null(col)) { - col = icol - } - if (length(col) == 1) { - col = rep(col, nfacets) - } else if (length(col) != nfacets) { - msg = "Length of 'col' must be 1 or equal to the number of facets" - stop(msg, call. = FALSE) - } - - if (is.null(lty)) { - lty = if (!is.null(ilty)) ilty else 1 - } - if (length(lty) == 1) { - lty = rep(lty, nfacets) - } else if (length(lty) != nfacets) { - msg = "Length of 'lty' must be 1 or equal to the number of facets" - stop(msg, call. = FALSE) - } - - if (is.null(lwd)) { - lwd = if (!is.null(ilwd)) ilwd else 1 - } - if (length(lwd) == 1) { - lwd = rep(lwd, nfacets) - } else if (length(lwd) != nfacets) { - msg = "Length of 'lwd' must be 1 or equal to the number of facets" - stop(msg, call. = FALSE) - } - - abline(h = h[ifacet], col = col[ifacet], lty = lty[ifacet], lwd = lwd[ifacet]) + abline(h = h[ifacet], col = icol, lty = ilty, lwd = ilwd) } return(fun) } diff --git a/R/type_vline.R b/R/type_vline.R index a581487e..462c85b1 100644 --- a/R/type_vline.R +++ b/R/type_vline.R @@ -1,7 +1,6 @@ #' Trace a vertical line on the plot #' #' @param v x-value(s) for vertical line(s). Numeric of length 1 or equal to the number of facets. -#' @inheritParams tinyplot #' @examples #' tinyplot(mpg ~ hp, data = mtcars) #' tinyplot_add(type = type_vline(150)) @@ -9,11 +8,10 @@ #' # facet-specify location and colors #' cols = c("black", "green", "orange") #' tinyplot(mpg ~ hp | factor(cyl), facet = ~ factor(cyl), data = mtcars, col = cols) -#' tinyplot_add(type = type_vline( -#' v = c(100, 150, 200), col = cols, lty = 3, lwd = 3 -#' )) +#' tinyplot_add(type = type_vline(v = c(100, 150, 200)), lty = 3, lwd = 3) +#' #' @export -type_vline = function(v = 0, col = "black", lty = 1, lwd = 1) { +type_vline = function(v = 0) { assert_numeric(v) data_vline = function(datapoints, ...) { if (nrow(datapoints) == 0) { @@ -33,37 +31,7 @@ type_vline = function(v = 0, col = "black", lty = 1, lwd = 1) { stop(msg, call. = FALSE) } - if (is.null(col)) { - col = icol - } - if (length(col) == 1) { - col = rep(col, nfacets) - } else if (length(col) != nfacets) { - msg = "Length of 'col' must be 1 or equal to the number of facets" - stop(msg, call. = FALSE) - } - - if (is.null(lty)) { - lty = if (!is.null(ilty)) ilty else 1 - } - if (length(lty) == 1) { - lty = rep(lty, nfacets) - } else if (length(lty) != nfacets) { - msg = "Length of 'lty' must be 1 or equal to the number of facets" - stop(msg, call. = FALSE) - } - - if (is.null(lwd)) { - lwd = if (!is.null(ilwd)) ilwd else 1 - } - if (length(lwd) == 1) { - lwd = rep(lwd, nfacets) - } else if (length(lwd) != nfacets) { - msg = "Length of 'lwd' must be 1 or equal to the number of facets" - stop(msg, call. = FALSE) - } - - abline(v = v[ifacet], col = col[ifacet], lty = lty[ifacet], lwd = lwd[ifacet]) + abline(v = v[ifacet], col = icol, lty = ilty, lwd = ilwd) } return(fun) } diff --git a/inst/tinytest/test-abline.R b/inst/tinytest/test-abline.R index 98c4c92d..578e93de 100644 --- a/inst/tinytest/test-abline.R +++ b/inst/tinytest/test-abline.R @@ -8,19 +8,20 @@ expect_error(tinyplot(type = type_abline(a = 0, b = 1)), pattern = "data points" f = function() { plt(mpg ~ hp | factor(cyl), facet = ~ factor(cyl), data = mtcars) - plt_add(type = type_hline(h = 12, col = "pink", lty = 3, lwd = 3)) + plt_add(type = type_hline(h = 12), col = "pink", lty = 3, lwd = 3) } expect_snapshot_plot(f, label = "hline") f = function() { - tinyplot(mpg ~ hp | factor(cyl), - facet = ~ factor(cyl), data = mtcars, + tinyplot( + mpg ~ hp | factor(cyl), + facet = ~ factor(cyl), + data = mtcars, col = c("black", "green", "orange")) - tinyplot_add(type = type_vline( - v = c(100, 150, 200), lty = 3, lwd = 3, - col = c("black", "green", "orange") - )) + tinyplot_add( + lty = 3, lwd = 3, col = c("black", "green", "orange"), + type = type_vline(v = c(100, 150, 200))) } expect_snapshot_plot(f, label = "vline_vector") diff --git a/man/tinyplot_add.Rd b/man/tinyplot_add.Rd index cadced23..916e7159 100644 --- a/man/tinyplot_add.Rd +++ b/man/tinyplot_add.Rd @@ -42,18 +42,18 @@ arguments (\code{x}, \code{y}, etc.) are attached to your global R environment.) library(tinyplot) tinyplot(Sepal.Width ~ Sepal.Length | Species, - facet = ~Species, - data = iris) + facet = ~Species, + data = iris) -tinyplot_add(type = "lm") ## or : plt_add(type = "lm") +tinyplot_add(type = "lm") ## or : plt_add(type = "lm") ## Note: the previous function is equivalent to (but much more convenient ## than) re-writing the full call with the new type and `add=TRUE`: # tinyplot(Sepal.Width ~ Sepal.Length | Species, # facet = ~Species, -# data = iris, -# type = "lm", +# data = iris, +# type = "lm", # add = TRUE) } diff --git a/man/type_abline.Rd b/man/type_abline.Rd index a25b1c94..365ef469 100644 --- a/man/type_abline.Rd +++ b/man/type_abline.Rd @@ -4,32 +4,10 @@ \alias{type_abline} \title{Add straight lines to a plot} \usage{ -type_abline(a = 0, b = 1, col = NULL, lty = NULL, lwd = NULL) +type_abline(a = 0, b = 1) } \arguments{ \item{a, b}{the intercept and slope, single values.} - -\item{col}{plotting color. Character, integer, or vector of length equal to -the number of categories in the \code{by} variable. See \code{col}. Note that the -default behaviour in \code{tinyplot} is to vary group colors along any variables -declared in the \code{by} argument. Thus, specifying colors manually should not -be necessary unless users wish to override the automatic colors produced by -this grouping process. Typically, this would only be done if grouping -features are deferred to some other graphical parameter (i.e., passing the -"by" keyword to one of \code{pch}, \code{lty}, \code{lwd}, or \code{bg}; see below.)} - -\item{lty}{line type. Character, integer, or vector of length equal to the -number of categories in the \code{by} variable. See \code{lty}. In addition, users -can supply a special \code{lty = "by"} convenience argument, in which case the -line type will automatically loop over the number groups. This automatic -looping will begin at the global line type value (i.e., \code{par("lty")}) and -recycle as necessary.} - -\item{lwd}{line width. Numeric scalar or vector of length equal to the -number of categories in the \code{by} variable. See \code{lwd}. In addition, users -can supply a special \code{lwd = "by"} convenience argument, in which case the -line width will automatically loop over the number of groups. This -automatic looping will be centered at the global line width value (i.e.,} } \description{ Add straight lines to a plot diff --git a/man/type_hline.Rd b/man/type_hline.Rd index 84469da7..8e85d745 100644 --- a/man/type_hline.Rd +++ b/man/type_hline.Rd @@ -4,37 +4,15 @@ \alias{type_hline} \title{Trace a horizontal line on the plot} \usage{ -type_hline(h = 0, col = NULL, lty = NULL, lwd = NULL) +type_hline(h = 0) } \arguments{ \item{h}{y-value(s) for horizontal line(s). Numeric of length 1 or equal to the number of facets.} - -\item{col}{plotting color. Character, integer, or vector of length equal to -the number of categories in the \code{by} variable. See \code{col}. Note that the -default behaviour in \code{tinyplot} is to vary group colors along any variables -declared in the \code{by} argument. Thus, specifying colors manually should not -be necessary unless users wish to override the automatic colors produced by -this grouping process. Typically, this would only be done if grouping -features are deferred to some other graphical parameter (i.e., passing the -"by" keyword to one of \code{pch}, \code{lty}, \code{lwd}, or \code{bg}; see below.)} - -\item{lty}{line type. Character, integer, or vector of length equal to the -number of categories in the \code{by} variable. See \code{lty}. In addition, users -can supply a special \code{lty = "by"} convenience argument, in which case the -line type will automatically loop over the number groups. This automatic -looping will begin at the global line type value (i.e., \code{par("lty")}) and -recycle as necessary.} - -\item{lwd}{line width. Numeric scalar or vector of length equal to the -number of categories in the \code{by} variable. See \code{lwd}. In addition, users -can supply a special \code{lwd = "by"} convenience argument, in which case the -line width will automatically loop over the number of groups. This -automatic looping will be centered at the global line width value (i.e.,} } \description{ Trace a horizontal line on the plot } \examples{ tinyplot(mpg ~ hp | factor(cyl), facet = ~ factor(cyl), data = mtcars) -tinyplot_add(type = type_hline(h = 12, col = "pink", lty = 3, lwd = 3)) +tinyplot_add(type = type_hline(h = 12), col = "pink", lty = 3, lwd = 3) } diff --git a/man/type_vline.Rd b/man/type_vline.Rd index d52a892c..8fb91dcd 100644 --- a/man/type_vline.Rd +++ b/man/type_vline.Rd @@ -4,32 +4,10 @@ \alias{type_vline} \title{Trace a vertical line on the plot} \usage{ -type_vline(v = 0, col = "black", lty = 1, lwd = 1) +type_vline(v = 0) } \arguments{ \item{v}{x-value(s) for vertical line(s). Numeric of length 1 or equal to the number of facets.} - -\item{col}{plotting color. Character, integer, or vector of length equal to -the number of categories in the \code{by} variable. See \code{col}. Note that the -default behaviour in \code{tinyplot} is to vary group colors along any variables -declared in the \code{by} argument. Thus, specifying colors manually should not -be necessary unless users wish to override the automatic colors produced by -this grouping process. Typically, this would only be done if grouping -features are deferred to some other graphical parameter (i.e., passing the -"by" keyword to one of \code{pch}, \code{lty}, \code{lwd}, or \code{bg}; see below.)} - -\item{lty}{line type. Character, integer, or vector of length equal to the -number of categories in the \code{by} variable. See \code{lty}. In addition, users -can supply a special \code{lty = "by"} convenience argument, in which case the -line type will automatically loop over the number groups. This automatic -looping will begin at the global line type value (i.e., \code{par("lty")}) and -recycle as necessary.} - -\item{lwd}{line width. Numeric scalar or vector of length equal to the -number of categories in the \code{by} variable. See \code{lwd}. In addition, users -can supply a special \code{lwd = "by"} convenience argument, in which case the -line width will automatically loop over the number of groups. This -automatic looping will be centered at the global line width value (i.e.,} } \description{ Trace a vertical line on the plot @@ -41,7 +19,6 @@ tinyplot_add(type = type_vline(150)) # facet-specify location and colors cols = c("black", "green", "orange") tinyplot(mpg ~ hp | factor(cyl), facet = ~ factor(cyl), data = mtcars, col = cols) -tinyplot_add(type = type_vline( - v = c(100, 150, 200), col = cols, lty = 3, lwd = 3 -)) +tinyplot_add(type = type_vline(v = c(100, 150, 200)), lty = 3, lwd = 3) + }