How to place text along an abline in a log-scale plot?

Viewed 38
    getasp=function() {
        h = par('pin')[2]/diff(par('usr')[3:4])
        w = par('pin')[1]/diff(par('usr')[1:2])
        return (h/w)
    }
plot(0:9, 1:10, type='n', log='y')
asp=getasp()
a=0; b=log(10)/10
abline(a=a, b=b)
x_abline = mean(par('usr')[1:2])
y_abline = a+b*x_abline
points(x_abline, y_abline)
text(x=x_abline, y=y_abline, sprintf('abline(a=%g, b=%g)', a, b), srt=atan(b*asp)/pi*180, pos=3)

The text is rather away from the line. I am not sure why it is off. Could anybody show me what computation is wrong?

enter image description here

2 Answers
y = 10^(mean(par('usr')[3:4]))
x = (log10(y) - a)/b
text(x, y, sprintf('abline(a=%g, b=%g)', a, b), srt=atan(b*asp)/pi*180, pos=3)

enter image description here

Is this you are looking for?

plot(0:9, 1:10, type='n', log='y')
asp=getasp()
a=0; b=log(10)/10
abline(a=a, b=b)
x_abline = mean(par('usr')[1:2])
y_abline = a+b*x_abline
points(x_abline, y_abline)
text(x=2, y=3, sprintf('abline(a=%g, b=%g)', a, b), srt=atan(b*asp)/pi*180, pos=3)

enter image description here

Related