Skip to content

Commit

Permalink
Fix plot zoom
Browse files Browse the repository at this point in the history
  • Loading branch information
nfrerebeau committed Nov 22, 2024
1 parent 9a934d2 commit d331c88
Show file tree
Hide file tree
Showing 10 changed files with 183 additions and 156 deletions.
1 change: 1 addition & 0 deletions DESCRIPTION
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,7 @@ Collate:
'ternary_segments.R'
'ternary_text.R'
'ternary_title.R'
'ternary_window.R'
'triangle_phase.R'
'triangle_soil.R'
'zzz.R'
6 changes: 3 additions & 3 deletions R/AllGenerics.R
Original file line number Diff line number Diff line change
Expand Up @@ -76,11 +76,11 @@ setGeneric(
#' interpret `x` in a suitable way (see [grDevices::xyz.coords()]).
#' @param center A [`logical`] scalar: should the data be centered?
#' @param scale A [`logical`] scalar: should the data be scaled?
#' @param xlim A length-two [`numeric`] vector giving the `x` limits in the
#' @param xlim A length-three [`numeric`] vector giving the `x` limits in the
#' range \eqn{[0,1]}.
#' @param ylim A length-two [`numeric`] vector giving the `y` limits in the
#' @param ylim A length-three [`numeric`] vector giving the `y` limits in the
#' range \eqn{[0,1]}.
#' @param zlim A length-two [`numeric`] vector giving the `z` limits in the
#' @param zlim A length-three [`numeric`] vector giving the `z` limits in the
#' range \eqn{[0,1]}.
#' @param xlab,ylab,zlab A [`character`] string giving a label for the x, y and
#' z axes.
Expand Down
30 changes: 3 additions & 27 deletions R/ternary_plot.R
Original file line number Diff line number Diff line change
Expand Up @@ -30,23 +30,9 @@ setMethod(
graphics::plot.new()

## Set plotting coordinates
if (is.null(xlim) && is.null(ylim) && is.null(zlim)) {
dx <- max(graphics::strwidth(c(xlab, ylab, zlab), cex = cex.lab)) / 2
rx <- expand_range(c(0, 1), add = dx)
lim <- list(
x = rx,
y = .top / 2 + c(-1, 1) * diff(rx) / 2
)
} else {
xrange <- yrange <- zrange <- NULL
if (!is.null(xlim)) xrange <- boundary(1, xlim)
if (!is.null(ylim)) yrange <- boundary(2, ylim)
if (!is.null(zlim)) zrange <- boundary(3, zlim)
lim <- coordinates_ternary(rbind(xrange, yrange, zrange))
lim$x <- range(lim$x)
lim$y <- range(lim$y)
}
graphics::plot.window(xlim = lim$x, ylim = lim$y, asp = 1)
ternary_window(xlim = xlim, ylim = ylim, zlim = zlim,
xlab = xlab, ylab = ylab, zlab = zlab,
cex = cex.lab)

## Reset center and scale
options(isopleuros.center = NULL)
Expand Down Expand Up @@ -122,13 +108,3 @@ setMethod(
invisible(pt)
}
)

boundary <- function(side, limits) {
limits <- range(limits)
side_min <- diag(1, 3, 3) - limits[1L]
side_max <- diag(1, 3, 3) - limits[2L]
side_range <- rbind(side_min, side_max)
side_range[side_range < 0] <- 0
side_range[, side] <- rep(limits, each = 3)
side_range[rowSums(side_range) == 1, ]
}
51 changes: 51 additions & 0 deletions R/ternary_window.R
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
# TERNARY WINDOW
#' @include AllGenerics.R
NULL

#' Set up Ternary Coordinates for Graphics Window
#'
#' @param xlim A length-three [`numeric`] vector giving the `x` limits in the
#' range \eqn{[0,1]}.
#' @param ylim A length-three [`numeric`] vector giving the `y` limits in the
#' range \eqn{[0,1]}.
#' @param zlim A length-three [`numeric`] vector giving the `z` limits in the
#' range \eqn{[0,1]}.
#' @param xlab,ylab,zlab A [`character`] string giving a label for the x, y and
#' z axes.
#' @return
#' `ternary_window()` is called it for its side-effects
#' (see [graphics::plot.window()]).
#' @keywords internal
#' @noRd
ternary_window <- function(xlim = NULL, ylim = NULL, zlim = NULL,
xlab = NULL, ylab = NULL, zlab = NULL,
cex = 1) {

n_null <- is.null(xlim) + is.null(ylim) + is.null(zlim)

if (n_null == 3) {
dx <- max(graphics::strwidth(c(xlab, ylab, zlab), cex = cex)) / 2
rx <- expand_range(c(0, 1), add = dx)
lim <- list(
x = rx,
y = .top / 2 + c(-1, 1) * diff(rx) / 2
)
}
if (n_null == 2) {
stop("You must provide at least two coordinates ranges.", call. = FALSE)
}
if (n_null < 2) {
xlims <- if (is.null(xlim)) 1 - ylim - zlim else xlim
ylims <- if (is.null(ylim)) 1 - xlim - zlim else ylim
zlims <- if (is.null(zlim)) 1 - xlim - ylim else zlim

assert_length(xlims, 3)
assert_length(ylims, 3)
assert_length(zlims, 3)

lim <- coordinates_ternary(abs(xlims), abs(ylims), abs(zlims))
lim$x <- range(lim$x)
lim$y <- range(lim$y)
}
graphics::plot.window(xlim = lim$x, ylim = lim$y, asp = 1)
}
9 changes: 6 additions & 3 deletions inst/examples/ex-plot.R
Original file line number Diff line number Diff line change
Expand Up @@ -15,9 +15,12 @@ ternary_plot(coda, pch = 16, col = "red")
ternary_plot(coda, panel.first = ternary_grid(5, 10))

## Zoom
ternary_plot(coda, xlim = c(0.5, 1), panel.first = ternary_grid())
ternary_plot(coda, ylim = c(0.5, 1), panel.first = ternary_grid())
ternary_plot(coda, zlim = c(0.5, 1), panel.first = ternary_grid())
ternary_plot(coda, ylim = c(0, 0.4, 0), zlim = c(0, 0, 0.4),
panel.first = ternary_grid())
ternary_plot(coda, xlim = c(0, 0.4, 0), zlim = c(0, 0, 0.4),
panel.first = ternary_grid())
ternary_plot(coda, xlim = c(0.4, 0, 0), ylim = c(0, 0.4, 0),
panel.first = ternary_grid())

## Color according to a supplementary variable
## Data from Aitchison 1986
Expand Down
78 changes: 36 additions & 42 deletions inst/tinytest/_tinysnapshot/zoom_x.svg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading

0 comments on commit d331c88

Please sign in to comment.