Text labels with background colour in R

后端 未结 4 1453
慢半拍i
慢半拍i 2021-02-20 18:19

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

4条回答
  •  醉梦人生
    2021-02-20 18:35

    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)
    

提交回复
热议问题