Home > Software design >  Set default options for matplot/plot
Set default options for matplot/plot

Time:02-13

I would like to set default styles for the plot/matplot functions. For instance, I almost always use

matplot(x, y, type='l', lty='solid', col=rainbow(), lwd=2)

so I would like to set default options of type='l', lty='solid', col=rainbow(), lwd=2 in .Rprofile. Of course, I could just make a new function,

mplot <- function(x, y, type='l', lty='solid', lwd=2, col=rbow(), log='') {
        matplot(x, y, type=type, lty=lty, col=col, log=log, lwd=lwd)
}

but if I want to specify xlim and/or ylim or set xaxt='n' etc. it becomes a lot of work since there are more default options that I want than those I don't want. I also thought something like

plist <- list(type=type, lty=lty, col=col, log=log, lwd=lwd)
matplot(x, y, plist)

should work but it doesn't.

CodePudding user response:

If you do

do.call(matplot, c(list(x, y), plist))

it should work. The other option is to use ...:

mplot <- function(x, y, type='l', lty='solid', lwd=2, col=rbow(), log='', ...) {
        matplot(x, y, type=type, lty=lty, col=col, log=log, lwd=lwd,
           ...)
}
  •  Tags:  
  • r
  • Related