Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

R fitting text on abline

Tags:

plot

r

I want to place a text string on an fx. abline(0,0.2)

But data is changing in scale (fx. 0,01-1 and 5-2000) in my for loop, so how to control the position of the text so it always sits nicely on the abline in the plot?

(without the loop):

#example data row 1
x1<-1:10 
y1<-10:1

plot(x1,y1)
abline(0,0.2)
text(?,?,"text",srt=0.2)
like image 207
user3178323 Avatar asked Oct 20 '22 09:10

user3178323


1 Answers

Here's mine, it should scale to the slope and scale of the graph:

x1<-1:10 
y1<-10:1

a<-0
b<-0.2
plot(x1,y1)
abline(a,b)
text(mean(x1),(b*max(x1)/2+a),"text",srt=0.2,pos=3)

The argument pos=3 denotes that the text should be offset above the point you specify, and the x and y arguments in plot are just the middle x point on the line.

Edit: Setting srt=b*45 will rotate your text along the line, but mamy be fidgety for smaller scaled plots.

like image 59
Csislander Avatar answered Nov 03 '22 06:11

Csislander