Integrate a new lm into an existing lm plot

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<- '/les_3-cohortes/Paris/global'  #faire 1dossier par score 
    graphe<- '/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.

Hello,
You have chosen quite a complex way to achieve your graphs. Unfortunately your ggplot code is not a true reprex (reproducible example) since you don't provide any data files.
However, if I understand correctly then the following code may produce what you want.
Where I have made changes are highlighted by a comment line starting with #***
Note that the ggplot pipe (or continuation symbol), '+', must be at the end of a line, not the beginning.

library(devtools)
library(ggplot2)
library(easyGgplot2)

#----- Your user-defined function to do plotting
#*** Pass two arguments to the function; the second is the ASM fit
ggplotRegression <- function (fit, fit_ASM=fit_ASM) {
      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))) +

        #*** Add the second regression
        geom_smooth(data=fit_ASM$model,
                    aes_string(x = names(fit_ASM$model)[2], y = names(fit_ASM$model)[1]),
                    color="blue",
                    method="lm")
    }


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)

  #**** Make the AMS regression for this subset of the data
  fit_AMS <- lm(AMS ~ score, data = fichier, na.action=na.omit)

  #*** Now passing two arguments to each call of ggplotRegression()
  #*** Second one is the default "fit_ASM"
  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)
}

There is redundant code in this approach because each regression calculation is being done twice.
Anyway, hopefully it works.

1 Like

This topic was automatically closed 21 days after the last reply. New replies are no longer allowed.