FAQ/reglineR252013-12-04 16:14:40PeterWatson242013-03-08 10:17:12localhostconverted to 1.6 markup232012-10-18 13:05:41PeterWatson222012-07-31 15:50:06PeterWatson212012-02-16 12:38:41PeterWatson202012-02-16 12:37:28PeterWatson192012-02-16 12:36:33PeterWatson182012-02-16 12:29:33PeterWatson172012-02-16 12:28:24PeterWatson162012-02-16 12:24:38PeterWatson152012-02-16 12:04:16PeterWatson142012-02-16 11:58:07PeterWatson132012-02-16 11:57:32PeterWatson122012-02-16 11:56:52PeterWatson112012-02-16 11:49:34PeterWatson102012-02-16 11:48:49PeterWatson92012-02-16 11:45:03PeterWatson82012-02-16 11:43:23PeterWatson72012-02-16 11:40:58PeterWatson62012-02-16 11:40:16PeterWatson52012-02-16 10:40:19PeterWatson42012-02-16 10:39:40PeterWatson32012-02-16 10:39:21PeterWatson22012-02-16 10:39:07PeterWatson12012-02-16 10:37:31PeterWatsonPutting user defined regression lines on scatterplots in ROne 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 this. The jpeg command sends the outputted graph into a portable jpeg file you can send via e-mail. You can also split a vector, Raven, into two, Raven1 and Raven2, using the split function to split the vector into Ravens score for each of the groups whose regression lines you wish to plot. The code for this is below. The Ravens scores will then be put into separate columns (for Patients and Controls) each beginning with rgp and succeeded by the label name (if any) which was used in the SPSS spreadsheet to describe the group codings e.g. rgp$'PD PATIENTS' and rgp$CONTROLS give the Raven data for the patients and controls respectively in this example since 'PD PATIENTS' and CONTROLS were used to label the respective group codings in the SPSS spreadsheet. These data columns then be used in the above syntax to represent predictor scores in each of the two groups for plotting their regression lines assuming equal slopes. The below R syntax plots regression lines with the legend in the plotting area after splitting each of the Ravens and WCST scores into two columns, one for each group. The code below will put the legend outside (and to the right of) the plot like 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. A slight variant of the above examples produces the plot axes minus the points and adds each group on separately afterwards instead of adding the points in the first group on first. The below example also shows you can add more than two groups onto the ANCOVA plot (in this case three). Not the col=n (where n is a positive integer) subcommand can be used with points,legend and abline to create coloured points and lines e.g. col=1 is black, col=2 is red, col=3 is green and col=4 is blue. The below uses raw data input creating a group variable in R to identify the two groups during plotting and saves to a .eps file which hs a higher resolution that .jpeg. R can save graphs in other formats - see here.