Skip to content

Commit

Permalink
Facet formula (#101)
Browse files Browse the repository at this point in the history
* Basic facet formula support

* facet_grid support

* Catch for RHS strip adjustment with "right!" legend

* doc tweaks

* Fix facet grid row and col logic

- The LHS variable should determine rows not columns
- Also fix some regex

* Fix docs

* fix example

* Add facet grid example

* Drop mtext for facet titles and add customization options

- Rather use a combo of text and rect
- Allows background fill and border, and rotation of rhs facet grid title
- Customization option via par2 or facet.args

* Document

* Update tests with mew facet strip logic

* Add facet grid to vignette

* Update README

* NEWS and version bump
  • Loading branch information
grantmcdermott authored Jan 25, 2024
1 parent 126228d commit 48d5145
Show file tree
Hide file tree
Showing 40 changed files with 2,301 additions and 378 deletions.
2 changes: 1 addition & 1 deletion DESCRIPTION
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
Package: plot2
Type: Package
Title: Lightweight extension of base R plot
Version: 0.0.3.9017
Version: 0.0.3.9018
Authors@R:
c(
person(
Expand Down
4 changes: 2 additions & 2 deletions NEWS.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
# News

## 0.0.3.917 (development version)
## 0.0.3.918 (development version)

Website:

Expand All @@ -26,7 +26,7 @@ existing plot window. (#60 @grantmcdermott)
- `plot2` gains a new `facet` argument for drawing faceted plots. Users can
override the default square arrangement by passing the desired number of facet
rows or columns to the companion `facet.args` helper function. Facets can be
combined with `by` grouping, or used on their own. (#83, #91, #94, #96
combined with `by` grouping, or used on their own. (#83, #91, #94, #96, #101
@grantmcdermott)
- Users can now control `plot2`-specific graphical parameters globally via
the new `par2()` function (which is modeled on the base `par()` function). At
Expand Down
47 changes: 47 additions & 0 deletions R/par2.R
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,21 @@
#' @section Graphical Parameters:
#'
#' \tabular{lll}{
#' `facet.cex` \tab\tab Expansion factor for facet titles. Defaults to `1`.\cr
#' \tab\tab\cr
#' \tab\tab\cr
#' `facet.font` \tab\tab An integer corresponding to the desired font face for facet titles. For most font families and graphics devices, one of four possible values: `1` (regular), `2` (bold), `3` (italic), or `4` (bold italic). Defaults to `NULL`, which is equivalent to `1` (i.e., regular).\cr
#' \tab\tab\cr
#' \tab\tab\cr
#' `facet.col` \tab\tab Character or integer specifying the facet text colour. If an integer, will correspond to the user's default global colour palette (see \code{\link[grDevices]{palette}}). Defaults to `NULL`, which is equivalent to "black".\cr
#' \tab\tab\cr
#' \tab\tab\cr
#' `facet.bg` \tab\tab Character or integer specifying the facet background colour. If an integer, will correspond to the user's default colour palette (see \code{\link[grDevices]{palette}}). Passed \code{\link[graphics]{rect}}. Defaults to `NULL` (none).\cr
#' \tab\tab\cr
#' \tab\tab\cr
#' `facet.border` \tab\tab Character or integer specifying the facet border colour. If an integer, will correspond to the users default colour palette (see \code{\link[grDevices]{palette}}). Passed \code{\link[graphics]{rect}}. Defaults to `NA` (none).\cr
#' \tab\tab\cr
#' \tab\tab\cr
#' `fmar` \tab\tab A numeric vector of form `c(b,l,t,r)` for controlling the (base) margin padding, in terms of lines, between the individual facets in a faceted plot. Defaults to `c(1,1,1,1)`, i.e. a single line of padding around each facet. If more that three facets are detected, the `fmar` parameter is scaled by 0.75 (i.e., three-quarters) to reduce the excess whitespace that would otherwise arise due to the absent axes lines and labels. (An exception is made for 2x2 plots to better match the `cex` expansion logic of the base graphics system under this particular layout.) Similarly, note that an extra 0.5 lines is subtracted from each side of the facet padding for plots that aren't framed, to reduce excess whitespace.\cr
#' \tab\tab\cr
#' \tab\tab\cr
Expand All @@ -29,6 +44,38 @@ par2 = function(...) {
par2_old = as.list(.par2)
nam = names(opts)

if (length(opts$facet.cex)) {
facet.cex = as.numeric(opts$facet.cex)
if(!is.numeric(facet.cex)) stop("facet.cex needs to be numeric")
if(length(facet.cex)!=1) stop("facet.cex needs to be of length 1")
.par2$facet.cex = facet.cex
}

if (length(opts$facet.font)) {
facet.font = as.numeric(opts$facet.font)
if(!is.numeric(facet.font)) stop("facet.font needs to be numeric")
if(length(facet.font)!=1) stop("facet.font needs to be of length 1")
.par2$facet.font = facet.font
}

if (length(opts$facet.col)) {
if(!is.numeric(facet.col) || !is.character(facet.col)) stop("facet.col needs to be a numeric or character")
if(length(facet.col)!=1) stop("facet.col needs to be of length 1")
.par2$facet.col = facet.col
}

if (length(opts$facet.bg)) {
if(!is.numeric(facet.bg) || !is.character(facet.bg)) stop("facet.bg needs to be a numeric or character")
if(length(facet.bg)!=1) stop("facet.bg needs to be of length 1")
.par2$facet.bg = facet.bg
}

if (length(opts$facet.border)) {
if(!is.numeric(facet.border) || !is.character(facet.border)) stop("facet.border needs to be a numeric or character")
if(length(facet.border)!=1) stop("facet.border needs to be of length 1")
.par2$facet.border = facet.border
}

if (length(opts$fmar)) {
fmar = as.numeric(opts$fmar)
if(!is.numeric(fmar)) stop("fmar needs to be numeric")
Expand Down
Loading

0 comments on commit 48d5145

Please sign in to comment.