Reputation: 727
Is there a way to write code efficiently for a repetitive code inside a ggarrange
? My current code looks ugly and time consuming as I have to type from z1
all the way up to z16
.
ggarrange(z1, z2, z3, z4, z5, z6, z7, z8, z9, z10, z11, z12, z13, z14, z15, z16, nrow = 4, ncol = 4, labels = c(1:16))
I tried:
combined = noquote(paste0("z", 1:16))
ggarrange(combined, nrow = 4, ncol = 4, labels = c(1:16))
Warning message:
In as_grob.default(plot) :
Cannot convert object of class noquote into a grob.
Upvotes: 0
Views: 200
Reputation: 206486
How did you wind up with those 16 different variables in the first place? It would be easier to work with in R if you had those related values in a list. Variables with indexes in their names as a sign you probably aren't doing things in a very R-like way.
We can "fix" the problem by using mget()
to put them all in a list. Then we can pass that list to the plotlist=
parameter of ggarrange
. For example
combined <- mget(paste0("z", 1:16))
ggarrange(plotlist=combined, nrow = 4, ncol = 4, labels = 1:16)
Using noquote()
doesn't turn strings into variables. It's just a purely cosmetic function in that it requests the console to suppress the quotes when printing the values.
Upvotes: 2