Bonjour,

J'utilise un code R pour créer des plots de régression linéaire d'une de mes variable pour chacun de mes fichiers.

Mon code marche très bien mais je souhaiterai y rajouter un nouveau modèle (en d'autre terme, je souhaiterai faire apparaître une seconde courbe en bleu par exemple) .

voici le code utilisé pour former mes plot de régression simple à une courbe :

Code : Sélectionner tout - Visualiser dans une fenêtre à part
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
 
work<- '/Volumes/Store\ N\ Go/les_3-cohortes/Paris/global'  #faire 1dossier par score 
graphe<- '/Volumes/Store\ N\ Go/les_3-cohortes/Paris/global/graphe'
 
library(devtools)
library(ggplot2)
library(easyGgplot2)
 
ggplotRegression <- function (fit) {
 
  require(ggplot2)
 
  ggplot(fit$model, aes_string(x = names(fit$model)[2], y = names(fit$model)[1])) + 
    geom_point() +
    stat_smooth(method = "lm", col = "red") +
    labs(title = paste("R2 = ",signif(summary(fit)$adj.r.squared, 5),
                       " P =",signif(summary(fit)$coef[2,4], 5)))
}
 
 
setwd(work)
files <- list.files(path = "data", pattern = (".csv$"))
 
 
for (k in 1:length(files)) {
  fname <- files[k]
  cat(paste0("Now analyse data/", fname, "...\n"))
  fichier <- read.csv2(paste0("data/", fname), header = T, stringsAsFactors = F, dec = ",")
  setwd(graphe)  #faire 1dossier par fichier 
  a<- gsub(pattern = "\\.csv$", "", fname)
 
  fit1 <- lm(EGFR_12 ~ score, data = fichier, na.action=na.omit)
  p1<-ggplotRegression(fit1)
 
  fit2 <- lm(EGFR_24 ~ score, data = fichier, na.action=na.omit)
  p2<-ggplotRegression(fit2)
 
  fit3 <- lm(EGFR_36 ~ score, data = fichier, na.action=na.omit)
  p3<-ggplotRegression(fit3)
 
  jpeg(paste0(a, ".jpeg"), width = 40, height =12, units="cm", quality=100, res=300)
  p<- ggplot2.multiplot(p1,p2,p3, cols=3)
  print(p)
  dev.off()
 
 
  setwd(work)
}
Voici un exemple de fichier :

Code : Sélectionner tout - Visualiser dans une fenêtre à part
1
2
3
4
5
 
score;AMS;EGFR_12;EGFR_24;EGFR_36;Age_donneur;Paire
483;483;67,56217938;53,61312383;52,93430604;68;1
454;454;53,28459074;57,23583761;43,94840102;58;2
751;751;23,0301249;30,99633423;21,9535767;58;3
plot obtenu :
Nom : Paris_Alloscore_laurent_article_avechla.jpeg
Affichages : 333
Taille : 405,9 Ko

J'ai donc effectué pour chaque variable EGFR une lm avec score mais désormais je souhaiterai ajouter sur le même plot une lm en bleu représentant la régression de AMS avec chaque variables EGFR.
J'ai tenté pour chaque variables de créer un model1 de lm
Code : Sélectionner tout - Visualiser dans une fenêtre à part
(model1 <- lm(EGFR_12 ~ AMS, data = fichier, na.action=na.omit)
et de l'ajouter via la fonction ggplotRegression :

Code : Sélectionner tout - Visualiser dans une fenêtre à part
1
2
3
4
5
6
7
8
9
10
11
12
13
ggplotRegression <- function (fit) {
  require(ggplot2)
 
  ggplot(fit$model, aes_string(x = names(fit$model)[2], y = names(fit$model)[1])) + 
    geom_point() +
    stat_smooth(method = "lm", col = "red") +
    labs(title = paste("R2 = ",signif(summary(fit)$adj.r.squared, 5),
                       #"Intercept =",signif(fit$coef[[1]],5 ),
                       #" Slope =",signif(fit$coef[[2]], 5),
                       " P =",signif(summary(fit)$coef[2,4], 5)))
 
  + geom_line(data=pred(model1), color="blue") 
}
Vous serez t-il possible de m'aider ?

Merci