From 1cd62d7ed381af78730dadcea9aa95ece480125a Mon Sep 17 00:00:00 2001 From: nfrerebeau Date: Mon, 21 Oct 2024 17:57:02 +0200 Subject: [PATCH] Draw only labels in the plotting region --- NEWS.md | 3 +++ R/annotations.R | 13 +++++++++++++ man/label.Rd | 3 +++ 3 files changed, 19 insertions(+) diff --git a/NEWS.md b/NEWS.md index 78a009a..d77befb 100644 --- a/NEWS.md +++ b/NEWS.md @@ -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. diff --git a/R/annotations.R b/R/annotations.R index 9f3e94d..741d302 100644 --- a/R/annotations.R +++ b/R/annotations.R @@ -17,6 +17,8 @@ #' @return #' `label()` is called it for its side-effects: it results in a graphic #' being displayed. +#' @details +#' Only labels in the plotting region (given by `par("usr")`) will be drawn. #' @seealso [graphics::text()] #' @note #' This function is modeled after [car::pointLabel()] (originally from the @@ -34,6 +36,17 @@ label <- function(x, y = NULL, labels = seq_along(x$x), labels <- grDevices::as.graphicsAnnot(labels) if (length(labels) < length(x$x)) labels <- rep(labels, length(x$x)) + ## 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 <- x$x[xy_filter] + x$y <- x$y[xy_filter] + labels <- labels[xy_filter] + ## Compute label positions labs <- compute_labels(x = x$x, y = x$y, labels = labels) diff --git a/man/label.Rd b/man/label.Rd index 612d2fb..15cac86 100644 --- a/man/label.Rd +++ b/man/label.Rd @@ -34,6 +34,9 @@ being displayed. \description{ Optimize the location of text labels to minimize overplotting text. } +\details{ +Only labels in the plotting region (given by \code{par("usr")}) will be drawn. +} \note{ This function is modeled after \code{\link[car:pointLabel]{car::pointLabel()}} (originally from the \pkg{maptools} package).