Chadwick Robbert - 1 month ago 16

R Question

I am using

`par()`

`par(mfrow = c(2, 2))`

hist(model$residuals) # model is some predefined lm object

plot((model$residuals + model$fitted.values) ~ model$fitted.values)

# Now I want to add a line (or points or curve) to only above plot like

abline(model$coef) # but this doesn't work

qqnorm(model$residuals) # some more plots, doesn't matter which

Any help? I do not intend to use

`ggplot()`

Answer

As @ZheyuanLi says, it's hard to see exactly what you want. Some of your problems appear to be from adding lines that don't overlap with the existing plot limits.

```
model <- lm(Illiteracy~Income,data.frame(state.x77))
par(mfrow = c(2, 2))
hist(model$residuals)
plot(model$residuals ~ model$fitted.values)
plot((model$residuals+model$fitted.values) ~ model$fitted.values)
```

Adding elements immediately after the plot works fine:

```
abline(a=0,b=1)
```

What if you want to go back and add elements to a previous frame? That's a bit difficult. Reset plot to row 1, column 2: this does **not** put us inside the plotting frame of the previous plot, it just gets us ready to plot in this subframe.

```
par(mfg=c(1,2))
```

We want to set up the same plot frame again: we'll cheat by plotting the same thing again (ensuring the same axis limits, etc. etc.), but turning off all aspects of the plot (`new=FALSE`

means we don't blank out the previous plot):

```
plot(model$residuals ~ model$fitted.values,
type="n",new=FALSE,axes=FALSE,ann=FALSE)
abline(h=0,col=2)
```

Base graphics are really not designed for modifying existing plots; if you want to do much of it, you should look into the `grid`

graphics system (which `lattice`

and `ggplot2`

graphics are built on).