diff --git a/R/plot.R b/R/plot.R index 2bcd2a46b..4c1f8b3c6 100644 --- a/R/plot.R +++ b/R/plot.R @@ -158,14 +158,27 @@ setMethod("pairs", signature(x="SpatRaster"), } ) +.textbox <- function(x, y=NULL, labels, col="black", hc="white", hw=0.1, ... ) { + xy <- grDevices::xy.coords(x, y) + hw <- hw[1] + n <- nchar(labels) + x0 <- hw * graphics::strwidth("A") + y0 <- hw * graphics::strheight("A") + x1 <- n * x0 + v <- vect(cbind(xy$x + c(0, x1), xy$y), crs="local") + b <- buffer(v, y0) +} .halo <- function(x, y=NULL, labels, col="black", hc="white", hw=0.1, ... ) { xy <- grDevices::xy.coords(x, y) + hw <- hw[1] xo <- hw * graphics::strwidth("A") yo <- hw * graphics::strheight("A") n <- nchar(labels) - theta <- seq(pi/4, 2*pi, length.out=8*hw*10) + fact <- 100 * max(1, hw*2) + + theta <- seq(pi/4, 2*pi, length.out=hw*fact) for (i in theta) { text( xy$x + cos(i)*xo, xy$y + sin(i)*yo, labels, col=hc, ... ) } @@ -178,7 +191,7 @@ halo <- function(x, y=NULL, labels, col="black", hc="white", hw=0.1, ... ) { setMethod("text", signature(x="SpatRaster"), - function(x, labels, digits=0, halo=FALSE, ...) { + function(x, labels, digits=0, halo=FALSE, hc="white", hw=0.1, ...) { if (missing(labels)) { labels <- 1 } @@ -202,8 +215,8 @@ setMethod("text", signature(x="SpatRaster"), } else if (is.numeric(labels)) { labels <- as.character(round(labels, digits=digits) ) } - if (halo) { - .halo(xy[,1], xy[,2], labels, ...) + if (halo && (is.TRUE(hw > 0))) { + .halo(xy[,1], xy[,2], labels, hc=hc, hw=hw, ...) } else { text(xy[,1], xy[,2], labels, ...) } @@ -212,7 +225,7 @@ setMethod("text", signature(x="SpatRaster"), setMethod("text", signature(x="SpatVector"), - function(x, labels, halo=FALSE, inside=FALSE, ...) { + function(x, labels, halo=FALSE, inside=FALSE, hc="white", hw=0.1, ...) { if (missing(labels)) { labels <- 1:nrow(x) } else if (length(labels) == 1) { @@ -229,8 +242,8 @@ setMethod("text", signature(x="SpatVector"), } } xy <- geom(centroids(x, inside=inside))[,c("x","y"),drop=FALSE] - if (halo) { - .halo(xy[,1], xy[,2], labels, ...) + if (halo && (is.TRUE(hw > 0))) { + .halo(xy[,1], xy[,2], labels, hc=hc, hw=hw, ...) } else { text(xy[,1], xy[,2], labels, ...) } diff --git a/R/plot_raster.R b/R/plot_raster.R index 95c45b525..de667f00e 100644 --- a/R/plot_raster.R +++ b/R/plot_raster.R @@ -605,7 +605,7 @@ prettyNumbs <- function(x, digits) { } if (isTRUE(x$halo.main)) { .halo(x$loc.main[1], x$loc.main[2], x$main, pos=pos, offset=x$line.main, cex=x$cex.main, - font=x$font.main, col=x$col.main, xpd=TRUE) + font=x$font.main, col=x$col.main, xpd=TRUE, hc=x$halo.main.hc, hw=x$halo.main.hw) } else { text(x$loc.main[1], x$loc.main[2], x$main, pos=pos, offset=x$line.main, cex=x$cex.main, font=x$font.main, col=x$col.main, xpd=TRUE) @@ -626,7 +626,7 @@ prettyNumbs <- function(x, digits) { sort=TRUE, decreasing=FALSE, grid=FALSE, las=0, all_levels=FALSE, decimals=NULL, background=NULL, xlab="", ylab="", cex.lab=0.8, line.lab=1.5, asp=NULL, yaxs="i", xaxs="i", main="", cex.main=1.2, line.main=0.5, font.main=graphics::par()$font.main, col.main = graphics::par()$col.main, loc.main=NULL, - halo=FALSE, axes=TRUE, box=TRUE, cex=1, maxcell=500000, buffer=FALSE, clip=TRUE, + halo=FALSE, hc="white", hw=0.1, axes=TRUE, box=TRUE, cex=1, maxcell=500000, buffer=FALSE, clip=TRUE, # for rgb stretch=NULL, scale=NULL, bgalpha=NULL, zlim=NULL, zcol=NULL, ...) { #cex is catch and kill @@ -733,6 +733,9 @@ prettyNumbs <- function(x, digits) { out$reset <- reset out$main <- main out$halo.main <- halo + out$halo.main.hc <- hc + out$halo.main.hw <- hw + out$loc.main <- loc.main out$cex.main <- cex.main out$font.main <- font.main diff --git a/man/text.Rd b/man/text.Rd index f19a2d3e9..4910cb008 100644 --- a/man/text.Rd +++ b/man/text.Rd @@ -13,16 +13,18 @@ Plots labels, that is a textual (rather than color) representation of values, on } \usage{ -\S4method{text}{SpatRaster}(x, labels, digits=0, halo=FALSE, ...) +\S4method{text}{SpatRaster}(x, labels, digits=0, halo=FALSE, hc="white", hw=0.1, ...) -\S4method{text}{SpatVector}(x, labels, halo=FALSE, inside=FALSE, ...) +\S4method{text}{SpatVector}(x, labels, halo=FALSE, inside=FALSE, hc="white", hw=0.1, ...) } \arguments{ \item{x}{SpatRaster or SpatVector} \item{labels}{character. Optional. Vector of labels with \code{length(x)} or a variable name from \code{names(x)}} \item{digits}{integer. How many digits should be used?} - \item{halo}{logical. If \code{TRUE} a "halo" is printed around the text. If \code{TRUE}, additional arguments \code{hc="white"} and \code{hw=0.1} can be modified to set the color and width of the halo} + \item{halo}{logical. If \code{TRUE} a "halo" is printed around the text} + \item{hc}{character. The halo color} + \item{hw}{numeric. The halo width} \item{inside}{logical. Should the text always be placed inside one the sub-geometries?} \item{...}{additional arguments to pass to graphics function \code{\link[graphics]{text}} } }