Search code examples
rplotkernel-density

`plot.density` extends "xlim" beyond the range of my data. Why and how to fix it?


Using the code below, I am trying to get density plot for different distributions.

dens <- apply(df[,c(7,9,12,14,16,18)], 2, density)

plot(NA, xlim=range(sapply(dens, "[", "x")), ylim=range(sapply(dens, "[", "y")))
mapply(lines, dens, col=1:length(dens))

legend("topright", legend=names(dens), fill=1:length(dens),bty = "n",lwd=1, cex=0.7)

The maximum upper limit for all variables is 5. But I got lines exceeded the 5. What do I need to change in my code to fix the plot? enter image description here


Solution

  • By default, density will extend the range so that the density curve approaches 0 at the extreme. Do you want to restrict the curve to the range of you data? If so, you need use from and to arguments inside density().

    x_range <- range(df[,c(7,9,12,14,16,18)])
    dens <- apply(df[,c(7,9,12,14,16,18)], 2, density, from = x_range[1], to = x_range[2])
    

    Perhaps it is better to provide a reproducible example.

    set.seed(0); X <- matrix(rnorm(300), 100, 3)
    
    ## range of your data
    x_range <- range(X)
    # [1] -2.904899  2.658658
    
    ## default handling of `density`
    dens <- apply(X, 2, density)
    range(sapply(dens, "[[", "x"))
    # [1] -3.922346  3.696451
    

    enter image description here

    ## customized `from` and `to`
    dens <- apply(X, 2, density, from = x_range[1], to = x_range[2])
    range(sapply(dens, "[[", "x"))
    # [1] -2.904899  2.658658
    

    enter image description here