How to add more arguments of a function in do.call? - r

My question is how I might be able to add more arguments to the do.call function.
For example, I want to draw faceted grid plots with grid.arrange, how can I add more arguments such as ncol=3 and main="main title" to the command do.call(grid.arrange,plots)?

consider this list of plots,
library(ggplot2)
library(gridExtra)
pl = replicate(5, qplot(1,1), simplify = FALSE)
you can combine it with a list of options to be passed to do.call,
do.call(grid.arrange, c(pl, list(ncol=5, main="title")))

Related

plotmath in gridarrange title

I have ggplot plots in a list, say
plts <- list(plt1=qplot(1:10),plt2=qplot(2:3))
which I would like to plot using grid.arrange and a title:
do.call(grid.arrange,c(plts,top='a title'))
The difficulty is I would like some plotmath expressions in that grid.arrange title. If I do
do.call(grid.arrange,c(plts,top=expression('a title[2]') ))
R coerces the second argument to do.call to an expression type rather than list, and do.call throws an error. I tried setting the class manually to "list" but to no avail. What is the proper way to go about this?
according to manual of grid.arrange:
top optional string, or grob
so in oder to use expression, you should provide a grob.
for example, If you want to plot 2 as subscript:
library(grid)
grid.arrange(grobs = plts,top= grid.text(expression('a' ~ title[2])))
# or
do.call(grid.arrange, list(grobs = plts,top = grid.text(expression('a' ~ title[2])) ))

how to plot multiplot (two columns plots) per page in a single pdf?

