我想知道是否有一种简单的方法可以使用基本图形系统将具有对比背景的文本标签添加到R图中.到目前为止,我一直使用该rect()功能graphics::strheight()并graphics::strwidth()单独创建背景框,然后我使用text()以下文本放置文本:
# 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
rect(xleft = posCoordsVec[1] - textWidth/2 - pad,
ybottom = posCoordsVec[2] - textHeight/2 - pad,
xright = posCoordsVec[1] …Run Code Online (Sandbox Code Playgroud)