JeP
JeP

Reputation: 61

Grabbing single plots from multi-plot output in R

I'm attempting to grab one plot from a multiple plot output. For example

library(mboost); 
mod=gamboost(Ozone~.,data=airquality[complete.cases(airquality),]); 
plot(mod)

The above creates a plot for each variable's "partial effect". The same could be said for the residual plots created when plotting a linear model (lm). I've attempted to save the output in a list akin to how ggplots can be saved and have spent a few hours searching how to extract just one plot but have failed. Any advice?

As for the context of the question, I'm trying to put the plots into a shiny app and have a variable number of plots show up as output.

Session info is as follows: R version 2.15.2 (2012-10-26) Platform: i386-redhat-linux-gnu (32-bit)

Upvotes: 6

Views: 4525

Answers (3)

Benjamin Hofner
Benjamin Hofner

Reputation: 211

Essentially, @greg-snow gave a proper solution. I will elaborate this a bit.

In mboost you can use

plot(mod, which = "Day")

to plot the effect of Day only. As we use regular expressions you can even do much more using the argument which. In a model with linear and smooth effects you can for example extract all smooth effects for plotting:

airquality$Month <- as.factor(airquality$Month)
mod <- mod <- gamboost(Ozone ~ bbs(Solar.R) + bbs(Wind) + bbs(Temp) + bols(Month) + bbs(Day), data=airquality[complete.cases(airquality),])

## now plot bbs effects, i.e., smooth effects:
par(mfrow = c(2,2))
plot(mod, which = "bbs")

## or the linear effect only
par(mfrow = c(1,1))
plot(mod, which = "bols")

You can use any portion of the name (see e.g. names(coef(mod)))to define the effect to be plotted. You can also use integer values to define which effect to plot:

plot(mod, which = 1:2)

Note that this can be also used to certain extract coefficients. E.g.

coef(mod, which = 1)
coef(mod, which = "Solar")
coef(mod, which = "bbs(Solar.R)") 

are all the same. For more on how to specify which, both in coef and plot please see our tutorial paper (Hofner et al. (2014), Model-based Boosting in R - A Hands-on Tutorial Using the R Package mboost. Computational Statistics, 29:3-35. DOI 10.1007/s00180-012-0382-5).

We acknowledge that this currently isn't documented in mboost but it is on our todo list (see github issue 14).

Upvotes: 3

gung - Reinstate Monica
gung - Reinstate Monica

Reputation: 11893

(I'm not familiar with GAMboost.)

Looking at the documentation for ?plot.GAMBoost, I see there is an argument called select. I gather you would set this argument to the variable you are interested in, and then you would get just the single plot you want. This is analogous the the which argument in plot.lm that @GregSnow notes.

Upvotes: 1

Greg Snow
Greg Snow

Reputation: 49640

Many functions that produce multiple plots also have an argument to select a subset of the plots. In the case of plot.lm it is the which argument. So saying plot(fit, which=1) will only produce one plot.

You can check the mboost documentation to see if there is a similar argument for that plotting function.

Upvotes: 6

Related Questions