我想在情节上抖动文本以避免过度绘制。为此,我假设我需要一个围绕文本组件的边界框。有没有办法得到这个?

例如,在基本图形中:

plot.new()
text(.5,.5,"word")
text(.6,.5,"word") #does this overlap?

在网格中有一种删除重叠文本的方法,但我似乎无法找到一种方法来访问确定是否发生重叠的代码。
grid.text(c("word","other word"),c(.5,.6),c(.5,.5),check=T)

最佳答案

也许 strwidthstrheight 函数可以在这里提供帮助

stroverlap <- function(x1,y1,s1, x2,y2,s2) {
  sh1 <- strheight(s1)
  sw1 <- strwidth(s1)
  sh2 <- strheight(s2)
  sw2 <- strwidth(s2)

  overlap <- FALSE
  if (x1<x2)
    overlap <- x1 + sw1 > x2
  else
    overlap <- x2 + sw2 > x1

  if (y1<y2)
    overlap <- overlap && (y1 +sh1>y2)
  else
    overlap <- overlap && (y2+sh2>y1)

  return(overlap)
}
stroverlap(.5,.5,"word", .6,.5, "word")

关于r - 找到绘制文本的边界框,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/6234335/

10-12 17:42