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

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.

enter image description here


1 Answer 1


First, you need to put the data into a sensible form for ggplot2:

dat <- data.frame(item=factor(rep(1:10,15)), 

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):

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"))

Output: Plot of Dirichlet draws

  • 2
    $\begingroup$ (+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) $\endgroup$
    – cardinal
    Commented Sep 5, 2011 at 13:53
  • $\begingroup$ @cardinal If you can post more explanation on why the above computation is equivalent to rdirichlet, it would be great. $\endgroup$
    – Anand
    Commented Sep 6, 2011 at 17:17
  • 3
    $\begingroup$ @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. $\endgroup$
    – cardinal
    Commented Sep 6, 2011 at 22:47
  • $\begingroup$ @Anand: My pleasure. $\endgroup$
    – cardinal
    Commented Sep 8, 2011 at 19:41

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.