简体   繁体   English

防止plot.gam产生图形

[英]Prevent plot.gam from producing a figure

Say, I have a GAM that looks like this: 说,我有一个看起来像这样的GAM:

# Load library
library(mgcv)

# Load data
data(mtcars)

# Model for mpg
mpg.gam <- gam(mpg ~ s(hp) + s(wt), data = mtcars)

Now, I'd like to plot the GAM using ggplot2 . 现在,我想使用ggplot2绘制GAM。 So, I use plot.gam to produce all the information I need, like this: 因此,我使用plot.gam生成我需要的所有信息,如下所示:

foo <- plot(mpg.gam)

This also generates an unwanted figure. 这也会产生不需要的数字。 (Yes, I realise that I'm complaining that a plotting function plots something...) When using visreg in the same way, I'd simply specify plot = FALSE to suppress the figure, but plot.gam doesn't seem to have this option. (是的,我意识到我在抱怨绘图函数在绘制某些东西...)以相同的方式使用visreg时,我只是指定plot = FALSE来压制图形,但是plot.gam似乎没有有这个选择。 My first thought was perhaps invisible would do the job (eg, invisible(foo <- plot(mpg.gam)) ), but that didn't seem to work. 我的第一个想法也许是invisible会完成这项工作(例如, invisible(foo <- plot(mpg.gam)) ),但这似乎没有用。 Is there an easy way of doing this without outputting the unwanted figure to file? 是否有一种简单的方法可以在不将不需要的图形输出到文件的情况下执行此操作?

Okay, so I finally figured it out 5 minutes after posting this. 好的,所以我最终在发布此消息5分钟后就知道了。 There is an option to select which term to plot (eg, select = 1 is the first term, select = 2 is the second), although the default behaviour is to plot all terms. 尽管默认行为是绘制所有项,但是可以选择绘制哪个项(例如, select = 1是第一个项, select = 2是第二个项)。 If, however, I use select = 0 it doesn't plot anything and doesn't give an error, yet returns exactly the same information. 但是,如果我使用select = 0则它不会绘制任何内容,也不会给出错误,但返回的信息完全相同。 Check it out: 看看这个:

# Load library
library(mgcv)

# Load data
data(mtcars)

# Model for mpg
mpg.gam <- gam(mpg ~ s(hp) + s(wt), data = mtcars)

# Produces figures for all terms
foo1 <- plot(mpg.gam)

# Doesn't produce figures
foo2 <- plot(mpg.gam, select = 0)

# Compare objects
identical(foo1, foo2)

[1] TRUE

Bonza! 邦扎!

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM