norm_kans <- function(gem,st_afw,a,b){
if( b>a & st_afw>0 & st_afw <Inf & -Inf<gem & gem<Inf) {
  library(ggplot2) 
  gemnew <- round(gem, 2)
  st_afwnew <- round(st_afw, 2)
  xlow <- gem - 4*st_afw
  xhigh <- gem + 4*st_afw
  ymax <- 1/(st_afw*(sqrt(2*pi)))
  tkx <- round(seq(from = xlow, to = xhigh, by = st_afw), 2)
  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)}
  anew <- max(a, xlow)
  bnew <- min(b, xhigh)
  if(a>xhigh | b<xlow) {
    oppvl <- "[= 0 ]"
  } else {
    oppvl <- integrate(myfun, lower = anew, upper = bnew)
    oppvl <- paste("[=", as.character(round(oppvl$value,3)), "]" ) }
  atext <- paste("a =", as.character(round(a, 3)))
  if(a==-Inf){atext = "a =  - ∞"}
  btext <- paste("b =", as.character(round(b, 3)))
  if(b==Inf){btext = "b =  + ∞"}
  intval <- paste("GEGEVEN \ninterval van a tot b met \n",  atext, " en ", btext)
  if(gem == 0 & st_afw == 1) {
    normtit <- "STANDAARD NORMALE \nmet"
    xlabel <- "z"
    ylabel <- "f(z)"  
  }else{ 
    normtit <- "NORMAAL KANSMODEL \nmet"
    xlabel <- "x"
    ylabel <- "y"
  }
  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(anew, bnew), fill = "blue",alpha = 0.3) +
    geom_segment(aes(x=anew, 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.55*ymax, label = intval,  size = 4.5, color = "red") +
    annotate("text", x = gem - 2.85*st_afw, y = 0.3*ymax, label = "GEVRAAGD \nkans in interval = \noppervlakte",  size = 4.5, color = "blue") +
    annotate("text", x = gem - 2.85*st_afw, y = 0.16*ymax, label = oppvl,  size = 4.5, color = "grey40") +
    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 a < b ")
}
}