[R] plotting a simple graph

Thomas W Blackwell tblackw at umich.edu
Mon May 19 21:03:05 CEST 2003

Jonathan  -

After  plot(...), points(...), newdat <- data.frame(...)
exactly as below, try

   lines(log(newdat$x1), predict(...), lty=1) .

and the same with  grp==1.  What I've done above is to pass the
x-coordinate explicitly to  lines()  since it is not present in
the return value from  predict().  Here's the logic:

Perhaps one difficulty is that the return value from  predict.lm()
is "a vector of predictions, or a matrix of predictions and bounds
... if 'interval' is set."  Quoting here from  help("predict.lm").

Thus,  lines(predict(...), lty=1)  never sees the contents of the
data frame  newdat  and so  lines()  does not know what x-coordinate
to use when plotting each entry of the vector returned by predict().
In this case,  lines()  uses its time-series methods and plots the
35 y-values given against the integers 1:35.  Is that what you see
on the screen ?

HTH  -  tom blackwell  -  u michigan medical school  -  ann arbor  -

On Mon, 19 May 2003, Jonathan Williams wrote:

> 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
> Radcliffe Infirmary
> Woodstock Road
> Tel +1865 (2)24356
> ______________________________________________
> R-help at stat.math.ethz.ch mailing list
> https://www.stat.math.ethz.ch/mailman/listinfo/r-help

More information about the R-help mailing list