[R] plotting a simple graph

Jonathan Williams jonathan.williams at pharmacology.oxford.ac.uk
Mon May 19 17:54:32 CEST 2003


I am having great difficulty plotting what should be a simple graph.
I have measured 1 'y' and 5 'x' variables in each of two groups.
Linear regression shows significant differences in the slopes of the
regression for each 'x' variable between the two groups.

All that I want to do is to plot one graph that shows the scatterplot
for the three groups (each group represented by a different symbol),
overlaid by the fitted regression lines for each group's relation
between the 'x1' and 'y' variables, covarying the remaining four 'x'
variables and adjusting them to their mean values.

#All five of the grp*x interactions are significant in:
fit1=lm(log(y)~grp*(log(x1)+log(x2)+log(x3)+log(x4)+log(x5))
summary(fit1)

#However, to avoid sending all of the data, I create dummy data-sets
#that have distributions resembling the original variables (which are
#very skewed, hence the log-transformations).

grp<-c(rep(0,150),rep(1,200))
y<-(rnorm(n=350,mean=8,sd=5)^1.15)+5
x1<-((rnorm(n=350,mean=10,sd=5))+1)
for (i in 1:350) {if (x1[i]<1 | x1[i]>20) x1[i]<-18}
x2<-(rnorm(n=350,mean=20,sd=5)^2)
x3<-(rnorm(n=350,mean=10,sd=3)^1.5)+50
x4<-rnorm(n=350,mean=45,sd=2.5)
x5<-rnorm(n=350,mean=2.4,sd=0.15)

#So, what I want to do is to plot the regression lines of log(y) on
#log(x1) for the three different groups, covarying grp, log(x2)...
#log(x6)x6 and grp:log(x2)....grp:log(x6). This what I tried:-

plot(log(x1)[grp==0],log(y)[grp==0],
main='Differential relations between log(y) and log(x1) in 3 groups,
adjusted for x2-x5',
xlab='log(x1)', ylab='log(y)')
newdat=data.frame(x1=seq(min(x1),max(x1),(max(x1)-min(x1))/10))
points(log(x1)[grp==1],log(y)[grp==1],pch=3)
lines(predict(lm(log(y)~log(x2)+log(x3)+log(x4)+log(x5),subset=grp==0),newda
t),lty=1)
lines(predict(lm(log(y)~log(x2)+log(x3)+log(x4)+log(x5),subset=grp==1),newda
t),lty='dashed')

This plots regression lines which have corners (which I think must be
wrong!) and which
bear no resemblance to the real regression lines (the lines are not very
close to the points
for the original data set).

#if I specify the means of x variables x2 to x5, as follows:-
newdat=data.frame(x1=seq(min(x1),max(x1),(max(x1)-min(x1))/10),x2=mean(x2),x
3=mean(x3),x4=mean(x4),x5=mean(x5))
#then this gives 2 parallel horizontal regression lines, which is also
incorrect

Please could someone enlighten me on how to plot the regression lines?
Thanks,

Jonathan Williams
OPTIMA
Radcliffe Infirmary
Woodstock Road
OXFORD OX2 6HE
Tel +1865 (2)24356




More information about the R-help mailing list