corr <- function(diagr){
 if(diagr %in% 1:2) {     
  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) < 2 | any(is.na(x)) | any(is.na(y)))) {
  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"}
  kenget <- bquote(bar(x) == .(gemx) ~ " "~ bar(y) == .(gemy) ~ " | "~ s[x] == .(stafwx) ~ " "~ s[y] == .(stafwy) ~" | "~ r == .(txtcor)) 
  mdat <- read_excel(excelfile, range = cell_cols("C"))
  lbl <- mdat |> pull(lbl)
  if(is.na(lbl[1])) { lbl[1] <- "x" } else {lbl[1] <- paste("x =", lbl[1])}
  if(is.na(lbl[2])) { lbl[2] <- "y" } else {lbl[2] <- paste("y =", lbl[2])}
  if(is.na(lbl[3])) { lbl[3] <- "" } else {lbl[3] <- lbl[3]}  
  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
  df <- data.frame(x, y)
 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( 
        aspect.ratio = 1,
        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) ) +
        geom_point(size = 1 ) 
 if(diagr == 1) {
   p <- p + labs(caption = kenget) +
     theme(plot.caption = element_text(size = 9, hjust=0.5) )
   p 
 }else {
 if(sd(x) != 0 & sd(y) != 0 & cor(x,y) !=0 ) {
     atr8 <- sign(cor(x, y)) * sd(y) / sd(x)
     btr8 <- mean(y) - atr8 * mean(x)
     atr <- round(atr8, 2)
     btr <- round(btr8, 2)
     if(btr > 0) {
       lbltr <- paste("typ. rechte y = ", as.character(atr), "x +" ,  as.character(btr))
       if(as.character(atr)==1){lbltr <- paste("typ. rechte y = x +" , as.character(btr))}
       if(as.character(atr)==-1){lbltr <- paste("typ. rechte y = -x +" , as.character(btr))} }
     if(btr < 0) {
       lbltr <- paste("typ. rechte y = ", as.character(atr), "x -" ,  as.character(abs(btr)))
       if(as.character(atr)==1){lbltr <- paste("typ. rechte y = x -" , as.character(abs(btr)))}
       if(as.character(atr)==-1){lbltr <- paste("typ. rechte y = -x -" , as.character(abs(btr)))} }
     if(btr == 0) {
       lbltr <- paste("typ. rechte y = ", as.character(atr), "x" )
       if(as.character(atr)==1){lbltr <- paste("typ. rechte y = x")}
       if(as.character(atr)==-1){lbltr <- paste("typ. rechte y = -x")} } 
   lblcap <-  bquote(bar(x) == .(gemx) ~ " "~ bar(y) == .(gemy) ~ " | " ~ r == .(txtcor) ~" | "~ .(lbltr)     ) 
       p <- p + labs(caption = lblcap) +
         geom_abline(slope = atr8, intercept = btr8, linetype= 2)  +
         theme(plot.caption = element_text(size = 9, hjust=0.5) )
     p  
  } else {
     cat("de standaardafwijking van x of y is gelijk aan nul \nof de correlatiecoëfficiënt is gelijk aan nul \npas de data aan in uw Excel bestand \nof werk alleen met het spreidingsdiagram (diagr <- 1)") }  }
 } else {
    cat("de kolommen met x-data en met y-data \nzijn niet even lang \nof bevatten lege cellen \nof er is maar 1 datapunt \npas de data aan in uw Excel bestand")  } 
 } else {
   cat("vul de juiste code in : \ndiagr <-... (ofwel 1 ofwel 2)")  }  
} 