2

Assume this easy example:

> numberofdrugs <- rpois(84, 5)
> healthvalue <- rpois(84, 75)
> test <-glm(healthvalue ~ numberofdrugs, family=poisson)
> plot(test, which=5)

Does anybody know, how to get rid of the red dashed line legend which says cook's distance?

Gavin Simpson
  • 170,508
  • 25
  • 396
  • 453
user734124
  • 489
  • 8
  • 20
  • You can't; the presence of the legend is hard coded in the `plot()` method. @mropa's suggestion of creating the plot yourself is the way to go. – Gavin Simpson Jun 05 '11 at 15:19
  • 1
    you can also type `plot.lm` into your R console without the `()` to take a look at the internals of the function. I'd use this as the basis for your own custom cooks distance plot function. – Chase Jun 06 '11 at 00:45
  • Thanks I've looked up the code, but I can't find the area where it's coded. – user734124 Jun 06 '11 at 08:30
  • @Gavin Simpson But you can avoid entering the branch with the legend using a zero-length `cook.levels` parameter. See my answer below. – James Jun 07 '11 at 11:41
  • @James that supposes that the OP wants to get rid of the dashed line representing the cook's distance contours. The OP only mentions removing the legend. Now this might be because there are no cooks distance contours on the plot they are looking at, in which case your suggestion works fine. If there are contours and the OP wants them displayed but prefers to deal with conveying their meaning in the figure legend say, then your suggestion isn't going to work. – Gavin Simpson Jun 07 '11 at 11:52
  • @Gavin Simpson True, I assumed the OP didn't want the legend because he also didn't want or have any lines. In that case, one could follow Chase's suggestion to create a modified `plot.lm`, commenting out or removing lines 247-248. – James Jun 07 '11 at 13:55
  • @Gavin Simpson @user734124 See the edit to my answer to see how to quickly create a new plot function from `plot.lm` with the legend removed. – James Jun 07 '11 at 14:17

2 Answers2

2

I don't know if you can get rid of that line but you can simply create that plot manually. Afterwards you can add all the legends you want or don't want. E.g. take a look at the boot package.

In your case, this will do your plot

plot(y = glm.diag(test)$rp, x = glm.diag(test)$h)

As far as i know there is a glm.diag.plots function as well. Take a look at their help pages.

mropa
  • 11,562
  • 10
  • 33
  • 29
0

There is a cook.levels parameter that controls how many and for what values the dotted lines are produced. Its length also used in the if statment that leads to the branch which produces the legend. Therefore if you set the value to NULL you get rid of the dotted lines and the legend. You may also want to remove the solid red line by setting add.smooth=FALSE.

plot(test, which=5, cook.levels=NULL, add.smooth=FALSE)

Edit

After suggestions in the question comment that only the legend is to be removed, editing a copy of plot.lm seems to be the way to go. This question gives an effective, though slightly esoteric, way of doing so using body:

plot.lm2 <- plot.lm
body(plot.lm2)[[27]][[3]][[9]][[4]][[8]][[3]][[6]]<-NULL
plot.lm2(test, which=5)
Community
  • 1
  • 1
James
  • 65,548
  • 14
  • 155
  • 193