I am running quantile regressions for several independent variables separately (same dependent). I want to plot only the slope estimates over several quantiles of each variable in a single plot.
Here's a toy data:
set.seed(1988)
y <- rnorm(50, 5, 3)
x1 <- rnorm(50, 3, 1)
x2 <- rnorm(50, 1, 0.5)
# Running Quantile Regression
require(quantreg)
fit1 <- summary(rq(y~x1, tau=1:9/10), se="boot")
fit2 <- summary(rq(y~x2, tau=1:9/10), se="boot")
I want to plot only the slope estimates over quantiles. Hence, I am giving parm=2
in plot
.
plot(fit1, parm=2)
plot(fit2, parm=2)
Now, I want to combine both these plots in a single page.
What I have tried so far;
- I tried setting
par(mfrow=c(2,2))
and plotting them. But it's producing a blank page. - I have tried using
gridExtra
and gridGraphics without success. Tried to convert base graphs into Grob objects as stated here - Tried using function
layout
function as in this document - I am trying to look into the source code of
plot.rqs
. But I am unable to understand how it's plotting confidence bands (I'm able to plot only the coefficients over quantiles) or to changemfrow
parameter there.
Can anybody point out where am I going wrong? Should I look into the source code of plot.rqs
and change any parameters there?
While
quantreg::plot.summary.rqs
has anmfrow
parameter, it uses it to overridepar('mfrow')
so as to facet overparm
values, which is not what you want to do.One alternative is to parse the objects and plot manually. You can pull the tau values and coefficient matrix out of
fit1
andfit2
, which are just lists of values for each tau, so in tidyverse grammar,Pull more out of the objects if you like, add the horizontal lines of the original, and otherwise go wild.
Another option is to use
magick
to capture the original images (or save them with any device and reread them) and manually combine them: