I want to display multiple plots depending on the length of my predictors. I have created two list and then used grid.arrange
function to display the plots within these lists, but I am getting the following error message - 'only 'grobs' allowed in "gList"
. Even when I try to use only one list say p, I get the same error message. Please help!
library(ggplot2)
library(gridExtra)
# dependent1 variable
# dependent2 variable
# predictor_vector is a vector of predictors
plot_output(data, dependent1, dependent2, predictor_vector)
{
length<-length(predictor_vector)
p<-list()
g<-list()
for( i in 1:length)
{
p[[i]]<-ggplot(data, aes(y=dependent1, x=predictor_vector[i]))
g[[i]]<-ggplot(data, aes(y=dependent2, x=predictor_vector[i]))
}
do.call("grid.arrange", c(p, g, list(ncol=2)))
}
Posting as an answer only to show the example that's impossible in a comment.
The idiom you're trying to use is correct:
library(ggplot2)
library(gridExtra)
p <- list(ggplot(mtcars, aes(x=wt, y=mpg))+geom_point(col="black"),
ggplot(mtcars, aes(x=wt, y=mpg))+geom_point(col="orange"),
ggplot(mtcars, aes(x=mpg, y=wt))+geom_point(col="blue"))
g <- list(ggplot(mtcars, aes(x=wt, y=mpg))+geom_point(col="red"),
ggplot(mtcars, aes(x=mpg, y=wt))+geom_point(col="green"))
do.call(grid.arrange, c(p, g, list(ncol=2)))
Two variable length ggplot object lists and then the parameter list. You need to provide the data and a more complete loop for us to know how to help you figure out what you're doing incorrectly.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.