title: Plot probability heatmap/hexbin with different sized bins
body: <p>This is related to another question: <a href="https://stackoverflow.com/questions/7280421/plot-weighted-frequency-matrix">Plot weighted frequency matrix</a>.</p>
  
  <p>I have this graphic (produced by the code below in R): <img src="https://i.stack.imgur.com/Wo3hv.png" alt="multisample"></p>
  
  <pre><code>#Set the number of bets and number of trials and % lines
  numbet &lt;- 36 
  numtri &lt;- 1000 
  #Fill a matrix where the rows are the cumulative bets and the columns are the trials
  xcum &lt;- matrix(NA, nrow=numbet, ncol=numtri)
  for (i in 1:numtri) {
  x &lt;- sample(c(0,1), numbet, prob=c(5/6,1/6), replace = TRUE)
  xcum[,i] &lt;- cumsum(x)/(1:numbet)
  }
  #Plot the trials as transparent lines so you can see the build up
  matplot(xcum, type="l", xlab="Number of Trials", ylab="Relative Frequency", main="", col=rgb(0.01, 0.01, 0.01, 0.02), las=1)
  </code></pre>
  
  <p>I very much like the way that this plot is built up and shows the more frequent paths as darker than the rarer paths (but it is not clear enough for a print presentation). What I would like to do is to produce some kind of hexbin or heatmap for the numbers. On thinking about it, it seems that the plot will have to incorporate different sized bins (see my back of the envelope sketch):</p>
  
  <p><img src="https://i.stack.imgur.com/riFtO.jpg" alt="binsketch"></p>
  
  <p>My question then: <strong>If I simulate a million runs using the code above, how can I present it as a heatmap or hexbin, with the different sized bins as shown in the sketch?</strong></p>
  
  <p>To clarify: I do not want to rely on transparency to show the rarity of a trial passing through a part of the plot. Instead I would like to denote rarity with heat and show a common pathway as hot (red) and a rare pathway as cold (blue). Also, I do not think the bins should be the same size because the first trial has only two places where the path can be, but the last has many more. Hence the fact I chose a changing bin scale, based on that fact. <strong>Essentially I am counting the number of times a path passes through the cell (2 in col 1, 3 in col 2 etc) and then colouring the cell based on how many times it has been passed through.</strong></p>
  
  <p>UPDATE: I already had a plot similar to @Andrie, but I am not sure it is much clearer than the top plot. It is the discontinuous nature of this graph, that I do not like (and why I want some kind of heatmap). I think that because the first column has only two possible values, that there should not be a huge visual gap between them etc etc. Hence why I envisaged the different sized bins. I still feel that the binning version would show large number of samples better. </p>
  
  <p><img src="https://i.stack.imgur.com/SaQPv.png" alt="plot2"></p>
  
  <p>Update: This <a href="http://www.actualanalytics.com/density-plot-heatmap-using-r-a58" rel="nofollow noreferrer">website</a> outlines a procedure to plot a heatmap:</p>
  
  <blockquote>
    <p>To create a density (heatmap) plot version of this we have to effectively enumerate the occurrence of these points at each discrete location in the image. This is done by setting a up a grid and counting the number of times a point coordinate "falls" into each of the individual pixel "bins" at every location in that grid.</p>
  </blockquote>
  
  <p>Perhaps some of the information on that website can be combined with what we have already?</p>
  
  <p>Update: I took some of what Andrie wrote with some of this <a href="https://stackoverflow.com/questions/5004063/creating-a-heat-map-from-x-y-corrdinates-in-r">question</a>, to arrive at this, which is quite close to what I was conceiving:
  <img src="https://i.stack.imgur.com/Z6gvg.png" alt="heatmap"></p>
  
  <pre><code>numbet &lt;- 20
  numtri &lt;- 100
  prob=1/6
  #Fill a matrix 
  xcum &lt;- matrix(NA, nrow=numtri, ncol=numbet+1)
  for (i in 1:numtri) {
    x &lt;- sample(c(0,1), numbet, prob=c(prob, 1-prob), replace = TRUE)
    xcum[i, ] &lt;- c(i, cumsum(x)/cumsum(1:numbet))
  }
  colnames(xcum) &lt;- c("trial", paste("bet", 1:numbet, sep=""))
  
  mxcum &lt;- reshape(data.frame(xcum), varying=1+1:numbet, 
    idvar="trial", v.names="outcome", direction="long", timevar="bet")
  
   #from the other question
   require(MASS)
  dens &lt;- kde2d(mxcum$bet, mxcum$outcome)
  filled.contour(dens)
  </code></pre>
  
  <p>I don't quite understand what's going on, but this seems to be more like what I wanted to produce (obviously without the different sized bins).</p>
  
  <p>Update: This is similar to the other plots here. It is not quite right:</p>
  
  <p><img src="https://i.stack.imgur.com/j7oqS.png" alt="hexbin"></p>
  
  <pre><code>plot(hexbin(x=mxcum$bet, y=mxcum$outcome))
  </code></pre>
  
  <p>Last try. As above:
  <img src="https://i.stack.imgur.com/07ey1.png" alt="enter image description here"></p>
  
  <pre><code>image(mxcum$bet, mxcum$outcome)
  </code></pre>
  
  <p>This is pretty good. I would just like it to look like my hand-drawn sketch.</p>