Hello,
I use an R code to create linear regression plots of one of my variables for each of my files.
My code works very well but I would like to add a new model (in other words, I would like to show a second curve in blue for example).
here is the code used to form my simple single curve regression plot:
work<- '/Volumes/les_3-cohortes/Paris/global' #faire 1dossier par score
graphe<- '/Volumes/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)
}
Here is an example of a file:
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 obtained :
I have therefore performed for each EGFR variable an lm with score but now I would like to add on the same plot a blue lm representing the regression of AMS with each EGFR variable.
I tried for each variable to create a model1 of lm(model1 <- lm(EGFR_12 ~ AMS, data = file, na.action=na.omit)
and add it via the ggplotRegression function:
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")
}
Will you be able to help me?
Thank you. Thank you.