在R中删除ggplot中未使用的变量和问题

时间:2016-03-25 10:23:37

标签: r variables ggplot2

要删除未使用的变量,我想使用local()和gc()函数。 但它们对我没用。所以我想在同一个图上绘制一些Scatter Plots,因为我用了多色函数。此函数采用我在{for}循环中创建的列表。

rm(list=ls())
library(ggplot2)
multiplot <- function(..., plotlist=NULL, file, cols=1, layout=NULL) {
  library(grid)
  plots <- c(list(...), plotlist)
  numPlots = length(plots)
  if (is.null(layout)) {
    layout <- matrix(seq(1, cols * ceiling(numPlots/cols)), ncol = cols, nrow = ceiling(numPlots/cols))
  }
 if (numPlots==1) {
    print(plots[[1]])
  } else {
    grid.newpage()
    pushViewport(viewport(layout = grid.layout(nrow(layout), ncol(layout))))
    for (i in 1:numPlots) {
      matchidx <- as.data.frame(which(layout == i, arr.ind = TRUE))
      print(plots[[i]], vp = viewport(layout.pos.row = matchidx$row, layout.pos.col = matchidx$col))
    }
  }
}
scatterList <- list()
ic <- c("multiplot", "scatterList", "ic")
for (i in c("mpg", "disp")) {
    for (y in c("hp", "qsec")) {
        scattername <- paste(i, y, sep = "_")
        scatterList[[scattername]] <- ggplot(mtcars, aes(mtcars[[i]], mtcars[[y]])) + geom_point(alpha = 1/10, colour = "red", size = 3, na.rm = T) + annotate("text", x = c(max(mtcars[[i]], na.rm = T) * 0.5, max(mtcars[[i]], na.rm = T) * 0.5), y = c(max(mtcars[[y]], na.rm = T) * 0.9, max(mtcars[[y]], na.rm = T) * 0.85), label = c(paste("Pearson.Cor = ", round(cor(mtcars[[i]], mtcars[[y]], method="pearson", use="pairwise.complete.obs"), digits=2), sep = ""), paste("Spearman.Cor = ", round(cor(mtcars[[i]], mtcars[[y]], method="spearman", use="pairwise.complete.obs"), digits=2), sep = "")), size=4)
    }
}
save(list=ic, file="mtcars_test.RData")
rm(list=ls()[!(ls() %in% ic)])
print(multiplot(plotlist = scatterList, cols = 2, layout = matrix(c(1:4), ncol = 2, nrow = 2, byrow = T)))

工作周期之后是我想要删除的变量:“i”,“y”,“skattername”。我删除,然后我无法绘制图形,因为列表“scatterList”中的数据存储为变量。

请告诉我如何修复它:打印散点图并删除未使用的变量

1 个答案:

答案 0 :(得分:1)

可能ggplot2创建了一个在全局范围内链接到i的公式。如果需要局部变量,则必须使用功能范围:

rm(list=ls())
library(ggplot2)
multiplot <- function(..., plotlist=NULL, file, cols=1, layout=NULL) {
  library(grid)
  plots <- c(list(...), plotlist)
  numPlots = length(plots)
  if (is.null(layout)) {
    layout <- matrix(seq(1, cols * ceiling(numPlots/cols)), ncol = cols, nrow = ceiling(numPlots/cols))
  }
 if (numPlots==1) {
    print(plots[[1]])
  } else {
    grid.newpage()
    pushViewport(viewport(layout = grid.layout(nrow(layout), ncol(layout))))
    for (i in 1:numPlots) {
      matchidx <- as.data.frame(which(layout == i, arr.ind = TRUE))
      print(plots[[i]], vp = viewport(layout.pos.row = matchidx$row, layout.pos.col = matchidx$col))
    }
  }
}
scatterList <- list()
ic <- c("multiplot", "scatterList", "ic")
fx <- function() {
    for (ix in c("mpg", "disp")) {
        for (y in c("hp", "qsec")) {
            scattername <- paste(ix, y, sep = "_")
            scatterList[[scattername]] <- ggplot(mtcars, aes(mtcars[[ix]], mtcars[[y]])) +
                geom_point(alpha = 1/10, colour = "red", size = 3, na.rm = T) +
                annotate("text", x = c(max(mtcars[[ix]], na.rm = T) * 0.5, max(mtcars[[ix]], na.rm = T) * 0.5),
                         y = c(max(mtcars[[y]], na.rm = T) * 0.9, max(mtcars[[y]], na.rm = T) * 0.85),
                         label = c(paste("Pearson.Cor = ", round(cor(mtcars[[ix]], mtcars[[y]], method="pearson", use="pairwise.complete.obs"), digits=2), sep = ""), paste("Spearman.Cor = ", round(cor(mtcars[[ix]], mtcars[[y]], method="spearman", use="pairwise.complete.obs"), digits=2), sep = "")), size=4)
        }   
    }
    scatterList
}
scatterList <- fx()
save(list=ic, file="mtcars_test.RData")
rm(list=ls()[!(ls() %in% ic)])
print(multiplot(plotlist = scatterList, cols = 2, layout = matrix(c(1:4), ncol = 2, nrow = 2, byrow = T)))

您也可以使用scatterList <- local({})scatterList<- (function(){})()