小编iko*_*kop的帖子

文本标签与背景颜色在R中

我想知道是否有一种简单的方法可以使用基本图形系统将具有对比背景的文本标签添加到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)

graphics text r

7
推荐指数
4
解决办法
4932
查看次数

标签 统计

graphics ×1

r ×1

text ×1