# Dirichlet distribution plot in R

I want to plot $15$ random draws of Dirichlet distribution with $\alpha = 1$ and dimension $n=10$ in R.

require(MCMCpack)
alpha <- 1
draws <- 15
dimen <- 10
x <- rdirichlet(draws, rep(alpha, dimen))


I want an output similar to the following image. This image is from Prof. David Blei's Topic Modeling tutorial at KDD 2011. I think it has been done using ggplot2 but I don't know how to generate it. Any help would be greatly appreciated. First, you need to put the data into a sensible form for ggplot2:

dat <- data.frame(item=factor(rep(1:10,15)),
draw=factor(rep(1:15,each=10)),
value=as.vector(t(x)))


Then you can plot it by building up the components you can see in the plot (points and lineranges; faceting, axis control and facet borders):

library(ggplot2)
ggplot(dat,aes(x=item,y=value,ymin=0,ymax=value)) +
geom_point(colour=I("blue"))       +
geom_linerange(colour=I("blue"))   +
facet_wrap(~draw,ncol=5)           +
scale_y_continuous(lim=c(0,1))     +
theme(panel.border = element_rect(fill=0, colour="black"))

• (+1) Some example output would be very nice. A Dirichlet sample of dimension $d$ and sample size $n$ with $\alpha=1$ can be easily generated as follows: pre.x <- matrix(rexp(n*d),n); x <- pre.x / rowSums(pre.x) – cardinal Sep 5 '11 at 13:53
• @cardinal If you can post more explanation on why the above computation is equivalent to rdirichlet, it would be great. – Anand Sep 6 '11 at 17:17
• @Anand, A standard method for sampling from a general $\mathrm{Dir}(\alpha_1,\ldots,\alpha_d)$ distribution is to generate independent $X_i \sim \Gamma(\alpha_i,1)$ random variables and then take $(X_1/S, \ldots, X_d/S)$ where $S = \sum_{i=1}^d X_i$. In the case where $\alpha_1 = \cdots = \alpha_d = 1$, then the $X_i$ are iid $\mathrm{Exp}(1)$ random variables since a $\Gamma(1,1)$ variate is equivalent to an $\mathrm{Exp}(1)$ one. – cardinal Sep 6 '11 at 22:47
• @Anand: My pleasure. – cardinal Sep 8 '11 at 19:41