如何在R中的vis.gam图中更改绘图区域颜色?

时间:2015-03-25 14:03:59

标签: r gam

我有这个数据集:

sample <- structure(list(A = c(1415.6, 1345.3, 1321.7, 1234.5, 1567.8, 
1476.6, 1610.1, 1422.6, 1209.1, 1249.3, 1377.5, 1525.7, 1683.7, 
1500.1, 1565.3, 1737.4, 1321, 1477.8, 1642, 1608.1, 1427.8, 1608.2, 
1404.4, 1688.3, 1795.4), B = c(98, 457, 756, 971, 1148, 4260, 
16307, 42614, 69787, 76301, 80491, 82267, 83975, 85310, 86322, 
94492, 98798, 102514, 126045.986, 160848.998, 183607.7625, 212747.9255, 
249117.2874, 306092.91, 339609.8663), C = c(1.2397, 1.5526, -0.1829, 
-0.3298, -0.1945, 2.8669, 1.3536, 0.781, 0.0324, -1.4283, -0.4413, 
-0.8583, -0.039, -0.2464, -0.277, 2.0885, -0.6405, -0.1474, 1.8457, 
0.3913, -0.4248, 0.2472, 0.2216, 0.4489, -0.5306)), .Names = c("A", 
"B", "C"), class = "data.frame", row.names = c(NA, -25L))

我希望在vis.gam函数中更改绘图区域颜色(反转灰色 - 绘图中的数字越大,绘图区域的颜色越深,反之亦然):

library(mgcv)
m0 <-gam(C ~ A + B, data = sample)

vis.gam(m0, plot.type="contour", color="gray")

我想反转调色板。如果不可能,请手动更改。 我试过这样的事情(我只是偶然选择了名字)

vis.gam(m0,plot.type="contour", col=c("#FFFFFF", "#F7F7F7", "666666"))
vis.gam(m0,plot.type="contour", col=("grey25", "grey26", "grey27"))

但这不起作用。

1 个答案:

答案 0 :(得分:2)

不幸的是vis.gam的定义不允许你想要的东西。幸运的是,修改此函数以执行您想要的操作非常容易:

 # first get the definition of vis.gam
 newDef <- deparse(vis.gam)

 # change the line defining the direction of the grey gradient
 newDef[grep("gray\\(seq\\(",newDef)] <- "            pal <- gray(seq(0.9, 0.1, length = nCol))"

 # then define a new function with this new definition
 vis.gam2 <- eval(parse(text=newDef))

现在使用vis.gam2可以执行您想要的操作:

vis.gam2(m0, plot.type="contour", color="gray")