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