2

I'm following up on this answer. I'm wondering if there is a way to set the default for argument rug to FALSE and argument multiline to TRUE in the plots generated by library(effects) as, for example, shown below in the code?

library(effects)
m <- lm(Fertility ~ Examination*Education, data = swiss)
plot(allEffects(m), rug = FALSE, multiline = TRUE)   # By default, change `rug = FALSE`
                                                     # `multiline = TRUE `
rnorouzian
  • 7,397
  • 5
  • 27
  • 72

2 Answers2

2

Note: The below suggestions refer to the general case of changing default values in a function.

Yes, messing with the default arguments is possible. One way is to modify the formals of the function, in this case that would be

formals(effects:::plot.eff)$rug <- FALSE
formals(effects:::plot.eff)$multiline <- TRUE

Another possibility is to use the default package like

default::default(effects:::plot.eff) <- list(rug = FALSE, 
                               multiline = TRUE)

To cite the package description,

A simple syntax to change the default values for function arguments, whether they are in packages or defined locally.

For more information on the package, you can look up the CRAN page.

Taufi
  • 1,557
  • 8
  • 14
  • Thanks, but, when I tried your first solution and then did `plot(allEffects(m))`, then I got the following error: `Error in plot(allEffects(m)) : call to standardGeneric("plot") apparently not from the body of that generic function` – rnorouzian Jan 30 '21 at 20:26
  • Did the second solution work? In case the first solution doesn't work you would need to look up how `allEffects()` calls the `plot()` function. Maybe it is necessary to apply the `formals()` to the `allEffects()` or a similar function instead. – Taufi Jan 30 '21 at 20:30
  • I didn't try the second solution, but I specifically am wondering why the first solution is producing this error, apparently you'r not getting this error, right? On the other hand, did you check [this answer](https://stackoverflow.com/a/62946040/7223434) to a related question? Maybe we should change the default in the `formals(effects:::plot.eff)` object. – rnorouzian Jan 30 '21 at 20:35
  • It's bizarre. The `effects:::plot.eff` objekt **does have** a `rug` property but setting it `FALSE` gives an error in `loadNamespace`. Let me think about how to handle that exception. – Taufi Jan 30 '21 at 21:04
  • @Taufi I think the `loadNamespace` error is because the function isn't exported from the namespace. I downloaded the package and exported `plot.efflist` and `plot.eff` and re-installed from this local version. The error stopped, but using either of the methods above (even if you specify them both on `plot.efflist` and `plot.eff`, which is called by the former, doesn't seem to result in the expected plot - it still has rugs and is not a multi-line plot. – DaveArmstrong Jan 30 '21 at 21:54
1

I think if you're just trying to add those two options to @MrFlick's answer you reference, you could do the following:

plot.efflist <- function (x, selection, rows, cols, graphics = TRUE, 
                          lattice, rug = FALSE, multiline = TRUE, ...) 
{
  lattice <- if (missing(lattice)) 
    list()
  else lattice
  if (!missing(selection)) {
    if (is.character(selection)) 
      selection <- gsub(" ", "", selection)
    pp <- plot(x[[selection]], lattice = lattice, rug = rug, multiline=multiline, ...)
    pp$x.scales$tck=c(1,0)
    pp$y.scales$tck=c(1,0)
    return(pp)
  }
  effects <- gsub(":", "*", names(x))
  neffects <- length(x)
  mfrow <- mfrow(neffects)
  if (missing(rows) || missing(cols)) {
    rows <- mfrow[1]
    cols <- mfrow[2]
  }
  for (i in 1:rows) {
    for (j in 1:cols) {
      if ((i - 1) * cols + j > neffects) 
        break
      more <- !((i - 1) * cols + j == neffects)
      lattice[["array"]] <- list(row = i, col = j, 
                                 nrow = rows, ncol = cols, more = more)
      pp <- plot(x[[(i - 1) * cols + j]], lattice = lattice, rug=rug, multiline=multiline,
                 ...)
      # hack to turn off opposite side tick marks
      pp$x.scales$tck=c(1,0)
      pp$y.scales$tck=c(1,0)
      print(pp)
    }
  }
}
environment(plot.efflist) <- asNamespace("effects")

library(effects)
m <- lm(Fertility ~ ., data = swiss)
plot(allEffects(m), rug = FALSE)

enter image description here

DaveArmstrong
  • 18,377
  • 2
  • 13
  • 25