Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Multiple plots with high-level plotting functions, especially plot.rqs()

Tags:

plot

r

I am trying to plot two regression summaries side-by-side with one centered title. Each regression summary is generated by plot.rqs() and amounts to a set of 9 plots.

I've tried using par(mfrow=c(1,2)) already, but as I learnt from Paul Murrel's (2006) book, high-level functions like plot.rqs() or pairs() save the graphics state before drawing and then restore the graphics state once completed, so that pre-emptive calls to par() or layout() can't help me. plot.rqs() doesn't have a 'panel' function either.

It seems that the only way to achieve the result is to modify the plot.rqs() function 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 might be able to work out how to add an overall title to the image using layout(). Does anyone know how to create a modified.plot.rqs() function that could be used in this way?

Thanks

like image 211
JS1204 Avatar asked Jun 12 '26 22:06

JS1204


2 Answers

You can patch a function as follows: use dput and capture.output to retrieve the code of the function, as a string; change it as you want (here, I just replace each occurrence of par with a function that does nothing); finally evaluate the result to produce 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))
like image 197
Vincent Zoonekynd Avatar answered Jun 15 '26 14:06

Vincent Zoonekynd


First we generate an example object, fm . Then we copy plot.rqs and use trace on the copy to insert par <- list at top effectively nullifying any use of par within the function. Then we do the same with plot.summary.rqs. Finally we test it out with our own 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.

like image 27
G. Grothendieck Avatar answered Jun 15 '26 14:06

G. Grothendieck



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!