ThinkCell type waterfall charts in R?
Hello Kerstin,
I had done something like this in the past but can't find it any more,
so I've put up a very raw example you might want to sophisticate upon.
The idea is that of drawing an empty high-level plot and then putting
some low-level elements (mainly bars and connecting lines) into it
according to the coordinates of the data. Attached below, I hope it
helps and avoids you the sin of importing ppt charts into LaTeX
(aaargh!).
PS The 'grid' package can help you produce more sophisticated (possibly
more "corporate-like", if this is a virtue...) graphs while retaining
the level of automation possible with R: e.g., for using Sweave for
automatic report generation. I'm sending you some examples by separate
mail.
Best,
Giovanni
examples: cascadeplot(rnorm(10)); cascadeplot(c(3,2,-7,5,8,3,-9,10))
cascadeplot <- function(x, barwidth=2, barspace=1,
colpalette=rep(grey(0.5), length(x)),
linecol="orange", ...) {
## how many bars? one for each data point
barnum <- length(x)
## total graph width
grw <- (barnum+1)*barspace+barnum*barwidth
## graph height
l.grh <- min(cumsum(x),0)
u.grh <- max(cumsum(x))+1
## draw an empty plot with the required dims
## (passing on any graphic parm you like, e.g. 'title')
plot(0:grw, ylim=c(l.grh,u.grh), xlim=c(0,grw), type="n", ...)
## draw x axis
lines(c(0,grw), c(0,0), col="blue")
## init vector (this will be useful later)
ytxtpos <- rep(NA, length(x))
## for each data point draw a polygon
## starting from the level reached by the
## previous bar
for(i in 1:length(x)) {
## each bar has 4 verices, abcd
## x coordinates for them:
## left vertices: ab, right: cd
## so bars are drawn like a->b->c->d
leftx <- barspace+(barspace+barwidth)*(i-1)
rightx <- leftx+barwidth
xcoo <- c(leftx, leftx, rightx, rightx)
## y coord.:
## lower y:
if(i==1) {
lowy <- 0
} else {
lowy <- cumsum(x)[i-1]
}
highy <- cumsum(x)[i]
ycoo <- c(lowy, highy, highy, lowy)
polygon(xcoo, ycoo, col=colpalette[i], ...)
## now draw the connecting lines between
## the current and the previous bar
if(i > 1) {
lines(x=c(leftx-barspace,leftx),
y=c(lowy,lowy),
col=linecol)
}
## save top-bar position for label
ytxtpos[i] <- max(lowy, highy)+0.05*(u.grh-l.grh)
}
## lastly, put the values on top of the bars
xtxtpos <- (1:length(x))*(barwidth+barspace)-barwidth/2
text(xtxtpos,ytxtpos,labels=as.character(x))
}
## original message below
------------------------------
Message: 157
Date: Tue, 03 Feb 2009 11:30:51 +0100
From: Kerstin <kerstin.balka at open-innovation-projects.org>
Subject: Re: [R] ThinkCell type waterfall charts in R?
To: Jim Lemon <jim at bitwrit.com.au>
Cc: r-help at r-project.org
Message-ID: <49881CDB.3090809 at open-innovation-projects.org>
Content-Type: text/plain; charset="iso-8859-1"; Format="flowed"
Jim Lemon wrote:
Kerstin wrote:
Hi all, with PowerPoint and ThinkCell one can draw something they call waterfall chart and it looks like this: http://www.think-cell.com/products/images/waterfall.gif I found discussions on waterfall charts in the archive of this mailinglist, but unfortunately they looked totally different. Other names for this type of plot seem to be bridge chart, cascade chart, stair case chart, etc. but neither of them brought successful results. So I decided to ask you directly on the list. Does anyone have an idea on how I could plot this type of chart in R?
Well Kirsten, it's a real challenge trying to find something that you _can't_ plot in R. Perhaps if you sent some data to the mailing list and a description of how the various counts (?) are related to one another, some R-nut may reply with what you want. Jim
Thanks a lot for your quick reply. In the attached pdf you can see the chart I would like to draw with R. I could of course make my life easier and use the Power Point chart, but importing a ppt chart into Latex would not make me sleep very well, besides the fact that all other plots in my document will come from R and I don't want this one to look totally different... Cheers Kerstin -------------- next part -------------- A non-text attachment was scrubbed... Name: Waterfall.pdf Type: application/pdf Size: 9761 bytes Desc: not available URL: <https://stat.ethz.ch/pipermail/r-help/attachments/20090203/6b6e3787/att achment-0001.pdf> ------------------------------ Giovanni Millo Research Dept., Assicurazioni Generali SpA Via Machiavelli 4, 34132 Trieste (Italy) tel. +39 040 671184 fax +39 040 671160 Ai sensi del D.Lgs. 196/2003 si precisa che le informazi...{{dropped:13}}