[R] Setting variable main in plot-function

Marc Schwartz MSchwartz at MedAnalytics.com
Fri Mar 11 02:55:51 CET 2005


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":

plot(resid(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:

plot(resid(z), 
     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




More information about the R-help mailing list