Take the 2-minute tour ×
Cross Validated is a question and answer site for people interested in statistics, machine learning, data analysis, data mining, and data visualization. It's 100% free, no registration required.

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?

share|improve this question
add comment

1 Answer

up vote 4 down vote accepted

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")
share|improve this answer
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.