Re: [R] Setting variable main in plot-function

From: Marc Schwartz <>
Date: Fri 11 Mar 2005 - 12:55:51 EST

On Fri, 2005-03-11 at 00:50 +0100, T Petersen wrote:
> Hi,
> I am plotting the residuals of z like this
> plot(resid(z))
> and I want the title of the graph to be
> main="Residuals from regression [name of regression]"
> where in this case "z" is the name of the regression. Is there a way to
> automaticall put the name of the regression in the title, e.g if the
> regressions name changes to "y", the title changes accordingly?

I am presuming that by the name of the regression, you mean the model formula.

Using one of the example models from ?lm, where a model is created called "lm.D9":


     main = paste("Residuals from regression", deparse(lm.D9$call)))

In this case, the model function call is:

> deparse(lm.D9$call)

[1] "lm(formula = weight ~ group)"

which gets pasted to your initial text. The initial model function call is stored in the linear model object as ModelName$call.

So in your case use:


     main = paste("Residuals from regression", deparse(z$call)))

Just replace 'z' with the name of each new model object and the title will change or use the same model object name each time you create a new model, whichever makes sense for your application.

One way to figure these things out, if you know that it has been done by some other function, is to review the code for that function. In this case review the code for print.lm or even plot.lm to gain some insight into how R expressions are used in function text or plot title and axis label outputs.

HTH, Marc Schwartz mailing list PLEASE do read the posting guide! Received on Fri Mar 11 13:01:50 2005

This archive was generated by hypermail 2.1.8 : Fri 03 Mar 2006 - 03:30:42 EST