Note that there are some explanatory texts on larger screens.

plurals
  1. POProbabilty heatmap in ggplot
    primarykey
    data
    text
    <p>I asked <a href="https://stackoverflow.com/questions/7305803/plot-probability-heatmap-hexbin-with-different-sized-bins">this question</a> a year ago and got code for this "probability heatmap": <img src="https://i.stack.imgur.com/Jf4Mi.png" alt="heatmap"></p> <pre><code>numbet &lt;- 32 numtri &lt;- 1e5 prob=5/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") library(plyr) mxcum2 &lt;- ddply(mxcum, .(bet, outcome), nrow) mxcum3 &lt;- ddply(mxcum2, .(bet), summarize, ymin=c(0, head(seq_along(V1)/length(V1), -1)), ymax=seq_along(V1)/length(V1), fill=(V1/sum(V1))) head(mxcum3) library(ggplot2) p &lt;- ggplot(mxcum3, aes(xmin=bet-0.5, xmax=bet+0.5, ymin=ymin, ymax=ymax)) + geom_rect(aes(fill=fill), colour="grey80") + scale_fill_gradient("Outcome", formatter="percent", low="red", high="blue") + scale_y_continuous(formatter="percent") + xlab("Bet") print(p) </code></pre> <p>(May need to change this code slightly because of <a href="https://stackoverflow.com/questions/10146109/formatter-argument-in-scale-continuous-throwing-errors-in-r-2-15">this</a>)</p> <p>This is <em>almost</em> exactly what I want. Except each vertical shaft should have different numbers of bins, ie the first should have 2, second 3, third 4 (N+1). In the graph shaft 6 +7 have the same number of bins (7), where 7 should have 8 (N+1). </p> <p>If I'm right, the reason the code does this is because it is the observed data and if I ran more trials we would get more bins. I don't want to rely on the number of trials to get the correct number of bins.</p> <p><strong>How can I adapt this code to give the correct number of bins?</strong></p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload