将图另存为R对象并显示在网格中 [英] Save plots as R objects and displaying in grid

查看:124
本文介绍了将图另存为R对象并显示在网格中的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在以下可重现的示例中,我尝试为ggplot分布图创建一个函数并将其保存为R对象,目的是在网格中显示两个图.

In the following reproducible example I try to create a function for a ggplot distribution plot and saving it as an R object, with the intention of displaying two plots in a grid.

ggplothist<- function(dat,var1)
{
        if (is.character(var1)) {
            var1 <- which(names(dat) == var1) 
    }
    distribution <- ggplot(data=dat, aes(dat[,var1])) 
    distribution <- distribution  + geom_histogram(aes(y=..density..),binwidth=0.1,colour="black", fill="white")
    output<-list(distribution,var1,dat)
    return(output)
}

调用函数:

set.seed(100)
df  <- data.frame(x = rnorm(100, mean=10),y =rep(1,100))    
output1 <- ggplothist(dat=df,var1='x')
output1[1]

一切都好,直到现在.

然后我要绘制第二个情节,(注意均值= 100,而不是前10个)

Then i want to make a second plot, (of note mean=100 instead of previous 10)

df2  <- data.frame(x = rep(1,1000),y = rnorm(1000, mean=100))    
output2 <- ggplothist(dat=df2,var1='y')
output2[1]

然后我尝试以均值10重新绘制第一次分布.

Then i try to replot first distribution with mean 10.

output1[1]

我得到的分配与以前一样? 但是,如果我使用函数中包含的信息,请将其返回并重置为有效的全局变量.

I get the same distibution as before? If however i use the information contained inside the function, return it back and reset it as a global variable it works.

var1=as.numeric(output1[2]);dat=as.data.frame(output1[3]);p1 <- output1[1]
p1

如果有人能解释为什么会发生这种情况,我想知道.似乎为了绘制预期的分布,我必须将data.frame和变量重置为用于绘制图的内容.有没有一种方法可以将图另存为对象,而不必这样做.幸运的是,我可以重新分配第一个发行版.

If anyone can explain why this happens I would like to know. It seems that in order to to draw the intended distribution I have to reset the data.frame and variable to what was used to draw the plot. Is there a way to save the plot as an object without having to this. luckly I can replot the first distribution.

但是我不能同时绘制它们

but i can't plot them both at the same time

 var1=as.numeric(output2[2]);dat=as.data.frame(output2[3]);p2 <- output2[1]
 grid.arrange(p1,p2)

错误:gList(list(list(list(data = list(x = c(9.66707664902549,11.3631137069225, 在"gList"中仅允许使用"grobs"

ERROR: Error in gList(list(list(data = list(x = c(9.66707664902549, 11.3631137069225, : only 'grobs' allowed in "gList"

在此" for循环中制作的多个ggplot2图的网格建议答案使用包含图的列表

In this" Grid of multiple ggplot2 plots which have been made in a for loop " answer is suggested to use a list for containing the plots

 ggplothist<- function(dat,var1)
{
    if (is.character(var1)) {
            var1 <- which(names(dat) == var1) 
    }
    distribution <- ggplot(data=dat, aes(dat[,var1])) 
    distribution <- distribution  + geom_histogram(aes(y=..density..),binwidth=0.1,colour="black", fill="white")
    plot(distribution)
    pltlist <- list()
    pltlist[["plot"]] <- distribution
    output<-list(pltlist,var1,dat)
    return(output)
}

output1 <- ggplothist(dat=df,var1='x')
p1<-output1[1]

output2 <- ggplothist(dat=df2,var1='y')
p2<-output2[1]

output1[1]

将再次产生均值= 100而不是均值= 10的分布 和:

Will produce the distribution with mean=100 again instead of mean=10 and:

grid.arrange(p1,p2)

将产生相同的错误

gList(list(list(list(plot = list(data = list(x = c(9.66707664902549,: 在"gList"中仅允许使用"grobs"

Error in gList(list(list(plot = list(data = list(x = c(9.66707664902549, : only 'grobs' allowed in "gList"

作为最后一次尝试,我尝试使用recordPlot()将有关绘图的所有内容记录到一个对象中.现在,在函数内部.

As a last attempt i try to use recordPlot() to record everything about the plot into an object. The following is now inside the function.

    ggplothist<- function(dat,var1)
{
    if (is.character(var1)) {
            var1 <- which(names(dat) == var1) 
    }
    distribution <- ggplot(data=dat, aes(dat[,var1])) 
    distribution <- distribution  + geom_histogram(aes(y=..density..),binwidth=0.1,colour="black", fill="white")
    plot(distribution)
    distribution<-recordPlot()
    output<-list(distribution,var1,dat)
    return(output)
}

此函数将产生与以前相同的错误,具体取决于将dat和var1变量重置为绘制分布所需的变量.同样也不能放在网格内

This function will produce the same errors as before, dependent on resetting the dat, and var1 variables to what is needed for drawing the distribution. and similarly can't be put inside a grid.

在此问题中,我尝试了类似rangingGrob()的类似操作"

I've tried similar things like arrangeGrob() in this question "R saving multiple ggplot2 plots as R-object in list and re-displaying in grid " but with no luck.

我真的很想创建一个包含图形的R对象的解决方案,该对象可以自己重绘,并且可以在网格内使用,而不必每次完成绘制时都要重置用于绘制图形的变量.我也想了解这是怎么发生的,因为我根本不认为这很直观.

I would really like a solution that creates an R object containing the plot, that can be redrawn by itself and can be used inside a grid without having to reset the variables used to draw the plot each time it is done. I would also like to understand wht this is happening as I don't consider it intuitive at all.

我能想到的唯一解决方案是将绘图绘制为png文件,保存在某处,然后让该函数返回路径,以便我可以重用-这是其他人在做什么?

The only solution I can think of is to draw the plot as a png file, saved somewhere and then have the function return the path such that i can be reused - is that what other people are doing?.

感谢您的阅读,并为冗长的问题感到抱歉.

Thanks for reading, and sorry for the long question.

推荐答案

找到了解决方法

如何引用函数中的局部环境,在R中?

通过插入

localenv <- environment() 

并在ggplot中引用

And referencing that in the ggplot

distribution <- ggplot(data=dat, aes(dat[,var1]),environment = localenv)

使一切正常!即使使用网格排列!

made it all work! even with grid arrange!

这篇关于将图另存为R对象并显示在网格中的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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