【问题标题】:Remove unused variables and problems with ggplot in R删除 R 中 ggplot 的未使用变量和问题
【发布时间】:2016-03-25 10:23:36
【问题描述】:

要删除未使用的变量,我想使用 local() 和 gc() 函数。 但它们对我没有用。所以我想在同一张图上画一些散点图,我使用了 multiplot 函数。此函数采用我在 {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”中的数据存储为变量。

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

【问题讨论】:

    标签: r variables ggplot2


    【解决方案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 &lt;- local({})scatterList&lt;- (function(){})()

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2016-05-22
      • 1970-01-01
      • 2013-07-14
      • 2013-01-27
      • 1970-01-01
      • 2021-05-23
      • 1970-01-01
      相关资源
      最近更新 更多