Skip to content

Commit

Permalink
r
Browse files Browse the repository at this point in the history
  • Loading branch information
rhijmans committed Dec 18, 2024
1 parent 3e24f9e commit 8b90979
Show file tree
Hide file tree
Showing 3 changed files with 30 additions and 12 deletions.
27 changes: 20 additions & 7 deletions R/plot.R
Original file line number Diff line number Diff line change
Expand Up @@ -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, ... )
}
Expand All @@ -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
}
Expand All @@ -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, ...)
}
Expand All @@ -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) {
Expand All @@ -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, ...)
}
Expand Down
7 changes: 5 additions & 2 deletions R/plot_raster.R
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand All @@ -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
Expand Down Expand Up @@ -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
Expand Down
8 changes: 5 additions & 3 deletions man/text.Rd
Original file line number Diff line number Diff line change
Expand Up @@ -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}} }
}
Expand Down

0 comments on commit 8b90979

Please sign in to comment.