norm_krpt <- function(gem,st_afw,lstaartkans){
if( 0<=lstaartkans & lstaartkans<=1 & st_afw>0 & st_afw <Inf & -Inf<gem & gem<Inf) {
  library(ggplot2) 
  xlow <- gem - 4*st_afw
  xhigh <- gem + 4*st_afw
  ymax <- 1/(st_afw*(sqrt(2*pi)))
  b <- qnorm(lstaartkans,gem,st_afw)
  gemnew <- round(gem, 2)
  st_afwnew <- round(st_afw, 2)
  if(gem == 0 & st_afw == 1) {
    normtit <- "STANDAARD NORMALE \nmet"
    xlabel <- "z"
    ylabel <- "f(z)"
    intval <- "GEVRAAGD: linkerstaart \nvan  -∞  tot kritisch punt z"
    btext <- paste("[ z = ", as.character(round(b, 2)), "]")
    if(abs(b)>=1.6447 & abs(b)<=1.6449) {
      btext <- paste("[ z = ", as.character(round(b, 3)), "]")}
  }else{ 
    normtit <- "NORMAAL KANSMODEL \nmet"
    xlabel <- "x"
    ylabel <- "y"
    intval <- "GEVRAAGD: linkerstaart \nvan  -∞  tot kritisch punt x"
    btext <- paste("[ x = ", as.character(round(b, 2)), "]")
  }    
  if (b < xlow){
    bnew <- xlow
    tkx <- gemnew
  } else if (b > xhigh){
    bnew <- xhigh
    tkx <- gemnew
  } else {
    bnew <- b
    tkx <- c(round(b, 2), gemnew)
    if(abs(b)>=1.6447 & abs(b)<=1.6449) {tkx <- c(round(b, 3), gemnew)}
  }
  tky <- c(0, round(1/(st_afw*sqrt(2*(pi)*exp(1))),3), round(ymax, 3))
  ylabels <- c(0, round(1/(st_afw*sqrt(2*(pi)*exp(1))),2), round(ymax, 2))
  myfun <- function(x) {y<-dnorm(x, mean = gem, sd = st_afw)}
  oppvl <- paste("GEGEVEN: kans = \noppervlakte boven \nlinkerstaart = ", as.character(round(lstaartkans,3)))
  ggplot(data.frame(x = c(xlow, xhigh)), aes(x)) + 
    coord_cartesian(expand = FALSE, xlim = c(xlow, xhigh), ylim = c(0, 1.1*ymax)) +     
    stat_function(fun = myfun, geom = "area", xlim = c(xlow, bnew), fill = "blue",alpha = 0.3) +
    geom_segment(aes(x=xlow, y=0, xend=bnew, yend=0),color = "red", linewidth = 2) +
    stat_function(fun = myfun) +
    annotate("text", x = gem - 2.4*st_afw, y =0.95*ymax , size = 4.5, label = normtit) +
    annotate("text", x = gem - 2.4*st_afw, y =0.855*ymax , size = 4.5, label = bquote( ~ mu == .(gemnew) ~ "en" ~ sigma ==.(st_afwnew)) , color = "black") +
    annotate("text", x = gem - 2.6*st_afw, y = 0.56*ymax, label = intval,  size = 4.5, color = "red") +
    annotate("text", x = gem - 2.6*st_afw, y = 0.47*ymax, label = btext,  size = 4.5, color = "grey40") +
    annotate("text", x = gem - 2.78*st_afw, y = 0.3*ymax, label = oppvl,  size = 4.5, color = "blue") +
    labs(x = xlabel, y = ylabel) +
    scale_x_continuous(breaks = tkx) +
    scale_y_continuous(breaks = tky, labels = ylabels) +   
    theme( 
      aspect.ratio = 0.70 ,
      axis.title.x = element_text(hjust = 1),
      axis.line.x = element_line(arrow= arrow(angle= 20, length= unit(0.4, "cm"))),
      axis.title.y = element_text(vjust = 1, angle =0))
} else {
  cat("controleer of de getallen die \nje hebt ingevuld voldoen aan \n∙ - ∞ < gem < + ∞  \n∙ 0 < st_afw < + ∞ \n∙ en bovendien 0 ≤ lstaartkans ≤ 1 ")
}
}