Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
409 views
in Technique[技术] by (71.8m points)

r - finding the bounding box of plotted text

I would like to jitter the text on a plot so as to avoid overplotting. To do so, I assume that I need a bounding box around the text component. Is there a way to get this?

For example, in base graphics:

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

In grid there is a way to drop overlapping text, but I can't seem to find a way to access the code that figures out if overlapping has occurred.

grid.text(c("word","other word"),c(.5,.6),c(.5,.5),check=T)
See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

Maybe the strwidth and strheight functions can help here

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")

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...