`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? By default, density will extend the range so that the density curve approaches 0 at the extreme. Do you want to restrict the