Skip to content

Instantly share code, notes, and snippets.

@gufodotto
Created May 16, 2012 17:12
Show Gist options
  • Save gufodotto/2712296 to your computer and use it in GitHub Desktop.
Save gufodotto/2712296 to your computer and use it in GitHub Desktop.
Vlaaivis.R
# My take on multi-variable pie charts. Inspired by:
# http://learnr.wordpress.com/2010/08/16/consultants-chart-in-ggplot2/
require(ggplot2)
require(reshape)
# let's create a dummy set
nvars<-3; varnames <- letters[1:nvars] # the number and names of the variables
ncpd<-16; cpdnames <- toupper(letters[1:ncpd]) # the number and name of item (in my case compounds)# a matrix filled with pseudorandom gibberish
MyMatrx<-matrix(ncol=nvars,nrow=ncpd,data=sample(5, repl=T, size=ncpd*nvars))
rownames(MyMatrx)<-cpdnames; colnames(MyMatrx)<-varnames
# alternatively, one could read such a data structure from a file...
# MyMatrx<-read.table(file="khuuhhl.uyg")
# Reorder the matrix by sum of columns - in an attempt of plotting first 'full' pies, then emptier ones
MyMatrx<-MyMatrx[order(rowSums(MyMatrx)),] # this works, but ploting order is unaltered...
# now melt your dataframe so as to be amenable to plotting as bargraph (of which piechart are but a subset)
DF <- melt(MyMatrx, varnames=c('cpd','variable'))
DF$cpd <- factor(DF$cpd, levels=rev(row.names(MyMatrx))) # this sets the order for the facet plotting
# let's now print out a series of Vlaaivis, faceted according to each compound - that is, one Vlaaivis x compound.
ggplot(DF, aes(factor(variable), value, fill = factor(variable)), color='black') + geom_bar(width = 1, alpha=0.5) + scale_y_continuous(breaks = 0:10) + coord_polar() + labs(x = "", y = "") + opts(legend.position = "none", axis.text.y = theme_blank(), axis.ticks = theme_blank()) + facet_wrap( ~ cpd) # facet_wrap, not grid
# It works!!! (Albeit not perfectly)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment