-2
y = x = seq(11)-1
plot(x, y, type='n')
{
  a=0; b=.5
  abline(a=a, b=b)
  x_abline = mean(par('usr')[1:2])
  y_abline = a+b*x_abline
  text(x=x_abline, y=y_abline, sprintf('abline(a=%g, b=%g)', a, b), srt=atan(b)/2/pi*360, pos=3)
}
{
  a=0; b=1
  abline(a=a, b=b)
  x_abline = mean(par('usr')[1:2])
  y_abline = a+b*x_abline
  text(x=x_abline, y=y_abline, sprintf('abline(a=%g, b=%g)', a, b), srt=atan(b)/2/pi*360, pos=3)
}
{
  a=0; b=2
  abline(a=a, b=b)
  x_abline = mean(par('usr')[1:2])
  y_abline = a+b*x_abline
  text(x=x_abline, y=y_abline, sprintf('abline(a=%g, b=%g)', a, b), srt=atan(b)/2/pi*360, pos=3)
}

I am trying to align text along with ablines. But it seems that they are not exactly aligned. I am not sure why it is so. Could anybody show me how to fix the problem?

enter image description here

None of the solutions to How to annotate a reference line at the same angle as the reference line itself? are good.

The following is too verbose.

getCurrentAspect <- function() {
   uy <- diff(grconvertY(1:2,"user","inches"))
   ux <- diff(grconvertX(1:2,"user","inches"))
   uy/ux
}

The others are for ggplot2, which are irrelevant to my question.

The following one uses asp. I don't want to specify asp in my plot manually.

asp <- 2

plot(1:10,1:10, asp=asp) 
abline(a=8, b=-1)
text(x=4, y=5, "reference line label", srt=180/pi*atan(-1*asp))

abline(a=4, b=-2)
text(x=1, y=3, "reference line label", srt=180/pi*atan(-2*asp))
Ben Bolker
  • 211,554
  • 25
  • 370
  • 453
user1424739
  • 11,937
  • 17
  • 63
  • 152
  • 3
    Re "I don't want to specify `asp` in my plot manually." Fine, that's exactly the purpose of [the answer by @Ben Bolker](https://stackoverflow.com/a/11767837/1851712). That their solution "is too verbose" I don't consider a reason to reopen. If you name the function `f`, it's not very verbose when you reuse it ;) – Henrik Aug 15 '21 at 16:37
  • Well, then your question is no longer "How to align text along ablines exactly?", which is _exactly_ what was asked in the question I linked to. Please avoid making your question moving targets. – Henrik Aug 15 '21 at 16:41
  • 2
    Relevant reading: [Chameleon question changed from one duplicate to another](https://meta.stackoverflow.com/questions/274958/chameleon-question-changed-from-one-duplicate-to-another). Good luck and good bye! – Henrik Aug 15 '21 at 16:46

1 Answers1

0

The reason the text rotation doesn't match the slope of the lines is that you haven't accounted for the aspect ratio of the plot.

I'm not sure what the issue is the solution here being "too verbose". The solution below adds three lines of code to compute the aspect ratio (instead of the 5 in the linked solution, which took longer because it defined a function). Each call to text() adds *asp in the srt= argument. The whole thing is now 13 lines long, compared with the 23 lines of the original post ...

y = x = seq(11)-1
plot(x, y, type='n')
uy <- diff(grconvertY(1:2,"user","inches"))
ux <- diff(grconvertX(1:2,"user","inches"))
asp <- uy/ux
f <- function(b, a=0) {
    abline(a=a, b=b)
    x_abline = mean(par('usr')[1:2])
    y_abline = a+b*x_abline
    text(x=x_abline, y=y_abline, sprintf('abline(a=%g, b=%g)', a, b),
         srt=atan(b*asp)/2/pi*360, pos=3)
}
f(b=0.5); f(b=1); f(b=2)
Ben Bolker
  • 211,554
  • 25
  • 370
  • 453