Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

Set default options for matplot/plot

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,

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

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.

>Solution :

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,
           ...)
}
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading