简体   繁体   中英

How to plot density plots with proportions on the y-axis?

I am using the sm package in R to draw a density plot of several variables with different sample sizes, like this:

var1 <- density(vars1[,1]) 
var2 <- density(vars2[,1]) 
var3 <- density(vars3[,1]) 

pdf(file="density.pdf",width=8.5,height=8)
plot(var1,col="BLUE")  
par(new=T)
plot(var2,axes=FALSE,col="RED")  
par(new=T)
plot(var3,axes=FALSE,col="GREEN")  
dev.off()

The problem I'm having, is that I want the y-axis to show the proportions so I can compare the different variables with each other in a more meaningful way. The maxima of all three density plots are now exactly the same, and I'm pretty sure that they wouldn't be if the y-axis showed proportions. Any suggestions? Many thanks!

Edit:

I just learned that I should not plot on top of an existing plot, so now the plotting part of the code looks like this:

pdf(file="density.pdf",width=8.5,height=8)
plot(var1,col="BLUE")  
lines(var2,col="RED")  
lines(var3,col="GREEN")  
dev.off()

The maxima of those lines however are now very much in line with the sample size differences. Is there a way to put the proportions on the y-axis for all three variables, so the area under the curve is equal for all three variables? Many thanks!

Don't plot on top of an existing plot, because they axes may be different. Instead, use lines() to plot the second and third densities after plotting the first. If necessary, adjust the ylim parameter in plot() so that they all fit.

An example for how sample size ought not matter:

    set.seed(1)
    D1 <- density(rnorm(1000))
    D2 <- density(rnorm(10000))
    D3 <- density(rnorm(100000))
    plot(D1$x,D1$y,type='l',col="red",ylim=c(0,.45))
    lines(D2$x,D2$y,lty=2,col="blue")
    lines(D3$x,D3$y,lty=3,col="green")

在此处输入图片说明

You could make tim's solution a little more flexible by not hard-coding in the limits.

plot(D1$x,D1$y,type='l',col="red",ylim=c(0, max(sapply(list(D1, D2, D3),
    function(x) {max(x$y)}))))

This would also cater for Vincent's point that the density functions are not necessarily constrained in their range.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM