3

I was wondering if there might be a way to set the srt (text rotation angle) argument of text() in R, such that the text generated by text() always has the same angle as the angle of the regression line in the plot (see picture below)?

Here is my R code:

plot.cor <- function(n){
x = 1:n
y = rnorm(n, 9)

plot(y~x, col = 4) 
abline(lm(y~x), col = 2)
text(mean(x), mean(y), paste0("r = ", round(cor(x,y), 3)), srt = 15, pos = 3, font = 2)
}
# Example of use:
plot.cor(30)  
plot.cor(30)
plot.cor(30)

enter image description here

rnorouzian
  • 7,397
  • 5
  • 27
  • 72

0 Answers0