[R] Rotated text on a regression line

Thomas Adams Thomas.Adams at noaa.gov
Wed May 28 14:26:50 CEST 2008


Christoph,

I see two problems:

(1) use plot(x,y,pch=16,xlim=c(0,10),asp=1), as the default has the x/y scales different.

(2) It looks to me that the expression "srt=180/pi*atan(slope)" should be "srt=180*atan(slope)/pi"

Regards,
Tom


Dr. Christoph Scherber wrote:
> Dear all,
>
> I stumbled over a problem recently when trying to use srt with text() on a
> windows device.
>
> What I intended to do was to plot a simple regression line, and to rotate
> a piece of text such that the text has the same angle as the regression
> line.
>
> However, the text is always plotted in a slightly wrong angle:
>
> ####
>
> x=1:10  #create arbitrary x and y values
> y=x*2-rnorm(1:10)
>
> plot(x,y,pch=16,xlim=c(0,10))  #create the graph
> abline(lm(y~x))
>
> #calculate the y coordinate of the text:
> yval=predict(lm(y~x),list(x=rep(2,length(x))))[1]
>
> #calculate the slope:
> slope=as.numeric(lm(y~x)[[1]][2])
>
> text(2,yval,"Regression",srt=180/pi*atan(slope),adj=0)
>
> ####
>
> What am I doing wrong here?
>
> Many thanks in advance for any help!
>
> Best wishes
> Christoph
>
> (using R 2.6.1 on Windows XP)
>
> ______________________________________________
> R-help at r-project.org mailing list
> https://stat.ethz.ch/mailman/listinfo/r-help
> PLEASE do read the posting guide http://www.R-project.org/posting-guide.html
> and provide commented, minimal, self-contained, reproducible code.
>   


-- 
Thomas E Adams
National Weather Service
Ohio River Forecast Center
1901 South State Route 134
Wilmington, OH 45177

EMAIL:	thomas.adams at noaa.gov

VOICE:	937-383-0528
FAX:	937-383-0033



More information about the R-help mailing list