I have the problem which I solved but I think my solution is to involved and unproductive when dealing with more variables. I want to write a function which combine three graphs :
Numbers from standard normal distribution
Numbers from standard uniform distribution
Numbers from standard exponential distribution
The length of the samples can be specified. Also in output I want to have one additional input named plot_types. By default it's combining all graphs. But user can specify also exactly which plots should be included. For example if plot_types==c('Norm.plot','Unif.plot') R should only plot numbers from normal and uniform distribution omitting numbers from exponential distribution.
My work so far
I wrote code following :
library(ggplot2)
library(gridExtra)
visual=function(num,plot_types='all'){
Norm.plot <- ggplot()+
geom_line(aes(x=1:num,y=rnorm(num)))+
labs(title='Norm plot',x=NULL,y=NULL)+
theme(plot.title = element_text(hjust = 0.5))
Unif.plot <- ggplot()+
geom_line(aes(x=1:num,y=runif(num)))+
labs(title='Unif plot',x=NULL,y=NULL)+
theme(plot.title = element_text(hjust = 0.5))
Exp.plot <- ggplot()+
geom_line(aes(x=1:num,y=rexp(num)))+
labs(title='Exp plot',x=NULL,y=NULL)+
theme(plot.title = element_text(hjust = 0.5))
if (plot_types=='all'){grid.arrange(Norm.plot,Exp.plot, Unif.plot,ncol=2)}
else if (plot_types==c('Norm.plot','Unif.plot') ||
plot_types==c('Unif.plot','Norm.plot')){grid.arrange(Norm.plot, Unif.plot)}
else if (plot_types==c('Norm.plot','Exp.plot') ||
plot_types==c('Exp.plot','Norm.plot')){grid.arrange(Norm.plot, Exp.plot)}
else if (plot_types==c('Unif.plot','Exp.plot') ||
plot_types==c('Exp.plot','Unif.plot')){grid.arrange(Exp.plot, Unif.plot)}
}
visual(50,plot_types = c('Norm.plot','Unif.plot'))
The code above has several problems. The first one is that it has a lot of loops so it's very unproductive. Also it would be very problematic when trying to extend it to higher numbers of variables.
Do you have any ideas how can I omit using so many loops ?

