i've got a function setup to plot the area under a curve of a gaussian distribution and i'd like to be able to highlight two areas on the same plot. In this case it should be mirrored.
areaplotter <- function(x1,x2){
x <- seq(from=-4,to=4,len=100)
plot(x,dnorm(x),type='l')
jj <- seq(from=x1,to=x2,len=100)
polygon(x=c(jj,x2,x1),y=c(dnorm(jj),0,0),col='red')
}
Essentially i'd like to get the two following plots in one
areaplotter(-4,qnorm(0.05))
areaplotter(qnorm(1-0.05),4)
Right now i managed to put them next to each other with par(mfrow = c(1,2))
but i want the shaded areas to appear on one plot
Is there a simple syntax or is there another funtion i should use? thanks in advance.
I tried using the lines()
function but I'm new to R and it didnt work so i might done it wrong
I simplified the function so that it only needs the alpha level.
areaplotter <- function(alpha = 0.05){
x <- seq(from = -4, to = 4, len = 100)
plot(x, dnorm(x), type = 'l')
x1 <- qnorm(alpha)
x2 <- qnorm(1 - alpha)
jj1 <- seq(from = x[1], to = x1, len = 100)
jj2 <- seq(from = x2, to = x[100], len = 100)
polygon(x = c(jj1, x1, x[1]), y = c(dnorm(jj1), 0, 0), col = 'red')
polygon(x = c(jj2, x[100], x2), y = c(dnorm(jj2), 0, 0), col = 'red')
}
areaplotter()
areaplotter(0.1)