Stacked bar graph with variable width elements?
<p>In Tableau I'm used to making graphs like the one below. It has for each day (or some other discrete variable), a stacked bar of categories of different colours, heights and widths.</p>

<p>You can imagine the categories to be different advertisements that I show to people. The heights correspond to the percentage of people I've shown the advertisement to, and the widths correspond to the rate of acceptance.</p>

<p>It allows me to see very easily which advertisements I should probably show more often (short, but wide bars, like the 'C' category on September 13th and 14th) and which I should show less often (tall, narrow bars, like the 'H' category on September 16th).</p>

<p>Any ideas on how I could create a graph like this in R or Python?</p>

<p><a href="http://i.stack.imgur.com/qDSKv.jpg" rel="nofollow"><img src="http://i.stack.imgur.com/qDSKv.jpg" alt="enter image description here"></a></p>

<pre><code>set.seed(1)
days &lt;- 5
cats &lt;- 8
dat &lt;- prop.table(matrix(rpois(days * cats, days), cats), 2)

bp1 &lt;- barplot(dat, col = seq(cats))
</code></pre>

<p><a href="http://i.stack.imgur.com/pvEFA.png" rel="nofollow"><img src="http://i.stack.imgur.com/pvEFA.png" alt="enter image description here"></a></p>

<pre><code>## some width for rect
rate &lt;- matrix(runif(days * cats, .1, .5), cats)

## calculate xbottom, xtop, ybottom, ytop
bp   &lt;- rep(bp1, each = cats)
ybot &lt;- apply(rbind(0, dat), 2, cumsum)[-(cats + 1), ]
ytop &lt;- apply(dat, 2, cumsum)

plot(extendrange(bp1), c(0,1), type = 'n', axes = FALSE, ann = FALSE)
rect(bp - rate, ybot, bp + rate, ytop, col = seq(cats))

text(bp, (ytop + ybot) / 2, LETTERS[seq(cats)])
axis(1, bp1, labels = format(Sys.Date() + seq(days), '%d %b %Y'), lwd = 0)
axis(2)
</code></pre>

<p><a href="http://i.stack.imgur.com/lGULx.png" rel="nofollow"><img src="http://i.stack.imgur.com/lGULx.png" alt="enter image description here"></a></p>

<p>Probably not very useful, but you can invert the color you are plotting so that you can actually see the labels:</p>

<pre><code>inv_col &lt;- function(color) {
  paste0('#', apply(apply(rbind(abs(255 - col2rgb(color))), 2, function(x)
    format(as.hexmode(x), 2)), 2, paste, collapse = ''))
}

inv_col(palette())
# [1] "#ffffff" "#00ffff" "#ff32ff" "#ffff00" "#ff0000" "#00ff00" "#0000ff" "#414141"


plot(extendrange(bp1), c(0,1), type = 'n', axes = FALSE, ann = FALSE)
rect(bp - rate, ybot, bp + rate, ytop, col = seq(cats), xpd = NA, border = NA)

text(bp, (ytop + ybot) / 2, LETTERS[seq(cats)], col = inv_col(seq(cats)))
axis(1, bp1, labels = format(Sys.Date() + seq(days), '%d %B\n%Y'), lwd = 0)
axis(2)
</code></pre>

<p><a href="http://i.stack.imgur.com/Dgu1m.png" rel="nofollow"><img src="http://i.stack.imgur.com/Dgu1m.png" alt="enter image description here"></a></p>

python graph ggplot2 data-visualization