mercredi 31 mars 2021

Conditioning color of barblot - r

I want to color in red the bar in position multiple of some period (s) in R. I have tried with lm=60 and s=12 and it works, but I don' t understand why it doesn't work with other different lag max (lm).

correlogrammi_mod_season<-function(dati, lm=36, s=12) # plotta i due corr insieme nello stesso plot
{                                                     # e distingue i multipli  di s 
  n.obs<-length(dati)
  par(mfrow=c(1,2))
  acfs <- acf(dati, lag.max = lm, plot = F)
  m.acfs <- max(abs(acfs$acf[-1])) + 0.1
  pacfs <- pacf(dati, lag.max = lm, plot = F)
  m.pacfs <- max(abs(pacfs$acf)) + 0.1
  
  #plot autocorrelazione globale
  id_color<-0:lm    # here I initialized a list to choose which bar to color in red
  #works fine only with lm=60
  barplot(rev(as.matrix(acfs$acf[-1])), beside = T, col=ifelse(id_color%%s == 0,"red","yellow"), horiz = T, xlim = c(-m.acfs,m.acfs), 
          main= "sample ACF", ylab="", cex.names = 0.9, names.arg = rev(seq(1:lm)))
  abline(v=0)
  abline(v=c(-1.96/n.obs^(1/2), 1.96/n.obs^(1/2)), lty=2)
  
  #plot autocorrelazione parziale                              # here' s the condition
  barplot(rev(as.matrix(pacfs$acf)), beside = T, col=ifelse(id_color%%s == 0,"red","yellow"), horiz = T, xlim = c(-m.pacfs, m.pacfs),
          main= "sample PACF", ylab= "", cex.names = 0.9, names.arg = rev(seq(1:lm)))
  abline(v=0)
  abline(v=c(-1.96/n.obs^(1/2), 1.96/n.obs^(1/2)), lty=2)
  
  par(mfrow=c(1,1))
}

Below an output when it works (only one time sadly, with lm=60 and s=12) enter image description here

Aucun commentaire:

Enregistrer un commentaire