How to fix text to follow an object built in R

I was wondering, how can I fix a piece text()that always appears above the bracket (or points()etc.) in a dynamically changing plot? In other words , how to define “x” and “y” for this piece of text following the parenthesis (or points()etc.) anywhere in the section? ( see my R code below )

As an example, suppose I have a bracket in the chart below, whose position (xs and ys) will always be known no matter how the plot changes. Here, how can I fix the position text()above this bracket (as shown in the graph) no matter where the bracket is?

PS At first I thought that I could take the "y" brackets, and then make the "y" from the text()following: ( "y" brackets +.1 * "y" brackets ). But, given that the graph can change dynamically (that is, the graph is associated with a function), the +.1 * "y" brackets in any plot give a different position for the text, which does not guarantee that the distance between the text and the bracket will always be maintained .

enter image description here

Here is the R code snippet:

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.
+4
1
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
+1

Source: https://habr.com/ru/post/1673465/


All Articles