Text labels with background colour in R

后端 未结 4 1568
慢半拍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:38

    Quick hack using altcode character for a box:

    plot(x=runif(1000), y=runif(1000), 
         type="p", pch=16, col="#40404050")
    
    labels <- c("some text", "something else")
    
    boxes <- sapply(nchar(labels), function(n) 
      paste(rep("█", n), collapse=""))
    
    pos <- rbind(c(0.2, .1), c(.5, .5))
    text(pos, labels=boxes, col="#CCCCCC99")
    text(pos, labels=labels)
    

提交回复
热议问题