Skip to content

Instantly share code, notes, and snippets.

@abikoushi
Created February 19, 2015 13:09
Show Gist options
  • Save abikoushi/a5d142e26b9f1c8396ee to your computer and use it in GitHub Desktop.
Save abikoushi/a5d142e26b9f1c8396ee to your computer and use it in GitHub Desktop.
waterfall <-function(left,right,group,leftname ="",rightname ="",
xlab="",ylab="",leftcol="grey80",rightcol="grey80",cols=NULL){
sum1 =sum(left)
sum2 =sum(right)
tmp1 <- c(cumsum(c(sum1,right-left)),sum2)
len <-length(left)
bp <- barplot(c(sum1,rep(0,len),sum2),
space=0,yaxt="n",xaxt="n",
col=c(leftcol,rep("white",len),rightcol),
xlab=xlab,ylab=ylab)
yrange <- c(0,max(c(sum1,sum2)))
ticks <- pretty(yrange)
labels <- format(ticks, big.mark=",", scientific=FALSE)
axis(2, at = ticks, labels = labels, las = 1, cex.axis=0.7)
if(is.null(cols)){cols=rep("grey80",len)}
for(i in 1:len){
rect(i,tmp1[i],i+1,tmp1[i+1],col=cols[i])
}
axis(side=1,pos=0,bp,c(leftname,group,rightname),tick = FALSE)
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment