2016-03-25 7 views
0

未使用の変数を削除するには、local()関数とgc()関数を使いたいと思います。 しかし、彼らは私の役に立たなかった。だから私は、これを使うために同じグラフにいくつかの散布図を描きたいと思います。この関数は{for}サイクルで作成したリストを取ります。ggplotで未使用の変数と問題を削除する

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

を削除し、ggplot2はグローバルスコープでiにリンクされている数式を作成しました:

それを修正する方法を教えてください。また、あなたがscatterList <- local({})またはscatterList<- (function(){})()

を使用することができます

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))) 

:あなたはローカル変数が必要な場合は、機能範囲を使用する必要があります。

関連する問題