I am using ggplot and ggplot.multiplot function to plot multiplots (2 columns plots) per page but I couldnt make it. please help
I have a list of ggplots in variable plot_list and using function ggplot2.multiplot to plot 2 plot per page. But it plot all figures in one page that messed up. I want two plot per page in single figure.
>plot_list ## ggplot saved in a list though i have long list to plot
[[1]]
[[2]]
[[3]]
[[4]]
In both case i tried but all four plots plotted in same page:
library(easyGgplot2)
library(ggplot2)
ggplot2.multiplot(plotlist = plot_list, cols=2)
ggplot2.multiplot(plotlist = plot_list)
However its work as:
ggplot2.multiplot(plot_list[[1]],plot_list[[2]],cols=2)
ggplot2.multiplot(plot_list[[3]],plot_list[[4]],cols=2)
But i have long list of figures to generate in a single pdf !!
I also i have tried library("cowplot") but got error while using list of figures.
plot_grid(plot_list, ncol = 2, nrow = 1)
Error in ggplot_to_gtable(x) :
Argument needs to be of class "ggplot" or "gtable"
Please help.
Thanks
there's gridExtra::marrangeGrob
library(ggplot2)
library(gridExtra)
pl <- replicate(5, ggplot(), simplify=FALSE)
ml <- marrangeGrob(pl, nrow=1, ncol=2)
ggsave("multipage.pdf", ml)
for your cowplot problem, there is an argument plotlist in the plot_grid function (https://rdrr.io/cran/cowplot/man/plot_grid.html):
plot_grid(plotlist=plot_list)
should work

R: gridExtra - How to plot a Summary as table?

I'm having trouble plotting a simple summary.
library(gridExtra)
SummaryTable <- summary(s.tvs$precio.nuevo)
grid.table(SummaryTable)
Gives me this:
I want to achieve something like this:
Upgrade comment:
grid.table calls tableGrob.
grid.table
#function (...)
#grid.draw(tableGrob(...))
#<environment: namespace:gridExtra>
From ?tableGrob its first argument is a matrix or data.frame. t coerces the named vector returned by summary to a matrix with dimension one row. Alternatively, you could of used as.matrix to produce a matrix with one column.
grid.newpage()
grid.table(t(summary(mtcars$mpg)))
grid.newpage()
grid.table(as.matrix(summary(mtcars$mpg)))
From comment:
Question:
I'm trying to plot a barplot and the table generated in this answer. I get: Error in gList(list(grobs = list(list(x = 0.5, y = 0.5, width = 1, height = 1, : only 'grobs' allowed in "gList" when using this code: grid.arrange(a, tbl, ncol = 1)
To combine different tables / plots using grid.arrange they need to be grobs (grid GRaphcal OBjects). So you cannot pass the results from grid.table to grid.arrange as it is not a grob (it actually plots the tableGrob directly). For this you need to pass the tableGrob.
So for example:
mybar <- qplot(mtcars$mpg, geom="bar")
tbl <- tableGrob(t(summary(mtcars$mpg)))
grid.newpage()
grid.arrange(mybar, tbl)

ggplot2 : printing multiple plots in one page with a loop

I have several subjects for which I need to generate a plot, as I have many subjects I'd like to have several plots in one page rather than one figure for subject.
Here it is what I have done so far:
Read txt file with subjects name
subjs <- scan ("ListSubjs.txt", what = "")
Create a list to hold plot objects
pltList <- list()
for(s in 1:length(subjs))
{
setwd(file.path("C:/Users/", subjs[[s]])) #load subj directory
ifile=paste("Co","data.txt",sep="",collapse=NULL) #Read subj file
dat = read.table(ifile)
dat <- unlist(dat, use.names = FALSE) #make dat usable for ggplot2
df <- data.frame(dat)
pltList[[s]]<- print(ggplot( df, aes(x=dat)) + #save each plot with unique name
geom_histogram(binwidth=.01, colour="cyan", fill="cyan") +
geom_vline(aes(xintercept=0), # Ignore NA values for mean
color="red", linetype="dashed", size=1)+
xlab(paste("Co_data", subjs[[s]] , sep=" ",collapse=NULL)))
}
At this point I can display the single plots for example by
print (pltList[1]) #will print first plot
print(pltList[2]) # will print second plot
I d like to have a solution by which several plots are displayed in the same page, I 've tried something along the lines of previous posts but I don't manage to make it work
for example:
for (p in seq(length(pltList))) {
do.call("grid.arrange", pltList[[p]])
}
gives me the following error
Error in arrangeGrob(..., as.table = as.table, clip = clip, main = main, :
input must be grobs!
I can use more basic graphing features, but I d like to achieve this by using ggplot. Many thanks for consideration
Matilde
Your error comes from indexing a list with [[:
consider
pl = list(qplot(1,1), qplot(2,2))
pl[[1]] returns the first plot, but do.call expects a list of arguments. You could do it with, do.call(grid.arrange, pl[1]) (no error), but that's probably not what you want (it arranges one plot on the page, there's little point in doing that). Presumably you wanted all plots,
grid.arrange(grobs = pl)
or, equivalently,
do.call(grid.arrange, pl)
If you want a selection of this list, use [,
grid.arrange(grobs = pl[1:2])
do.call(grid.arrange, pl[1:2])
Further parameters can be passed trivially with the first syntax; with do.call care must be taken to make sure the list is in the correct form,
grid.arrange(grobs = pl[1:2], ncol=3, top=textGrob("title"))
do.call(grid.arrange, c(pl[1:2], list(ncol=3, top=textGrob("title"))))
library(gridExtra) # for grid.arrange
library(grid)
grid.arrange(pltList[[1]], pltList[[2]], pltList[[3]], pltList[[4]], ncol = 2, main = "Whatever") # say you have 4 plots
OR,
do.call(grid.arrange,pltList)
I wish I had enough reputation to comment instead of answer, but anyway you can use the following solution to get it work.
I would do exactly what you did to get the pltList, then use the multiplot function from this recipe. Note that you will need to specify the number of columns. For example, if you want to plot all plots in the list into two columns, you can do this:
print(multiplot(plotlist=pltList, cols=2))

Multiple graphs within plot with loop

How to get graph for each column of data.frame within one plot with loop? Must be easy just can't figure it out.
Sample data:
rdata <- data.frame(y=rnorm(1000,2,2),v1=rnorm(1000,1,1),v2=rnorm(1000,3,3),
v3=rnorm(1000,4,4),v4=rnorm(1000,5,5))
What I have tried?
library(lattice)
p <- par(mfrow=c(2,2))
for(i in 2:5){
w <- xyplot(y~rdata[,i],rdata)
print(w)
}
par(p)
If you don't have to use lattice you can just use base plot instead and it should work as you want.
p <- par(mfrow=c(2,2))
for(i in 2:5){
plot(y~rdata[,i],rdata)
}
par(p)
If you want to use lattice look this answer. Lattice ignores par, so you have to do some more work to achieve what you want.
Inorder to easily arrange a bunch of lattice plots, I like to use the helper function print.plotlist. It has a layout= parameter that acts like the layout() function for base graphics. For example, you could call
rdata <- data.frame(y=rnorm(1000,2,2),v1=rnorm(1000,1,1),v2=rnorm(1000,3,3),
v3=rnorm(1000,4,4),v4=rnorm(1000,5,5))
library(lattice)
plots<-lapply(2:5, function(i) {xyplot(y~rdata[,i],rdata)})
print.plotlist(plots, layout=matrix(1:4, ncol=2))
to get
Otherwise you normally use a split= parameter to the print statement to place a plot in a subsection of the device. For example, you could also do
print(plots[[1]], split=c(1,1,2,2), more=T)
print(plots[[2]], split=c(1,2,2,2), more=T)
print(plots[[3]], split=c(2,1,2,2), more=T)
print(plots[[4]], split=c(2,2,2,2))

Resources