Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Plot multiple histograms in R with prob=TRUE

Tags:

r

I want to plot multiple histograms in R which do not show frequency, but the density instead:

A <- rnorm(100)
B <- rnorm(100)
hist1 <- hist(A,prob=TRUE,breaks=30)
hist2 <- hist(B,prob=TRUE,breaks=30) 
plot(hist1, col="red",lty=0, xlim=c(-4,4))
plot(hist2, col="blue", lty=0, xlim=c(-4,4), add=TRUE, main="Example")
lines(density(A))

However, my 'prob=TRUE' option apparently doesn't go through when plotting the objects. Can someone explain to me what I am doing wrong?

like image 217
user1814665 Avatar asked Mar 15 '26 07:03

user1814665


1 Answers

leave the prob=T out of the hist() command

hist1 <- hist(A,breaks=30)
hist2 <- hist(B,freq=F,breaks=30) 

And put freq=F into the plot command.

plot(hist1, col="red",lty=0, xlim=c(-4,4),freq=F)
plot(hist2, col="blue", lty=0, xlim=c(-4,4), add=TRUE, main="Example",freq=F)
like image 73
Seth Avatar answered Mar 16 '26 20:03

Seth



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!