Skip to content

Commit

Permalink
Draw only labels in the plotting region
Browse files Browse the repository at this point in the history
  • Loading branch information
nfrerebeau committed Oct 21, 2024
1 parent b9bf18a commit 3157522
Show file tree
Hide file tree
Showing 5 changed files with 23 additions and 22 deletions.
3 changes: 3 additions & 0 deletions NEWS.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,9 @@
## New classes and methods
* Add `export()` to create a Zip archive of all results in CSV format.

## Internals
* Compute the position and draw only the labels in the plotting region.

# dimensio 0.9.0
## New classes and methods
* Add `pcoa()` to compute principal coordinates analysis.
Expand Down
9 changes: 5 additions & 4 deletions R/annotations.R
Original file line number Diff line number Diff line change
Expand Up @@ -38,12 +38,13 @@ label <- function(x, y = NULL, labels = seq_along(x$x),
labs <- compute_labels(x = x$x, y = x$y, labels = labels)

## Draw labels
switch(
fun <- switch(
type,
text = graphics::text(labs, labels = labels, ...),
shadow = text_shadow(labs, labels = labels, ...),
box = text_box(labs, labels = labels, ...)
text = graphics::text,
shadow = text_shadow,
box = text_box
)
fun(labs, labels = labels, ...)

invisible(labs)
}
Expand Down
20 changes: 2 additions & 18 deletions R/biplot.R
Original file line number Diff line number Diff line change
Expand Up @@ -212,26 +212,10 @@ viz_biplot <- function(coord_row, coord_col, ..., rows = TRUE, columns = TRUE,
if (!is.null(labels)) {
labels <- match.arg(labels, several.ok = TRUE)
if (any(labels == "rows") | any(labels == "individuals")) {
label(
x = coord_row$x,
y = coord_row$y,
labels = coord_row$label,
type = "shadow",
cex = coord_row$cex,
col = coord_row$col,
xpd = TRUE
)
viz_labels(coord_row, filter = NULL)
}
if (any(labels == "columns") | any(labels == "variables")) {
label(
x = coord_col$x,
y = coord_col$y,
labels = coord_col$label,
type = "shadow",
cex = coord_col$cex,
col = coord_col$col,
xpd = TRUE
)
viz_labels(coord_col, filter = NULL)
}
}

Expand Down
10 changes: 10 additions & 0 deletions R/viz_coordinates.R
Original file line number Diff line number Diff line change
Expand Up @@ -364,6 +364,8 @@ viz_points <- function(x, margin, axes, ...,
#' It must be one of "`text`", "`shadow`" or "`box`". Any unambiguous substring
#' can be given.
#' @param ... Currently not used.
#' @details
#' Only labels in the plotting region (given by `par("usr")`) will be drawn.
#' @author N. Frerebeau
#' @keywords internal
viz_labels <- function(x, filter = "contribution", n = 10,
Expand All @@ -376,6 +378,14 @@ viz_labels <- function(x, filter = "contribution", n = 10,
x <- x[k, , drop = FALSE] # Subset
}

## Filter
xlim <- graphics::par("usr")[c(1, 2)]
ylim <- graphics::par("usr")[c(3, 4)]
x_filter <- x$x >= min(xlim) & x$x <= max(xlim)
y_filter <- x$y >= min(ylim) & x$y <= max(ylim)
xy_filter <- which(x_filter & y_filter)
x <- x[xy_filter, , drop = FALSE]

label(
x = x$x,
y = x$y,
Expand Down
3 changes: 3 additions & 0 deletions man/viz_labels.Rd

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

0 comments on commit 3157522

Please sign in to comment.