corr_z <- function(){
  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)))) {
    if(!(length(x) == 1  | sd(x) == 0 | sd(y) == 0)) {
      r <- round(cor(x,y) , 2)
      x <- as.numeric(scale(x))
      y <- as.numeric(scale(y))
      ticks <- max(abs(min(x)), max(x), abs(min(y)), max(y))
      ticks <- (ceiling(round(ticks, 1) * 2)) / 2
      xyticks <- round(seq(-ticks, ticks, 0.5) , 1)
      mdat <- read_excel(excelfile, range = cell_cols("C"))
      lbl <- mdat |> pull(lbl)
      if(is.na(lbl[3])) { lbl[3] <- "" } else {lbl[3] <- lbl[3]}
      df <- data.frame(x, y)
      ggplot(df, aes(x, y)) +
        coord_fixed(ratio = 1, xlim = c(-ticks, ticks) , ylim =  c(-ticks,  ticks)) +
        labs(title = lbl[3] , subtitle = bquote("gestandaardiseerd spreidingsdiagram " ~ (r == .(r))), x = bquote(~ z[x]) , y = bquote(~ z[y]) ) + 
        scale_x_continuous(breaks = xyticks) +
        scale_y_continuous(breaks = xyticks) +  
        theme( 
          plot.title = element_text(size = 15, hjust=0.5) ,
          plot.subtitle = element_text(size = 12, hjust=0.5) ,
          axis.title.x = element_text(hjust = 0.5),
          axis.title.y = element_text(angle = 0 , vjust = 0.5) ) +
        geom_point(size = 1) +
        geom_segment(x=0, y=-ticks, xend=0, yend=ticks,  linetype = 2, linewidth = 0.3) +
        geom_segment(x=-ticks, y=0, xend=ticks, yend=0,  linetype = 2, linewidth = 0.3) 
    } else {
      cat("de standaard afwijking van de x- of y-data \nis gelijk aan nul of is niet gedefinieerd \npas de data aan in uw Excel bestand mijndata.xlsx") }
  } else {
    cat("de kolommen met x-data en met y-data \nzijn niet even lang of bevatten lege cellen \npas de gegevens aan in uw Excel bestand mijndata.xlsx")   } 
} 