# Plotting two densities on the same graph in R

I generated two density functions in R and trying to plot them on the same graph, but for some reason I cannot see the full plots. Here is the R code:

v3 <- rt(100000, 1)/sqrt(3-2)
w3 <- rchisq(100000,2)
z3 <- rnorm(n=100000, m=0, sd=1)
z_eff_3 <- v3 + z3 * sqrt((3*(1+v3*v3))/w3)
plot(density(z_eff_3,from=0,to=5),xlim=c(0,5))

v4 <- rt(100000, 1)/sqrt(4-2)
w4 <- rchisq(100000,3)
z4 <- rnorm(n=100000, m=0, sd=1)
z_eff_4 <- v4 + z4 * sqrt((4*(1+v4*v4))/w4)

lines(density(z_eff_4,from=0,to=5),xlim=c(0,5),col ="blue")


Do you guys know how to fix this?

-

The parameters xlim and ylim are used to set the plot window. As you already give a range (but obviously not the good one) to density, xlim is useless. See below the corrected code.

v3 <- rt(100000, 1)/sqrt(3-2)
w3 <- rchisq(100000,2)
z3 <- rnorm(n=100000, m=0, sd=1)
z_eff_3 <- v3 + z3 * sqrt((3*(1+v3*v3))/w3)
plot(density(z_eff_3,from=-10,to=10),ylim=c(0,0.2))

v4 <- rt(100000, 1)/sqrt(4-2)
w4 <- rchisq(100000,3)
z4 <- rnorm(n=100000, m=0, sd=1)
z_eff_4 <- v4 + z4 * sqrt((4*(1+v4*v4))/w4)

lines(density(z_eff_4,from=-10,to=10), col ="blue")

-