Solved – Dirichlet distribution plot in R

dirichlet distributionggplot2r

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.

enter image description here

Best Answer

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

Output: Plot of Dirichlet draws

Related Question