comparisonGraph = function(emmeansObj, prefix="hypothèse", groupe=T){
  letterComparison = cld(emmeansObj, Letters = LETTERS)
  var = colnames(letterComparison)[1:which(colnames(letterComparison)=="emmean")-1]
  letterComparison = letterComparison %>% arrange(desc(emmean))
  nom_col = paste(var, collapse = "+")
  
  if (length(var) > 1){
    letterComparison["var"] = apply(letterComparison[, var], 1, paste, collapse = "+")
  } else {
    letterComparison["var"] = letterComparison[var] 
  }
  
  letterComparison$var = factor(letterComparison$var, levels = letterComparison$var, labels = letterComparison$var)
  
  # Crée le graphique ggplot
  g = ggplot(letterComparison, aes_string(y = "emmean", x = "var")) + 
    geom_bar(stat = "identity") + 
    xlab(nom_col) + ylab("Moyenne ajustée") + 
    theme(axis.text.x = element_text(angle = 90))
  
  if (groupe == T){
    g = g + geom_text(aes(label = .group), position = position_stack(vjust = 0.5), col = "orange")
  }
  
  # Conversion du graphique ggplot en graphique Plotly
  graphique_comparaison = ggplotly(g)
  
  return(graphique_comparaison)
}

hypothesisGraph = function(modele, effets, export=F, prefix="hypothèse"){
  if(class(modele) == "lmerMod"){
    donnees = modele@frame
  }
  if(class(modele) == "lme"){
    donnees = modele$data
  }
  
  listes_res = list()
  donnees$residus = residuals(modele,type="pearson")
  donnees$fitted = fitted(modele)
  g_residus_complet = ggplot(donnees,aes(x=fitted, y=residus))
  graphe_residus_complet = g_residus_complet + geom_point()
  listes_res[['residus complets']] = graphe_residus_complet
  
  for(var_fixe in effets[effets%in%colnames(donnees)]){
    listes_res[[var_fixe]] = ggplot(donnees,aes_string(x=var_fixe, y="residus"))  + geom_point() + xlab(var_fixe)
  }
  if (export ==T){
    for (graph in names(listes_res)){
      png(filename = paste(prefix,graph,".png",collapse="_"), width = 11, height = 11, units = "cm", res=200)
      plot(listes_res[[graph]])
      dev.off()
    }
  }
  return(listes_res)
}