如何修复文本的位置以跟随在R中绘制的对象

我想知道如何在动态变化的情节中修复一段文本()总是出现在括号(或点()等)之上?换句话说,我应该如何确定该文本的“x”和“y”以跟随图中任何位置的括号(或点()等)? (见下面我的R代码)

举个例子,假设我在下面的图中有一个括号,无论情节如何变化,其位置(xs和ys)都将始终为人所知.在这里,无论括号在哪里,我如何修复此括号上方文本()的位置(如图中所示)?

附:起初,我以为我可以取括号的“y”,然后将text()的“y”设为:(括号的“y”括号.1 *“y”).但鉴于绘图可以动态改变(即绘图连接到函数),任何绘图中括号的.1 *“y”给出了不保证文本与文本之间距离的文本的不同位置.支架始终保持.

《如何修复文本的位置以跟随在R中绘制的对象》

这是一段R代码:

if(!require(library(pBrackets))){install.packages('pBrackets') }

library(pBrackets)

plot(1:10, ty = 'n')

brack <- brackets(x1 = 4, y1 = 6, x2 = 8, y2 = 6, h = 1, ticks = .5, curvature = .5, 
              type=1, col = "blue", 
              lwd = 2, xpd = T)

text(x = 6, y = 7.2, "Equivalent to ZERO", font = 2) ## How to determine "x" and "y"
                                                      # such that the "text" always
                                                      # appears above the bracket 
                                                      # even if the plot changes. This helps making functions.

最佳答案

plot(1:10, ty = 'n')
x1 = 4
y1 = 6
x2 = 8
y2 = 6
h = 1 #Or some variation of h = sqrt( (x2-x1)^2 + (y2-y1)^2 )/4
brack <- brackets(x1 = x1, y1 = y1, x2 = x2, y2 = y2, h = h, ticks = .5, curvature = .5, 
                      type=1, col = "blue", 
                      lwd = 2, xpd = T)

text(x = (x1+x2)/2, y = (y1+h), "Equivalent to ZERO", font = 2, pos = 3)
#pos = 3 means the text will be on top of x and y
点赞