reg <- function(respons, regr, x_waarde = 0){
if(respons %in% 1:2 & (regr %in% 1:2 | regr == 2000 | regr == 9000)) {   
    library(ggplot2) 
    library(readxl) 
    library(dplyr, warn.conflicts = FALSE)
    excelfile <- "xcl/mijndata.xlsx"
    mdat <- read_excel(excelfile, range = cell_cols("A:B"))
    x <- mdat |> pull(x)
    y <- mdat |> pull(y)
 if(!(length(x) == 0  | any(is.na(x)) | any(is.na(y)))) {
      mdat <- read_excel(excelfile, range = cell_cols("C"))
      lbl <- mdat |> pull(lbl)
      if(is.na(lbl[1])) { lbl[1] <- "" } else {lbl[1] <- lbl[1]}
      if(is.na(lbl[2])) { lbl[2] <- "" } else {lbl[2] <- lbl[2]}
      if(is.na(lbl[3])) { lbl[3] <- "" } else {lbl[3] <- lbl[3]}
      if(respons == 1) {zz <- x
      x <- y
      y <- zz
      zz <- lbl[1]
      lbl[1] <- lbl[2]
      lbl[2] <- zz }
      if(lbl[1]=="") { lbl[1] <- "x" }else{lbl[1] <- paste("x =", lbl[1])}
      if(lbl[2]=="") { lbl9000 <- "" }else{lbl9000 <- paste("(", lbl[2] ,")")}      
      if(lbl[2]=="") { lbl[2] <- "y" }else{lbl[2] <- paste("y =", lbl[2])}
  if(sd(x) != 0) {
      df <- data.frame(x, y)      
      xmin <- min(x) - (max(x)-min(x))*0.1
      xmax <- max(x) + (max(x)-min(x))*0.1
      ymin <- min(y) - (max(y)-min(y))*0.1
      ymax <- max(y) + (max(y)-min(y))*0.1
    p <- ggplot(df, aes(x, y)) +
        coord_cartesian (xlim= c(xmin,xmax), ylim= c(ymin,ymax), expand= FALSE ) +
        labs(title = lbl[3],  x = lbl[1], y = lbl[2]) +
        theme( plot.title = element_text(size = 15, hjust=0.5) ,
          axis.title.x = element_text(hjust = 0.5),
          axis.title.y = element_text(vjust = 0.5) ,
          aspect.ratio = 1 ) +
        geom_point(size = 1) 
      gemx <- round(mean(x) , 2)
      gemy <- round(mean(y) , 2)
      stafwx <- round(sd(x) , 2)
      stafwy <- round(sd(y) , 2)
      if (sd(x)!=0 & sd(y)!=0) {txtcor <- as.character(round(cor(x,y),2))
      }else{ txtcor <-"N/A"}
      capreg <- bquote(bar(x) == .(gemx) ~ " "~ bar(y) == .(gemy) ~ " | "~ s[x] == .(stafwx) ~ " "~ s[y] == .(stafwy) ~" | "~ r == .(txtcor))       
   if (regr == 1) {
          p1 <- p + labs(caption = capreg) +
            theme(plot.caption = element_text(size = 9, hjust=0.5) )
          return(p1) 
   }else {     
        regmod <- lm(y~x, data = df)
        atr8 <- as.numeric(regmod$coef[2])
        btr8 <- as.numeric(regmod$coef[1])
       p <- p + geom_abline(slope = atr8, intercept = btr8, linetype= 1,  color ="blue")
        atr <- round(atr8, 2)
        btr <- round(btr8, 2)
        if(!atr == 0){
          if(btr > 0) {
            vgl <- paste(" =", as.character(atr), "x +" ,  as.character(btr)) 
            if(as.character(atr)==1){vgl <- paste(" = x +" , as.character(btr)) }
            if(as.character(atr)==-1){vgl <- paste(" = - x +" , as.character(btr))} }
          if(btr < 0) {
            vgl <- paste(" =", as.character(atr), "x -" ,  as.character(abs(btr)))   
            if(as.character(atr)==1){vgl <- paste(" = x -" , as.character(abs(btr)))}
            if(as.character(atr)==-1){vgl <- paste(" = - x -", as.character(abs(btr)))}}
          if(btr == 0) {
            vgl <- paste(" =", as.character(atr), "x" )
            if(as.character(atr)==1){vgl <- paste(" = x")}
            if(as.character(atr)==-1){vgl <- paste(" = - x")}} }   
        if(atr == 0) {
          vgl <- paste(" =", as.character(btr))}   
   if(regr == 2) {
       capreg <-  bquote(bar(x) == .(gemx) ~" "~ bar(y) == .(gemy) ~ " | " ~ r == .(txtcor) ~" | "~  "regr " ~ hat(y) ~ .(vgl))
      p2 <- p + labs(caption = capreg) +
        theme(plot.caption = element_text(size = 9, hjust=0.5) )
      return(p2)    }   
   if(regr == 2000) {
         yhoed <- round(x_waarde * atr8 + btr8 , 2)
         bijx <- paste("x =", as.character(round(x_waarde , 2)), "➔ ")
         capreg <- bquote(r == .(txtcor) ~" | "~ "regr " ~ hat(y) ~ .(vgl) ~ " | " ~ .(bijx) ~ hat(y) == .(yhoed) ) 
      p2000 <- p + labs( caption = capreg ) +         
         theme( plot.caption = element_text(size = 9, hjust=0.5) )
      return(p2000)   }   
   if(regr == 9000) {    
        res <- regmod$residual
        df9000 <- data.frame(x, res)      
        resmin <- min(res) - (max(res)-min(res))*0.1
        resmax <- max(res) + (max(res)-min(res))*0.1
        lblres <- paste("e = residu" , lbl9000)
      p9000 <- ggplot(df9000, aes(x, res)) +
          coord_cartesian (xlim= c(xmin,xmax), ylim= c(resmin,resmax), expand= FALSE ) +
          labs(title = lbl[3],  x = lbl[1], y = lblres) +
          theme( plot.title = element_text(size = 15, hjust=0.5) ,
                 axis.title.x = element_text(hjust = 0.5),
                 axis.title.y = element_text(vjust = 0.5) ,
                 aspect.ratio = 1 ) +
          geom_point(size = 1, color = "red") +
          geom_abline(slope = 0, intercept = 0, size = 0.25)
      return(p9000)    }
  } 
      } else {
        cat("de standaardafwijking van de verklarende veranderlijke is gelijk aan nul \npas de data aan in uw Excel bestand")   }  
    } else {
      cat("de kolommen met x-data en met y-data \nzijn niet even lang of bevatten lege cellen \npas de data aan in uw Excel bestand")   }  
  } else {
    cat("vul de juiste codes in : \nrespons <-... (ofwel 1 ofwel 2) \nregr <-... (ofwel 1 ofwel 2)")  }  
}  