Search code examples
rstatisticscurveprobability-density

Plot density curve in R with two density functions


I am struggling to create a continuous density curve in R for the below pdf:

{ 3x(1-x)^2     0 < x < 1

f(x) = { 3(2-x)(1-x)^2  1 ≤ x  < 2

{ 0              otherwise

Using the curve function, I can plot the two functions separately but not together:

curve(3*x*(1-x)^2,0,1)

curve(3*(2-x)*(1-x)^2,1,2)

Any help would be appreciated?


Solution

  • You can start with an empty graph and then put together these pieces with add=TRUE.

    plot(NULL, xlim=c(0,2), ylim=c(0,0.5))
    curve(3*x*(1-x)^2,0,1, add=TRUE)
    curve(3*(2-x)*(1-x)^2,1,2, add=TRUE)
    

    Two curves