Mark
Mark

Reputation: 11

How do I plot multiple probability distributions side-by-side in R?

I want to plot several probability distributions side-ways (density on the x-axis, variable on y-axis). Each distribution will be associated with a different category, and I want them side-by-side so that I can compare between them. This is a bit like a box-plot but instead I want an theoretical probability distribution that I will specify giving parameters. So if they were all normal distributions, I would simply provide the mean and std deviation for each. Thanks.

Upvotes: 1

Views: 1329

Answers (2)

Seb
Seb

Reputation: 5507

do you mean something like this?

 x <- seq(-10, 10, length=100)
 normal.dist <- dnorm(x, 0, 2)
 f.dist <- df(x, 3, 4)
 t.dist <- dt(x, 3)
 chi.dist <- dchisq(x,3)
 par(mfrow=c(2,2))
 plot(x, normal.dist, type='l', lty=1 )
 plot(x, f.dist, type='l', lty=1, xlab="x value", col='blue')
 plot(x, t.dist, type='l', lty=1, xlab="x value", col='red')
 plot(x, chi.dist, type='l', lty=1, xlab="x value", col='green')

see also Roman Luštrik's very helpful link as well as the helfiles (e.g. ?dnorm).

enter image description here

Rotated axis

 x <- seq(-10, 10, length=100)
 normal.dist <- dnorm(x, 0, 1)
 normal.dist2 <- dnorm(x, 0, 2)
 normal.dist3 <- dnorm(x, 0, 3)
 normal.dist4 <- dnorm(x, 0, 4)


 par(mfrow=c(2,2))
 plot(normal.dist, x, type='l', lty=1 )
 plot(normal.dist2, x, type='l', lty=1, col='red' )
 plot(normal.dist3, x, type='l', lty=1, col='green' )
 plot(normal.dist4, x, type='l', lty=1, col='blue' )

enter image description here

Upvotes: 2

Marshall Shen
Marshall Shen

Reputation: 1333

You can set up a frame for plot display and specify how many plots you want to show in a frame using par(mfrow()), for example:

par(mfrow=c(2,2))
plot(first plot)
plot(second plot)
hist(third histogram)
boxplot(fourth boxplot)

See the following link for the full description: http://www.statmethods.net/advgraphs/layout.html

Upvotes: 1

Related Questions