绘图功能错误

时间:2016-06-01 08:44:24

标签: r function plot viewport

我在不同的数据集上多次运行leaps包。这产生了许多不同的数字。因为我试图运行它很多次,我想把它变成一个绘图功能,让我更容易应用它。但是我在功能方面并不擅长而且奇怪的是出了问题 - 我怀疑这是因为我在某个地方写错了函数!

FUNCTIONNAME <- function(A, B, C){

plot.new()
vp0 <- viewport(x=0, y = 0.9, just = c("center"))
vp1 <- viewport(x=0,y=0,width=0.5, height=1, just = c("left", "bottom"))
vp2 <- viewport(x=0.5,y=0,width=0.5, height=1, just = c("left", "bottom"))

pushViewport(vp1)
par(new=TRUE, fig=gridFIG())
plot.regsubsetsMOD((A), scale = "bic", main = NULL, cex.main = 0.5, cex.axis = 0.65)
grid.text("BIC", 0.55, 0.825)
upViewport()

pushViewport(vp2)
par(new=TRUE, fig=gridFIG())
plot.regsubsetsMOD((A), scale = "r2", main = NULL, cex.main = 0.5, cex.axis = 0.65)
grid.text("R2", 0.55, 0.825)
upViewport()

pushViewport(vp0)
grid.text("B")

dev.copy(png,"C", height = 400, width = 1000)
dev.off()

}

FUNCTIONNAME(A = "Aut_Q_PCA_Models", B = "'textstring'", C = "'textstring.png'" )

我得到的错误是:

  

“lsum $ rsq中的错误:$运算符对原子向量无效”

这让我感到困惑,因为lsum$rsq功能位于plot.regsubsetsMOD,而不是我的绘图功能。 plot.regsubsetsMOD函数通常正常工作,所以我认为错误不在那里 - 它在我的新函数中用于绘制viewport。任何帮助将不胜感激!

以下是该页面的完整代码:

library(leaps)
library(grid)
library(gridBase)

#####Apply leaps to first data set
setwd("J:/Academic papers/Dissertation journal paper/Version 3/New analysis/R code/Full data/AutumnLIFE-ChalkRiver")

AutumnLIFEChalkRiver <- read.csv("AutumnLIFE-ChalkRiver.csv",header=T)
attach(AutumnLIFEChalkRiver)

Aut_Q_PCA <- AutumnLIFEChalkRiver[,c(3, 6, 7, 10, 12, 16)]

Aut_Q_PCA_Models <-regsubsets(AutumnChalkLife~.,
                          data=Aut_Q_PCA,
                          nbest=5)

#####Generate figure
setwd("J:/R/Leaps")
FUNCTIONNAME(A ="Aut_Q_PCA_Models", B = "'textstring'", C = "'textstring.png'" )

代码plot.regsubsetsMOD是对leaps代码的修改,但它非常小,我只是在几行上添加了...,以便我可以将图更改为更多清晰可辨:

plot.regsubsetsMOD <- function (x, labels = obj$xnames, main = NULL, scale = c("bic", 
                                                                               "Cp", "adjr2", "r2"), col = gray(seq(0, 0.9, length = 10)), 
                                ...) 
{
  obj <- x
  lsum <- summary(obj)
  par(mar = c(7, 5, 6, 3) + 0.1)
  nmodels <- length(lsum$rsq)
  np <- obj$np
  propscale <- FALSE
  sscale <- pmatch(scale[1], c("bic", "Cp", "adjr2", "r2"), 
                   nomatch = 0)
  if (sscale == 0) 
    stop(paste("Unrecognised scale=", scale))
  if (propscale) 
    stop(paste("Proportional scaling only for probabilities"))
  yscale <- switch(sscale, lsum$bic, lsum$cp, lsum$adjr2, lsum$rsq)
  up <- switch(sscale, -1, -1, 1, 1)
  index <- order(yscale * up)
  colorscale <- switch(sscale, yscale, yscale, -log(pmax(yscale, 
                                                         1e-04)), -log(pmax(yscale, 1e-04)))
  image(z = t(ifelse(lsum$which[index, ], colorscale[index], 
                     NA + max(colorscale) * 1.5)), xaxt = "n", yaxt = "n", 
        x = (1:np), y = 1:nmodels, xlab = "", ylab = "", 
        col = col)
  laspar <- par("las")
  on.exit(par(las = laspar))
  par(las = 2)
  axis(1, at = 1:np, labels = labels, ...) #Modified
  axis(2, at = 1:nmodels, labels = signif(yscale[index], 2), ...) #Modified
  if (!is.null(main)) 
    title(main = main, ...) #Modified
  box()
  invisible(NULL)
}

1 个答案:

答案 0 :(得分:1)

要理解的主要是"A"是一个字符串,包含字母 A 。即使变量的名称A是包含字母 Aut_Q_PCA_Models 的字符串,它仍然存在。换句话说,"A"A是不同的东西(只要定义A <- "A")。同样适用于"B"B"C"C的对比。

您的绘图功能,名为FUNCTIONNAME,调用函数plot.regsubsetsMOD。正如你所写,它以

开头
plot.regsubsetsMOD <- function (x, labels = obj$xnames, main = NULL, scale = c("bic", Cp", "adjr2", "r2"), col = gray(seq(0, 0.9, length = 10)), 
                            ...) 
{
obj <- x
lsum <- summary(obj)
par(mar = c(7, 5, 6, 3) + 0.1)
nmodels <- length(lsum$rsq)

您的绘图功能会将A = "Aut_Q_PCA_Models"解析为它。所以当基本上R试图运行summary("Aut_Q_PCA_Models")$rsq时会出现问题。在单个字符串上运行摘要显然毫无意义。当您从regsubsets - 类 Aut_Q_PCA_Models 命名模型时,

FUNCTIONNAME(A = Aut_Q_PCA_Models, B = "textstring", C = "textstring.png" )
如果您将行dev.copy(png,"C", height = 400, width = 1000)更改为dev.copy(png, C, height = 400, width = 1000)而将grid.text("B")更改为grid.text(B),则

应该有效。