In the following reproducible example, I am trying to create a function for the ggplot distribution graph and save it as an R object in order to display two graphs in the 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) }
Function call:
set.seed(100) df <- data.frame(x = rnorm(100, mean=10),y =rep(1,100)) output1 <- ggplothist(dat=df,var1='x') output1[1]

Everything is fine so far.
Then I want to make a second graph (note mean = 100 instead of the previous 10)
df2 <- data.frame(x = rep(1,1000),y = rnorm(1000, mean=100)) output2 <- ggplothist(dat=df2,var1='y') output2[1]

Then I will try to transfer the first distribution with an average of 10.
output1[1]

Am I getting the same distribution as before? However, if I use the information contained within the function, return it and reset as the global variable in which it works.
var1=as.numeric(output1[2]);dat=as.data.frame(output1[3]);p1 <- output1[1] p1

If anyone can explain why this is happening, I would like to know. It seems that in order to draw the intended distribution, I have to reset data.frame and variable to use what was used to draw the graph. Is there a way to save the plot as an object without this. fortunately, I can replace the first distribution.
but I can't build them both at the same time
var1=as.numeric(output2[2]);dat=as.data.frame(output2[3]);p2 <- output2[1] grid.arrange(p1,p2)
ERROR: error in gList (list (data list = list (x = c (9.66707664902549, 11.3631137069225 ,: only "grobs" is allowed in "gList"
In this " grid of several ggplot2 plots that were made in a for loop ." The answer is to use a list to place charts
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]
Will produce a distribution with an average value = 100 again instead of an average = 10 and:
grid.arrange(p1,p2)
will give the same error
Error in gList (list (list (plot = list (data = list (x = c (9.66707664902549 ,: only "grobs" is allowed in "gList"
As a last attempt, I'm trying to use recordPlot () to write everything about the plot to an object. Inside the function is now the following.
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) }
This function will generate the same errors as before, depending on the reset of the variables dat and var1 to what is needed to draw the distribution. and likewise cannot be placed inside the net.
In this question, I tried similar things like organizGrob () " R, saving several ggplot2 plots as an R-object in the list and re-displaying in the grid ", but with no luck.
I would really like the solution to create an R object that contains a plot that can be redrawn by itself and can be used inside the grid without using reset variables used to draw the graph every time this is done. I would also like to understand that this is happening because I do not find it intuitive at all.
The only solution I can think of is to draw the plot as a png file, save it somewhere, and then return the function so that I can be reused - this is what other people do.
Thanks for reading and sorry for the long question.