0

I applied a for loop in R and generated graphs using do call and grid arrange

It looks similar to this output here which is what I want

How do I arrange a variable list of plots using grid.arrange?

p1 <- c(p,list(nrow=4,ncol=2)) 
do.call(grid.arrange,p1, right = "people", bottom = "XX", top="Regression")

However, I want to tidy the graph and I would like a universal y and x-axis labels with one title but after using the codes above I got error "unused arguments (right = "people", bottom = "XX", top = "Regression")

Any ideas on to sort this

Community
  • 1
  • 1
nee
  • 125
  • 1
  • 11

1 Answers1

2

From ?do.call

Usage

do.call(what, args, quote = FALSE, envir = parent.frame())

Arguments

what either a function or a non-empty character string naming the function to be called.
args a list of arguments to the function call. The names attribute of args gives the argument names.
quote a logical value indicating whether to quote the arguments.
envir an environment within which to evaluate the call. This will be most useful if what is a character string and the arguments are symbols or quoted expressions.

You just need to amend your argument list to include the other parameters. I changed the y axis to be on the left.

library(ggplot2)


df <- data.frame(x=1:10, y=rnorm(10))

plot <- ggplot(df, aes(x,y)) + geom_point() + labs(x = NULL, y = NULL)

p <- list(plot, plot, plot, plot, plot, plot, plot, plot) 

args <- c(p, list(nrow = 4,ncol = 2, left = "people", bottom = "XX", top = "Regression")) 

do.call(grid.arrange, args)

enter image description here

or what @baptiste suggests (I swear I'm going to vote for a !summon command on SO for him)

grid.arrange(grobs = p, nrow = 4,ncol = 2, left = "people", bottom = "XX", top = "Regression")

Jake Kaupp
  • 7,892
  • 2
  • 26
  • 36