Search code examples
rplotline

how to plot this figure in r


I'm trying to plot this figure in r, how can I add the lines and colors? enter image description here

x=seq(0,1,0.01)
z1=dbeta(x,1,1)
y=1
n=3
z2=dbeta(x,1+y,1+n-y)
m1=data.frame(cbind(z2,z1))
matplot(x,m1,type="l",lwd=2,cex.main=1,col=c("black","black"))

Solution

  • You can add the area under the density curve with the addition of polygons. The boundaries are dependent on x in your seq (the desired interval in x) as well as m1$z2 values when x is between the selected interval of x.

    polygon(c(.3, x[x >= .3 & x <= .4], .4), 
            c(0, m1$z2[x >= .3 & x <= .4], 0), col="red")
    polygon(c(.2, x[x >= .2 & x <= .3], .3), 
            c(0, m1$z2[x >= .2 & x <= .3], 0), col="light blue")
    

    selected areas filled in plot