diff --git a/NEWS.md b/NEWS.md index 0c5068c9..570eac60 100644 --- a/NEWS.md +++ b/NEWS.md @@ -44,6 +44,9 @@ types enable a variety of additional features. (#222 @vincentarelbundock) of the x and y axes. This should work regardless of plot type, e.g. `plt(~Sepal.Length | Species, data = iris, type = "density", flip = TRUE)`. (#216 @grantmcdermott) +- `tpar()` gains additional `grid.col`, `grid.lty`, and `grid.lwd` arguments for + fine-grained control over the appearance of the default panel grid when + `plt(..., grid = TRUE)` is called. (#237 @grantmcdermott) Bug fixes: diff --git a/R/facet.R b/R/facet.R index acbef418..4797c490 100644 --- a/R/facet.R +++ b/R/facet.R @@ -424,7 +424,7 @@ draw_facet_window = function(grid, ...) { xg = pretty(as.Date(round(extendrange(xlim)), tz = tz)) } } - abline(v = xg, col = "lightgray", lty = "dotted", lwd = par("lwd")) + abline(v = xg, col = .tpar[["grid.col"]], lty = .tpar[["grid.lty"]], lwd = .tpar[["grid.lwd"]]) gnx = NA } if (!any(c(par("ylog"), type == "boxplot"))) { @@ -439,10 +439,10 @@ draw_facet_window = function(grid, ...) { yg = pretty(as.Date(extendrange(ylim), tz = tz)) } } - abline(h = yg, col = "lightgray", lty = "dotted", lwd = par("lwd")) + abline(h = yg, col = .tpar[["grid.col"]], lty = .tpar[["grid.lty"]], lwd = .tpar[["grid.lwd"]]) gny = NA } - grid(nx = gnx, ny = gny) + grid(nx = gnx, ny = gny, col = .tpar[["grid.col"]], lty = .tpar[["grid.lty"]], lwd = .tpar[["grid.lwd"]]) } } else { grid diff --git a/R/tpar.R b/R/tpar.R index 6173e6af..e8ab3c82 100644 --- a/R/tpar.R +++ b/R/tpar.R @@ -78,6 +78,15 @@ #' `grid` \tab\tab Logical indicating whether a background panel grid should be added to plots automatically. Defaults to NULL, which is equivalent to `FALSE`.\cr #' \tab\tab\cr #' \tab\tab\cr +#' `grid.col` \tab\tab Character or (integer) numeric that specifies the color of the panel grid lines. Defaults to `"lightgray"`.\cr +#' \tab\tab\cr +#' \tab\tab\cr +#' `grid.lty` \tab\tab Character or (integer) numeric that specifies the line type of the panel grid lines. Defaults to `"dotted"`.\cr +#' \tab\tab\cr +#' \tab\tab\cr +#' `grid.lwd` \tab\tab Non-negative numeric giving the line of the panel grid lines. Defaults to `1`.\cr +#' \tab\tab\cr +#' \tab\tab\cr #' `lmar` \tab\tab A numeric vector of form `c(inner, outer)` that gives the margin padding, in terms of lines, around the automatic `tinyplot` legend. Defaults to `c(1.0, 0.1)`, where the first number represents the "inner" margin between the legend and the plot region, and the second number represents the "outer" margin between the legend and edge of the graphics device. (Note that an exception for the definition of the "outer" legend margin occurs when the legend placement is `"top!"`, since the legend is placed above the plot region but below the main title. In such cases, the outer margin is relative to the existing gap between the title and the plot region, which is itself determined by `par("mar")[3]`.)\cr #' \tab\tab\cr #' \tab\tab\cr @@ -213,6 +222,23 @@ tpar = function(...) { .tpar$grid = grid } + if (length(opts$grid.col)) { + grid.col = opts$grid.col + .tpar$grid.col = grid.col + } + + if (length(opts$grid.lty)) { + grid.lty = opts$grid.lty + .tpar$grid.lty = grid.lty + } + + if (length(opts$grid.lwd)) { + grid.lwd = as.numeric(opts$grid.lwd) + if(!is.numeric(grid.lwd) || grid.lwd < 0) stop("grid.lwd needs to be a non-negative numeric") + .tpar$grid.lwd = grid.lwd + } + + if (length(opts$lmar)) { lmar = as.numeric(opts$lmar) if(!is.numeric(lmar)) stop("lmar needs to be numeric") diff --git a/R/zzz.R b/R/zzz.R index e86e19f1..3ce682a2 100644 --- a/R/zzz.R +++ b/R/zzz.R @@ -29,6 +29,9 @@ # Plot grid .tpar$grid = if(is.null(getOption("tinyplot_grid"))) FALSE else as.logical(getOption("tinyplot_grid")) + .tpar$grid.col = if(is.null(getOption("tinyplot_grid.col"))) "lightgray" else getOption("tinyplot_grid.col") + .tpar$grid.lty = if(is.null(getOption("tinyplot_grid.lty"))) "dotted" else getOption("tinyplot_grid.lty") + .tpar$grid.lwd = if(is.null(getOption("tinyplot_grid.lwd"))) 1 else as.numeric(getOption("tinyplot_grid.lwd")) # Legend margin, i.e. gap between the legend and the plot elements .tpar$lmar = if(is.null(getOption("tinyplot_lmar"))) c(1.0, 0.1) else as.numeric(getOption("tinyplot_lmar")) diff --git a/inst/tinytest/_tinysnapshot/tpar_grid.svg b/inst/tinytest/_tinysnapshot/tpar_grid.svg new file mode 100644 index 00000000..08c71d7a --- /dev/null +++ b/inst/tinytest/_tinysnapshot/tpar_grid.svg @@ -0,0 +1,83 @@ + + + + + + + + + + + + +Index +0:10 + + + + + + +2 +4 +6 +8 +10 + + + + + + + +0 +2 +4 +6 +8 +10 + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + diff --git a/inst/tinytest/test-tpar.R b/inst/tinytest/test-tpar.R new file mode 100644 index 00000000..d773e821 --- /dev/null +++ b/inst/tinytest/test-tpar.R @@ -0,0 +1,9 @@ +source("helpers.R") +using("tinysnapshot") + +f = function () { + op = tpar(grid = TRUE, grid.col = "hotpink", grid.lty = "solid", grid.lwd = 2) + tinyplot(0:10, pch = 19) + tpar(op) +} +expect_snapshot_plot(f, label = "tpar_grid") \ No newline at end of file diff --git a/man/tpar.Rd b/man/tpar.Rd index 7cde1771..07118f50 100644 --- a/man/tpar.Rd +++ b/man/tpar.Rd @@ -88,6 +88,15 @@ you should rather use \code{par()} instead. \code{grid} \tab\tab Logical indicating whether a background panel grid should be added to plots automatically. Defaults to NULL, which is equivalent to \code{FALSE}.\cr \tab\tab\cr \tab\tab\cr +\code{grid.col} \tab\tab Character or (integer) numeric that specifies the color of the panel grid lines. Defaults to \code{"lightgray"}.\cr +\tab\tab\cr +\tab\tab\cr +\code{grid.lty} \tab\tab Character or (integer) numeric that specifies the line type of the panel grid lines. Defaults to \code{"dotted"}.\cr +\tab\tab\cr +\tab\tab\cr +\code{grid.lwd} \tab\tab Non-negative numeric giving the line of the panel grid lines. Defaults to \code{1}.\cr +\tab\tab\cr +\tab\tab\cr \code{lmar} \tab\tab A numeric vector of form \code{c(inner, outer)} that gives the margin padding, in terms of lines, around the automatic \code{tinyplot} legend. Defaults to \code{c(1.0, 0.1)}, where the first number represents the "inner" margin between the legend and the plot region, and the second number represents the "outer" margin between the legend and edge of the graphics device. (Note that an exception for the definition of the "outer" legend margin occurs when the legend placement is \code{"top!"}, since the legend is placed above the plot region but below the main title. In such cases, the outer margin is relative to the existing gap between the title and the plot region, which is itself determined by \code{par("mar")[3]}.)\cr \tab\tab\cr \tab\tab\cr