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)
I think this comment for ?par, meant for both crt and srt, applies: crt A numerical value specifying (in degrees) how single characters should be rotated. It is unwise to expect values other than multiples of 90 to work. Compare with srt which does string rotation. So I would say that even getting close to the right angle is not bad. I would think that perhaps the graphics devices can only write in particular directions, and that's the one closest to your graph. Actually, further playing with it, I think all you are missing is that this angle is not dependent on the aspect ratio, it's an absolute angle. Try to resize the window, and see how the direction of the string does not change at all, even though the line slope does change. Hence the advice about only relying on 0 and 90 degrees. Haris Skiadas Department of Mathematics and Computer Science Hanover College On May 27, 2008, at 5:25 PM, 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) >
Note that the scale of x-axis and y-axis is different in your plot. One simple way to avoid this is to keep the data unit in the x direction is equal that in the y direction, by setting asp=1 in calling plot function. X Dr. Christoph Scherber ??:> 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. > >
Thanks to all for the postings so far! I found that setting asp=0.5 and then dividing the slope by 2 seems to do the trick: ## x=1:10 y=x*2-rnorm(1:10) plot(x,y,pch=16,asp=0.5) abline(lm(y~x)) yval=predict(lm(y~x),list(x=rep(2,length(x))))[1] slope=as.numeric(lm(y~x)[[1]][2]) text(2,yval,"Regression",srt=180/pi*atan(slope/2),adj=0) ## This might be a way of controlling for the aspect ratio witho8ut producing "ugly" figures.
On Tue, 27 May 2008, Xiaohui Chen wrote:> Note that the scale of x-axis and y-axis is different in your plot. One > simple way to avoid this is to keep the data unit in the x direction is equal > that in the y direction, by setting asp=1 in calling plot function.Or when that is not possible or not desirable translate the slope to the device scales: usr2dev <- function(x) 180/pi*atan(x * diff( par("usr")[1:2])/ diff(par("usr")[3:4])) text(2,yval,"Regression", srt=usr2dev( slope ), adj=0) HTH, Chuck> > X > > Dr. Christoph Scherber ??????: >> 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. >> >> > > ______________________________________________ > 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. > >Charles C. Berry (858) 534-2098 Dept of Family/Preventive Medicine E mailto:cberry at tajo.ucsd.edu UC San Diego http://famprevmed.ucsd.edu/faculty/cberry/ La Jolla, San Diego 92093-0901
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