0

I have three ggplots : Simple dotplot, ACF, quantile-quntile plot. I want to write a function which has certain features. It should by default plot all three plots, but it should have input plot_types and there I can specify which exactly plots should be drew. So my function should have three inputs : First should be a vector, second one which plots should be drew and third one - maximum number of lags in ACF (this variable should be only important when I specify ACF in second input). For example

visualise(rnorm(100),plot_types=c('dotplot','ACF'),lag_max=30) #it should plot only two graphs, 

                                                               simple dotplot and ACF with maximum

                                                               number of lags equals to 30. 


visualise(runif(100),plot_types=c('dotplot','qq-plot')) #it should plot dotplot and qqplot. Third input is not 
                      
                                                        needed because we do not need maximum number
                         
                                                        of lags regarding to not specified ACF in input.

My research

#QQ plot 

qqplot_data <- function(vec){
  ggplot() + geom_qq(aes(sample = vec))+geom_abline()
}


#ACF
acf_plot<-function(vec,lag_max){
  #calculate acf values
  val_acf <- acf(vec, plot = FALSE,lag.max = lag_max)
  #create data frame to use ggplot
  df <- with(val_acf, data.frame(lag, acf))
  #plot acf 
  ggplot(data = df, mapping = aes(x = lag, y = acf)) +
    geom_hline(aes(yintercept = 0)) +
    geom_segment(mapping = aes(xend = lag, yend = 0))+
    scale_y_continuous(breaks=round(c(-1,-0.75,-0.5,-0.25,0.25,0.50,0.75),digits=2))
}


#Simple dotplot 
dot_plot<-function(vec){
  ggplot()+aes(x=1:length(vec),y=vec)+geom_point()
}



visualise<-function(vec,plot_types='all',lag_max){
  #creating list of plots
  plot.list<-list(dot_plot(vec),qqplot_data(vec),
                  acf_plot(vec,lag_max))
  #turning off appearance each of plot
  show.plot <- c(FALSE, FALSE, FALSE)
  
  #Turning on visualisation of all plots when plot_types is set to 'all'
  if('all' %in% plot_types) {
    show.plot <- c(TRUE, TRUE, TRUE)
  } else {
    #Turning on appearance of given plots specified in plot_types
    if('dot_plot' %in% plot_types) show.plot[1] <- TRUE
    if('qq_plot' %in% plot_types) show.plot[2] <- TRUE
    if( 'ACF' %in% plot_types) show.plot[3] <- TRUE
  }
  
  plot_grid(plotlist = plot.list[show.plot])
}


#Examples 

visualise(vec=rnorm(100),lag_max=30) #All plots 
visualise(vec=rnorm(100),plot_types=c('ACF','dot_plot'),lag_max=30) #Plots ACF and dot_plot 

The problem is when I'm trying to run code visualise(vec=rnorm(100),plot_types=c('dot_plot','qq_plot')) I get information that lag_max is missing with no default but it's not relevant in terms of not specifying ACF in second input. I tried to solve this, of course it's can be done very easily by specifying certain value for lag_max at input level (for instance visualise<-function(vec,plot_types,lag_max=20){...)) But I'm looking for solution which do not include specifying lag_max by default. Do you have any idea how it can be solved ?

John
  • 1,849
  • 2
  • 13
  • 23
  • the problem seems to be that you are making all plots and then selecting which you want to plot. It would be better to make only the plots you want. Basically put the plot generation into the if statements – Richard Telford Sep 15 '20 at 08:44
  • I'm not sure what you mean by putting plot generation into if statements. Can you please extend that ? – John Sep 15 '20 at 08:54
  • Side issue: as written your code passes in `vec` to the function `visualise()`, but not `residuals` which is one of the named arguments, you'll need both for it to run correctly. – Miff Sep 15 '20 at 08:57
  • Yea, you're right. I've changed all residuals to vec – John Sep 15 '20 at 08:59

0 Answers0