我怎样才能存储一个ggplots的列表,以便在多重位置中使用而不会覆盖之前的地块? [英] How can I store a list of ggplots to use in multiplot without overwriting previous plots?

查看:153
本文介绍了我怎样才能存储一个ggplots的列表,以便在多重位置中使用而不会覆盖之前的地块?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想使用从另一个函数创建的对象(下面的 cd 参数)在多槽中绘制协方差/相关矩阵的一些热图。协方差矩阵存储在一个三维数组中,因此 cd $ covmat [,, i] 调用第i个协方差矩阵。

原来我在复制同一个图时遇到了一些问题。但是,我发现我有一个环境问题。我尝试了几种解决方法,下面的代码是最新的,但我无法弄清楚它为什么没有正确读取它。



有没有特别的原因呢?我已经试过包括和排除环境参数(我希望不需要),我试着直接使用 cd $ covmat [,, i]
aes()参数。

  drawCovs <-function (cd,ncols){
require(ggplot2)
coords = expand.grid(x = 1:cd $ q,y = 1:cd $ q)
climits = c(-1 ,1)* max(cd $ covmat)
cd $ levels = c(cd $ levels,Total)
covtext = ifelse(!(cd $ use.cor),'Covariance','相关性')
plots = list()
cmat = list()
for(i in 1:(nlevels + 1)){
cmat [[i]]< -cd $ covmat [,, i]
.e< -environment
plots [[i]]< -ggplot(environment = .e)+ geom_tile(aes(x = coords $ x,y = coords $ y,
fill = as.numeric(cmat [[i]]),color ='white'))+
scale_fill_gradient(covtext,low ='darkblue',high ='red' ,limit = climits)+ ylab('')
+ xlab('')+ guides(color ='none')+ scale_x_discrete(labels = cd $ varnames,
限制= 1:cd $ q,expand = c(0,0))+ scale_y_discrete(labels = cd $ varnames,
limits = 1:cd $ q,expand = c(0,0))+ theme axis.text.x = element_text(angle = 90,
hjust = 1))+ labs(title = paste0(covtext,of data,,cd $ levels [i]))
}

多图(plotlist = plots,cols = ncols)
}



  drawCovs<  -  function(cd,ncols){
require(ggplot2)
require(reshape2)
plots = list()
cmat = list()
for(i in 1 :(长度(cd $ covmat) )){
cmat [[i]] < - cd $ covmat [[i]]
plot [[i]] < - ggplot(melt(cmat),aes(x = Var1, y = Var2,fill = value))+
geom_tile(color ='white')
}
多图(plotlist = plots,cols = ncols)
}

cd < - list()
cd $ covmat< - list(matrix(runif(25),5),matrix(runif(25),5))

drawCovs(cd,1)


I want to plot some heatmaps of covariance/correlation matrices in a multiplot using an object created from another function (the cd parameter below). The covariance matrices are stored in an array of 3 dimensions, so that cd$covmat[,,i] calls the ith covariance matrix.

Originally I had some issues with this with having the same plot replicated. However, I discovered I had an environment issue. I've tried resolving this several ways, with the code below being the most recent, but I can't figure out why it's not reading it properly.

Is there a particular reason for this? I've tried including and excluding the environment parameter (which I hopefully shouldn't need) and I've tried directly using the cd$covmat[,,i] in the aes() parameter.

drawCovs<-function(cd,ncols){
    require(ggplot2)
    coords=expand.grid(x=1:cd$q,y=1:cd$q)
    climits = c(-1,1)*max(cd$covmat)
    cd$levels=c(cd$levels,"Total")
    covtext=ifelse(!(cd$use.cor),'Covariance','Correlation')
    plots=list()
    cmat=list()
    for (i in 1:(nlevels+1)){
        cmat[[i]]<-cd$covmat[,,i]
        .e<-environment
        plots[[i]]<-ggplot(environment=.e)+geom_tile(aes(x=coords$x,y=coords$y,
        fill=as.numeric(cmat[[i]]),color='white'))+
        scale_fill_gradient(covtext,low='darkblue',high='red',limits=climits)+ylab('')
        +xlab('')+guides(color='none')+scale_x_discrete(labels=cd$varnames,
        limits=1:cd$q, expand=c(0,0))+scale_y_discrete(labels=cd$varnames,
        limits=1:cd$q, expand=c(0,0))+theme(axis.text.x = element_text(angle = 90,
        hjust = 1))+labs(title=paste0(covtext,"s of data, ",cd$levels[i]))
    }

    multiplot(plotlist=plots,cols=ncols)
}

解决方案

If you end up trying to fix things with direct calls to environments, you are probably overcomplicating your code. Here's a simple snippet that may serve as a core for your function:

drawCovs <- function(cd, ncols) {
  require(ggplot2)
  require(reshape2)
  plots=list()
  cmat=list()
  for (i in 1:(length(cd$covmat))) {
    cmat[[i]] <- cd$covmat[[i]]
    plots[[i]] <- ggplot(melt(cmat), aes(x=Var1, y=Var2, fill=value)) + 
                  geom_tile(color='white')
  }  
  multiplot(plotlist=plots,cols=ncols)
}

cd <- list()
cd$covmat <- list(matrix(runif(25), 5), matrix(runif(25), 5))

drawCovs(cd, 1)

这篇关于我怎样才能存储一个ggplots的列表,以便在多重位置中使用而不会覆盖之前的地块?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