plotmath in gridarrange title - r

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])) ))

Related

Shiny reactive as ggplot2 series name and legend label?

I want to use a Shiny input as a name in my shiny plot.
Here is my code:
ggplot() +
...
geom_point(data=data.frame(), aes(x=x, y=y, color=paste(input$name)),
size = 3) +
scale_color_manual(values=c("df1"="blue", "df2"="blue",
paste(input$name)="red"))
It doesn't recognize paste(input$name) as a string. Here is the error message:
1512: scale_color_manual(values=c("df1"="blue", "df2"="blue",
1513: paste(input$name)=
^
Anyone know how to properly structure this?
You can't intermix strings and symbols and expressions like you are doing. If you want to use a string in an aes() mapping, use aes_ or aes_string (no need for paste)
aes_string(x="x", y="y", color=input$name)
And you can't put an expression on the left of = in a named vector. Use something like setNames() instead.
values = setNames(c("blue", "blue", "red"), c("df1", "df2", input$name))
As requested above, it would be easier in the future if you include a reproducible example so that possible solutions can be properly tested. This isn't at all Shiny related. This is just how ggplot and R work.

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

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))

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

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")))

How to create plots dynamically in grid.arrange in R?

I have a function get.single.plot, which takes one character argument and returns a ggplot2 plot object. I would like to build a grid.arrange object with n plots on it, where the n is the size of vector of (mentioned) character arguments.
E.g., I woould like something like this to work:
character.argument.vector <- c("12", "1", "2")
grid.arrange(unlist(lapply(character.argument.vector, function(n) get.single.plot(n))),
ncol = 1)
Such thing does not work - I receive the following information:
Error in arrangeGrob(..., as.table = as.table, clip = clip, main = main, :
input must be grobs!
How should I do it?
With gridExtra v>=2.0 you can simply pass a list of grobs (or plots) to grid.arrange,
grid.arrange(grobs = lapply(...), ncol=1)

Resources