Chadwick Robbert Chadwick Robbert - 1 year ago 99
R Question

`abline` does not add line when producing regression diagonstic plots with `par()`

I am using

function to draw a multi-panel plot and I want to add a line to exactly second plot...

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
and want to keep it simple.

Answer Source

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))
plot(model$residuals ~ model$fitted.values)
plot((model$residuals+model$fitted.values) ~ model$fitted.values)

Adding elements immediately after the plot works fine:


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.


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,

enter image description here

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).

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download