Skip to content

Commit

Permalink
Fix graphical parameters
Browse files Browse the repository at this point in the history
  • Loading branch information
nfrerebeau committed Aug 18, 2023
1 parent 66eec01 commit 59c3599
Show file tree
Hide file tree
Showing 20 changed files with 194 additions and 150 deletions.
47 changes: 28 additions & 19 deletions R/AllGenerics.R
Original file line number Diff line number Diff line change
Expand Up @@ -559,21 +559,15 @@ NULL
#' plotted?
#' @param labels A [`logical`] scalar: should labels be drawn?
#' @param map_alpha,map_color,map_shape,map_size A vector specifying the
#' information to be highlighted.
#' It will be mapped to the corresponding aesthetic, according to the
#' corresponding `scale` argument (see examples and vignettes).
#' information to be highlighted. It will be mapped to the corresponding
#' aesthetic (see examples and vignettes).
#' If a single `character` string is passed, it must be one of "`observation`",
#' "`mass`", "`sum`", "`contribution`" or "`cos2`" (see details).
#' Any unambiguous substring can be given.
#' If `NULL` (the default), no highlighting is applied.
#' @param scale_color The colors for points. Multiple colors can be
#' specified so that each point can be given its own color.
#' @param scale_shape A vector of plotting characters or symbols.
#' @param scale_size A `numeric` vector giving the amount by which plotting
#' characters and symbols should be scaled relative to the default.
#' @param main A [`character`] string giving a main title for the plot.
#' @param sub A [`character`] string giving a subtitle for the plot.
#' @param ... Currently not used.
#' @param ... Further [graphical parameters][graphics::par] (see details).
#' @details
#' Available statistics:
#' \describe{
Expand All @@ -583,6 +577,16 @@ NULL
#' \item{`contribution`}{Joint contributions to the definition of `axes`.}
#' \item{`cos2`}{Joint \eqn{cos^2}{cos2} along `axes`.}
#' }
#'
#' Commonly used [graphical parameters][graphics::par] are:
#' \describe{
#' \item{`col`}{The colors for lines and points. Multiple colors can be
#' specified so that each point can be given its own color.}
#' \item{`pch`}{A vector of plotting characters or symbols.}
#' \item{`cex`}{A numerical vector giving the amount by which plotting
#' characters and symbols should be scaled relative to the
#' default.}
#' }
#' @return
#' `viz_*()` is called for its side-effects: it results in a graphic
#' being displayed. Invisibly returns `x`.
Expand Down Expand Up @@ -615,22 +619,15 @@ setGeneric(
#' plotted?
#' @param labels A [`logical`] scalar: should labels be drawn?
#' @param map_alpha,map_color,map_linetype,map_shape,map_size A vector
#' specifying the information to be highlighted.
#' It will be mapped to the corresponding aesthetic, according to the
#' corresponding `scale` argument (see examples and vignettes).
#' specifying the information to be highlighted. It will be mapped to the
#' corresponding aesthetic (see examples and vignettes).
#' If a single `character` string is passed, it must be one of "`observation`",
#' "`mass`", "`sum`", "`contribution`" or "`cos2`" (see details).
#' Any unambiguous substring can be given.
#' If `NULL` (the default), no highlighting is applied.
#' @param scale_color The colors for points. Multiple colors can be
#' specified so that each point can be given its own color.
#' @param scale_linetype A vector of line type specification.
#' @param scale_shape A vector of plotting characters or symbols.
#' @param scale_size A `numeric` vector giving the amount by which plotting
#' characters and symbols should be scaled relative to the default.
#' @param main A [`character`] string giving a main title for the plot.
#' @param sub A [`character`] string giving a subtitle for the plot.
#' @param ... Currently not used.
#' @param ... Further [graphical parameters][graphics::par] (see details).
#' @details
#' Available statistics:
#' \describe{
Expand All @@ -640,6 +637,18 @@ setGeneric(
#' \item{`contribution`}{Joint contributions to the definition of `axes`.}
#' \item{`cos2`}{Joint \eqn{cos^2}{cos2} along `axes`.}
#' }
#'
#' Commonly used [graphical parameters][graphics::par] are:
#' \describe{
#' \item{`col`}{The colors for lines and points. Multiple colors can be
#' specified so that each point can be given its own color.}
#' \item{`pch`}{A vector of plotting characters or symbols.}
#' \item{`cex`}{A numerical vector giving the amount by which plotting
#' characters and symbols should be scaled relative to the
#' default.}
#' \item{`lty`}{A vector of line types.}
#' \item{`lwd`}{A vector of line widths.}
#' }
#' @return
#' `viz_*()` is called for its side-effects: it results in a graphic
#' being displayed. Invisibly returns `x`.
Expand Down
2 changes: 1 addition & 1 deletion R/biplot.R
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ setMethod(
definition = function(x, axes = c(1, 2),
type = c("rows", "columns", "contributions"),
active = TRUE, sup = TRUE,
labels = c("rows", "columns"),
labels = "columns",
col.rows = "#004488", col.columns = "#BB5566",
pch.rows = 16, pch.columns = 17,
main = NULL, sub = NULL, ...) {
Expand Down
12 changes: 12 additions & 0 deletions R/dimensio-internal.R
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,18 @@

`%notin%` <- Negate(`%in%`)

recycle <- function(x, n, verbose = getOption("dimensio.verbose")) {
if (length(x) == n) return(x)

if (verbose) {
arg <- deparse(substitute(x))
msg <- sprintf("Note that %s was recycled to length %d.", sQuote(arg), n)
message(msg)
}
x <- rep_len(x, length.out = n)
x
}

#' Weighted Column Means and Standard Deviations
#'
#' @param x A [`numeric`] matrix.
Expand Down
106 changes: 42 additions & 64 deletions R/viz_coordinates.R
Original file line number Diff line number Diff line change
Expand Up @@ -13,16 +13,11 @@ setMethod(
labels = FALSE,
map_alpha = NULL, map_color = NULL,
map_shape = NULL, map_size = NULL,
scale_color = NULL, scale_shape = NULL,
scale_size = NULL,
main = NULL, sub = NULL, ...) {
viz_points(x, margin = 1, axes = axes,
active = active, sup = sup, labels = labels,
map_alpha = map_alpha, map_color = map_color,
map_shape = map_shape, map_size = map_size,
scale_color = scale_color,
scale_shape = scale_shape,
scale_size = scale_size,
main = main, sub = sub, ...)
invisible(x)
}
Expand All @@ -34,13 +29,11 @@ setMethod(
setMethod(
f = "viz_rows",
signature = c(x = "BootstrapCA"),
definition = function(x, axes = c(1, 2), scale_color = NULL,
scale_shape = NULL, ...) {
definition = function(x, axes = c(1, 2), ...) {
group <- get_groups(x, margin = 1)
viz_points(x, margin = 1, axes = axes,
active = TRUE, sup = TRUE, labels = FALSE,
map_color = group, scale_color = scale_color,
map_shape = group, scale_shape = scale_shape, ...)
map_color = group, map_shape = group, ...)
invisible(x)
}
)
Expand All @@ -55,13 +48,11 @@ setMethod(
definition = function(x, axes = c(1, 2), active = TRUE, sup = TRUE,
labels = FALSE, map_alpha = NULL, map_color = NULL,
map_shape = NULL, map_size = NULL,
scale_color = NULL, scale_shape = NULL,
scale_size = NULL, main = NULL, sub = NULL, ...) {
main = NULL, sub = NULL, ...) {
viz_rows(x, axes = axes, active = active, sup = sup, labels = labels,
map_alpha = map_alpha, map_color = map_color,
map_shape = map_shape, map_size = map_size,
scale_color = scale_color, scale_shape = scale_shape,
scale_size = scale_size, main = main, sub = sub, ...)
main = main, sub = sub, ...)
invisible(x)
}
)
Expand All @@ -77,15 +68,11 @@ setMethod(
definition = function(x, axes = c(1, 2), active = TRUE, sup = TRUE,
labels = FALSE, map_alpha = NULL, map_color = NULL,
map_shape = NULL, map_size = NULL,
scale_color = NULL, scale_shape = NULL,
scale_size = NULL, main = NULL, sub = NULL, ...) {
main = NULL, sub = NULL, ...) {
viz_points(x, margin = 2, axes = axes,
active = active, sup = sup, labels = labels,
map_alpha = map_alpha, map_color = map_color,
map_shape = map_shape, map_size = map_size,
scale_color = scale_color,
scale_shape = scale_shape,
scale_size = scale_size,
main = main, sub = sub, ...)
invisible(x)
}
Expand All @@ -97,12 +84,10 @@ setMethod(
setMethod(
f = "viz_columns",
signature = c(x = "BootstrapCA"),
definition = function(x, axes = c(1, 2), scale_color = NULL,
scale_shape = NULL, ...) {
definition = function(x, axes = c(1, 2), ...) {
group <- get_groups(x, margin = 2)
viz_points(x, margin = 2, axes = axes, active = TRUE, sup = TRUE,
labels = FALSE, map_color = group, scale_color = scale_color,
map_shape = group, scale_shape = scale_shape, ...)
labels = FALSE, map_color = group, map_shape = group, ...)
invisible(x)
}
)
Expand All @@ -117,17 +102,16 @@ setMethod(
definition = function(x, axes = c(1, 2), active = TRUE, sup = TRUE,
labels = TRUE, map_alpha = NULL, map_color = NULL,
map_linetype = NULL, map_size = NULL,
scale_color = NULL, scale_linetype = NULL,
scale_size = NULL, main = NULL, sub = NULL, ...) {
main = NULL, sub = NULL, ...) {
## Prepare data
coord <- prepare_coord(x, margin = 2, axes = axes, active = active,
sup = sup)

## Graphical parameters
param <- prepare_param(coord, map_alpha = map_alpha,
map_color = map_color, scale_color = scale_color,
map_linetype = map_linetype, scale_linetype = scale_linetype,
map_size = map_size, scale_size = scale_size)
map_color = map_color,
map_linetype = map_linetype,
map_size = map_size, ...)

## Open new window
grDevices::dev.hold()
Expand Down Expand Up @@ -159,9 +143,8 @@ setMethod(

## Labels
if (labels && nrow(coord) > 1) {
usr <- graphics::par("usr")
viz_labels(x = coord$x, y = coord$y, labels = coord$label,
xlim = usr[c(1, 2)], ylim = usr[c(3, 4)], col = param$col)
col = param$col)
}

## Evaluate post-plot and pre-axis expressions
Expand Down Expand Up @@ -197,40 +180,34 @@ setMethod(
setMethod(
f = "viz_variables",
signature = c(x = "BootstrapPCA"),
definition = function(x, axes = c(1, 2), scale_color = NULL,
scale_shape = NULL, ...) {
definition = function(x, axes = c(1, 2), ...) {
group <- get_groups(x, margin = 2)
viz_points(x, margin = 2, axes = axes, active = TRUE, sup = TRUE,
labels = FALSE, map_color = group, scale_color = scale_color,
map_shape = group, scale_shape = scale_shape, ...)
labels = FALSE, map_color = group, map_shape = group, ...)
invisible(x)
}
)

# Helpers ======================================================================
viz_points <- function(x, margin, axes, active = TRUE, sup = TRUE, labels = FALSE,
map_alpha = NULL, map_color = NULL, scale_color = NULL,
map_shape = NULL, scale_shape = NULL,
map_size = NULL, scale_size = NULL,
main = NULL, sub = NULL, ...) {
map_alpha = NULL, map_color = NULL, map_shape = NULL,
map_size = NULL, main = NULL, sub = NULL, ...) {
## Prepare data
coord <- prepare_coord(x, margin = margin, axes = axes, active = active,
sup = sup)

## Graphical parameters
param <- prepare_param(coord, map_alpha = map_alpha,
map_color = map_color, scale_color = scale_color,
map_shape = map_shape, scale_shape = scale_shape,
map_size = map_size, scale_size = scale_size)
param <- prepare_param(coord, map_alpha = map_alpha, map_color = map_color,
map_shape = map_shape, map_size = map_size, ...)

## Open new window
grDevices::dev.hold()
on.exit(grDevices::dev.flush(), add = TRUE)
graphics::plot.new()

## Set plotting coordinates
xlim <- range(coord$x)
ylim <- range(coord$y)
xlim <- range(coord$x, na.rm = TRUE)
ylim <- range(coord$y, na.rm = TRUE)
graphics::plot.window(xlim = xlim, ylim = ylim, asp = 1)

## Evaluate pre-plot expressions
Expand All @@ -244,9 +221,8 @@ viz_points <- function(x, margin, axes, active = TRUE, sup = TRUE, labels = FALS

## Labels
if (labels) {
usr <- graphics::par("usr")
viz_labels(x = coord$x, y = coord$y, labels = coord$label,
xlim = usr[c(1, 2)], ylim = usr[c(3, 4)], col = param$col, ...)
col = param$col, ...)
}

## Evaluate post-plot and pre-axis expressions
Expand Down Expand Up @@ -339,19 +315,17 @@ prepare_coord <- function(object, margin, axes = c(1, 2), active = TRUE,
data
}

prepare_param <- function(x, map_alpha = NULL,
map_color = NULL, scale_color = NULL,
map_size = NULL, scale_size = NULL,
map_linetype = NULL, scale_linetype = NULL,
map_shape = NULL, scale_shape = NULL) {
prepare_param <- function(x, map_alpha = NULL, map_color = NULL,
map_size = NULL, map_linetype = NULL,
map_shape = NULL, ...) {
n <- nrow(x)

## Graphical parameters
col <- scale_color
pch <- scale_shape
lty <- scale_linetype
cex <- scale_size %||% graphics::par("cex")
lwd <- scale_size %||% graphics::par("lwd")
col <- list(...)$col
pch <- list(...)$pch
lty <- list(...)$lty
cex <- list(...)$cex %||% graphics::par("cex")
lwd <- list(...)$lwd %||% graphics::par("lwd")

choices <- c("observation", "mass", "sum", "contribution", "cos2")

Expand All @@ -371,12 +345,13 @@ prepare_param <- function(x, map_alpha = NULL,
} else {
## Discrete scale
n_col <- length(unique(map_color))
if (is.null(col)) col <- grDevices::hcl.colors(n_col, "viridis")
if (length(col) < n_col) assert_length(col, n_col)
if (is.null(col))
col <- grDevices::hcl.colors(n_col, "viridis")
col <- recycle(col, n_col)
col <- col[as.factor(map_color)]
}
} else {
col <- rep(col %||% graphics::par("col"), n)
col <- rep_len(col %||% graphics::par("col"), n)
}

if (!is.null(map_alpha)) {
Expand All @@ -397,10 +372,11 @@ prepare_param <- function(x, map_alpha = NULL,
}

n_lty <- length(unique(map_linetype))
if (is.null(lty)) lty <- seq_along(unique(map_linetype))
if (is.null(lty)) lty <- seq_len(n_lty)
lty <- recycle(lty, n_lty)
lty <- lty[as.factor(map_linetype)]
} else {
lty <- rep(lty %||% graphics::par("lty"), n)
lty <- rep_len(lty %||% graphics::par("lty"), n)
}

if (!is.null(map_shape)) {
Expand All @@ -409,10 +385,12 @@ prepare_param <- function(x, map_alpha = NULL,
map_shape <- x[[map_shape]]
}

if (is.null(pch)) pch <- seq_along(unique(map_shape))
n_pch <- length(unique(map_shape))
if (is.null(pch)) pch <- seq_len(n_pch)
pch <- recycle(pch, n_pch)
pch <- pch[as.factor(map_shape)]
} else {
pch <- rep(pch %||% graphics::par("pch"), n)
pch <- rep_len(pch %||% graphics::par("pch"), n)
}

if (!is.null(map_size)) {
Expand All @@ -424,8 +402,8 @@ prepare_param <- function(x, map_alpha = NULL,
cex <- cex + map_size / max(map_size)
lwd <- lwd + map_size / max(map_size)
} else {
cex <- rep(cex, n)
lwd <- rep(lwd, n)
cex <- rep_len(cex, n)
lwd <- rep_len(lwd, n)
}

data.frame(col = col, pch = pch, cex = cex, lty = lty, lwd = lwd)
Expand Down
Loading

0 comments on commit 59c3599

Please sign in to comment.