Search code examples
rplotidentify

Troubleshooting labeling points on a residuals vs. fitted plot


In R, I have a residuals vs. fitted plot and am trying to label the points. Specifically there are two points at the extreme end of the plot's x-axis that I want to identify. I've looked at hat values and Cook's distances; combined, these flag four different points, but that doesn't narrow down which two of the four (if any) are the ones I'm focused on in my residuals plot.

General format of my code:

mod1 <- lm(y.variable ~ x.variable, data = dataframe)
summary(mod1)
plot(fitted(mod1), residuals(mod1))
abline(h = 0)

I've tried:

identify(fitted(mod1), residuals(mod1), labels=name, plot=TRUE)

to which I get:

'warning: no point within 0.25 inches' 

even when I'm definitely clicking within the point. Also when I hit escape, I get integer(0).


Solution

  • You can plot the labels directly:

    plot(fitted(mod1), residuals(mod1), type = "n")
    text(fitted(mod1), residuals(mod1), labels = names)
    

    assuming names is an appropriate vector of point labels (can be 1:length(fitted(mod1)).