Several plots with high-level plotting functions, especially plot.rqs ()
I am trying to plot two regression summaries side by side with one central title. Each regression summary is generated plot.rqs()
and a set of 9 graphs.
I already tried to use par(mfrow=c(1,2))
, but as I learned from the book by Paul Murrell (2006), high level functions like plot.rqs()
or pairs()
save the graphics state before drawing and then restore the graphics state once completed, so preemptive calls par()
or layout()
can't help me ... plot.rqs()
also does not have a "panel" function.
It seems that the only way to achieve the result is to change the function plot.rqs()
to get a new function, say modified.plot.rqs()
, and then run
par(mfrow=c(1,2)) modified.plot.rqs(summary(fit1)) modified.plot.rqs(summary(fit2)) par(mfrow=c(1,1))
From there, I can work out how to add a generic title to the image using layout()
. Does anyone know how to create a function modified.plot.rqs()
that can be used this way?
thank
source to share
You can fix the function like this: use dput
and capture.output
to extract the function code as a string; change it as you want (here I am just replacing each occurrence par
with a function that does nothing); finally, evaluate the result to create a new function.
library(quantreg)
a <- capture.output(dput(plot.summary.rqs))
b <- gsub("^\\s*par\\(", "nop(", a)
nop <- function(...) {}
my.plot.summary.rqs <- eval(parse(text=b))
source to share
First, we create an example object fm
. We'll then copy plot.rqs
and use trace
in copy to paste par <- list
at the beginning, effectively overriding any use par
inside the function. Then we do the same with plot.summary.rqs
. Finally, we'll check this with our par
:
library(quantreg)
example(plot.rqs) # fm to use in example
# plot.rqs
plot.rqs <- quantreg::plot.rqs
trace("plot.rqs", quote(par <- list), print = FALSE)
# plot.summary.rqs
plot.summary.rqs <- quantreg::plot.summary.rqs
trace("plot.summary.rqs", quote(par <- list), print = FALSE)
# test it out
op <- par(mfrow = c(2, 2))
plot(summary(fm))
plot(fm)
title("My Plots", outer = TRUE, line = -1)
par(op)
EDIT: Added plot.summary.rqs
.
source to share