Diff for "FAQ/reglineR" - CBU statistics Wiki
location: Diff for "FAQ/reglineR"
Differences between revisions 14 and 15
Revision 14 as of 2012-02-16 11:58:07
Size: 2745
Editor: PeterWatson
Comment:
Revision 15 as of 2012-02-16 12:04:16
Size: 2802
Editor: PeterWatson
Comment:
Deletions are marked like this. Additions are marked like this.
Line 9: Line 9:
This first set of code puts the legend in the picture like [attachment:wcst.ppt this.] This first set of code puts the legend in the plotting area like [attachment:wcst.ppt this.]
Line 22: Line 22:
The code below will put the legend outside (and to the right of) the plot like [attachment:WCSTr.ppt this]. the graph is slightly smaller to accommodate the extra space needed for the legend. The code below will put the legend outside (and to the right of) the plot like [attachment:WCSTr.ppt this]. This graph is slightly smaller than the one with the legend in the plotting area to accommodate the extra space needed for the legend.

Putting user defined regression lines on scatterplots in R

One example where you may wish to insert two or more regression lines on a scatterplot is when you have groups whose line of best fit you wish to plot assuming they have the same slope (ie as assumed in an ANCOVA).

This is also covered in the regression graduate statistics talk and demos. The code below extends the R syntax described in the graduate talk by specifying the minimum and maximum values on the axes using xlim and ylim options in the plot command and in placement of the legend outside of the plot. R is particularly good at legends for describing regression lines for different groups because it produces a legend which succinctly describes both marker and line information to denote the different groups which is important for displaying graphical results in monochrome.

The code below takes a SPSS data file (R PLOTS FORMAT2.sav) containing four columns denoting the Raven score (predictors Raven1 and Raven2) and WCST values (responses WCST1 and WCST2) for each of two groups (patients (1) and controls (2)) and plots the two regression lines (one for each group) which, as assumed in ANCOVA, have the same slope but different intercepts. The values of these coefficients can be obtained as regression coefficients from a linear regression using group and Raven to predict WCST and then entered into the plot using the 'abline' function.

This first set of code puts the legend in the plotting area like [attachment:wcst.ppt this.]

library(foreign)
x <- read.spss("C:\\Documents and Settings\\peterw\\Desktop\\My Documents\\My Documents2\\JOHN D ANCOVA FOR WIKI\\JD PLOTS 25-1-12\\R PLOTS FORMAT2.sav")
attach(x)
plot(Raven1,WCST1,xlim=c(10,40),ylim=c(1,6),pch=1,,xlab="RCPM(Total Score)",ylab="WCST (Number of categories achieved)")
points(Raven2,WCST2,pch=4)
abline(a=-0.455,b=0.183,lty=2)
abline(a=-0.181,b=0.183,lty=1)
legend(x=28,y=2,c("Controls","Patients"), pch=c(4,1),lty=c(1,2))

The code below will put the legend outside (and to the right of) the plot like [attachment:WCSTr.ppt this]. This graph is slightly smaller than the one with the legend in the plotting area to accommodate the extra space needed for the legend.

library(foreign)
x <- read.spss("C:\\Documents and Settings\\peterw\\Desktop\\My Documents\\My Documents2\\JOHN D ANCOVA FOR WIKI\\JD PLOTS 25-1-12\\R PLOTS FORMAT2.sav")
attach(x)
par(xpd=TRUE, mar=par()$mar+c(0,0,0,4))
plot(Raven1,WCST1,xlim=c(10,40),ylim=c(1,6),pch=1,xlab="RCPM(Total Score)",ylab="WCST (Number of categories achieved)")
points(Raven2,WCST2,pch=4)
legend(x=41,y=5,c("Controls","Patients"), pch=c(4,1),lty=c(1,2),bty='n') 
par(xpd=FALSE)
abline(a=-0.455,b=0.183,lty=2)
abline(a=-0.181,b=0.183,lty=1)

None: FAQ/reglineR (last edited 2013-12-04 16:14:40 by PeterWatson)