I was wondering if there is a simple way to add text labels with a contrasting background to an R plot using the base graphic system. Until now I have always used the rect
Bless your hardworking hearts, but plotrix
has boxed.labels()
:
# Prepare a noisy background:
plot(x = runif(1000), y = runif(1000), type = "p", pch = 16, col = "#40404050")
## Parameters for my text:
myText <- "some Text"
posCoordsVec <- c(0.5, 0.5)
cex <- 2
## Background rectangle:
textHeight <- graphics::strheight(myText, cex = cex)
textWidth <- graphics::strwidth(myText, cex = cex)
pad <- textHeight*0.3
## Place text:
plotrix::boxed.labels(posCoordsVec[1], posCoordsVec[2], myText, cex = cex,
border = NA, bg ="lightblue", xpad = 1.4, ypad = 1.4)