Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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)

like image 236
JeP Avatar asked Jun 03 '13 16:06

JeP


People also ask

How do you make multiple plots to a single page layout in R?

To arrange multiple ggplot2 graphs on the same page, the standard R functions - par() and layout() - cannot be used. The basic solution is to use the gridExtra R package, which comes with the following functions: grid. arrange() and arrangeGrob() to arrange multiple ggplots on one page.

How do I combine scatter plots in R?

To overlay a scatter plot in the R language, we use the points() function. The points() function is a generic function that overlays a scatter plot by taking coordinates from a data frame and plotting the corresponding points.

What does PAR () do in R?

The par() function is used to set or query graphical parameters. We can divide the frame into the desired grid, add a margin to the plot or change the background color of the frame by using the par() function. We can use the par() function in R to create multiple plots at once.


2 Answers

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.

like image 159
Greg Snow Avatar answered Oct 16 '22 16:10

Greg Snow


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).

like image 32
Benjamin Hofner Avatar answered Oct 16 '22 17:10

Benjamin Hofner