Created
July 23, 2013 07:15
-
-
Save aaronsaunders/6060414 to your computer and use it in GitHub Desktop.
side by side barcharts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# http://timotheepoisot.fr/2013/02/17/stacked-barcharts/ | |
library(RColorBrewer) | |
Abund = round(matrix(runif(200,0,1),nc=10),2) | |
Abund = Abund/max(Abund) | |
rownames(Abund) = LETTERS[c(1:nrow(Abund))] | |
colnames(Abund) = letters[c(1:ncol(Abund))] | |
# This will give us a site/abundance matrix. The next step is to create a plot with nothing in it: | |
par(las=2) | |
X = c(1:nrow(Abund)) | |
Y = c(1:ncol(Abund)) | |
pal = colorRampPalette(brewer.pal(9,'Paired'))(nrow(Abund)) | |
image(X,Y,Abund,col='white',axes=FALSE,bty='n') | |
# It's easy to make it so that the width of each sub-plot is proportional to the relative abundance of each species.Then, for each row/line (and I'm aware this is not optimized at all), we will draw a vertical bar. Each taxon (i.e. each column in our graph) will have its own sub-plot. The size can be fixed, or equal. But this allows comparison of species abundances across dates. | |
for(ro in 1:nrow(Abund)){ | |
for(co in 1:ncol(Abund)){ | |
rect(ro-0.5, co-0.4, ro-0.5+Abund[ro,co]*0.9, co+0.4,border=NA,col=pal[ro]) | |
}} | |
# Finally, let's add reference lines for each species, and the site/species names on axes: | |
abline(v=X-0.5,col='darkgrey',lwd=2) | |
axis(2,at=c(1:ncol(Abund)),labels=colnames(Abund),col=NA) | |
axis(1,at=c(1:nrow(Abund)),labels=rownames(Abund),col=NA) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment