I wrote before about heatmap tables as a better way of producing frequency or other tables, with a solution which works nicely in latex.
It is possible to do them much more easily in ggplot2, like this
library(Hmisc) library(ggplot2) library(reshape) data(HairEyeColor) P=t(HairEyeColor[,,2]) Pm=melt(P) ggfluctuation(Pm,type="heatmap")+geom_text(aes(label=Pm$value),colour="white")+ opts(axis.text.x=theme_text(size = 15),axis.text.y=theme_text(size = 15))
Note that ggfluctuation will also take a table as input, but in this case P isn’t a table, it is a matrix, so we have to melt it using the reshape package.
Here is the output from the code above:
However, doing the marginal totals would be a bit of a faff like this.
Notice that this is statistically quite a different animal – unlike the previous version, the colours just divide the range of values. They are not indications of any kind of significant deviation from expected values. So they are less useful to the careful reader but on the other hand need no explanation.
Note also that ggfluctuation produces by default a different output
which is better in many ways. But it looks like a graphic, not a table, and the point of heatmap tables is you can slip them in where your reader expects a table and you don’t have to do so much explaining.



I do something similar but I find that table.grob() and grid.table() from the gridExtra package are easier to work with and provide direct to PNG output (so you don’t have to crop)
Ah, I didn’t know about table.grob() and grid.table(); but how easy is it to provide conditional formatting like this?
Hello,
Thank you for your post but can you share the code?
Best regards
Hi Regis, thanks for your comment! You mean the code for the latex tables in the first picture? That is in the post I referred to: http://socialdatablog.com/heatmap-tables-done-better-in-sweave-and-latex/
Hope this helps
Steve
[...] Any suggestions/ tricks, etc, about color picking, or using other type of data for the heatmap (rather than p-values)…? (Some tips using residuals from here). [...]
[...] can be included. Maybe could be useful to write in the tiles the actual p-values (like in this solution with ggplot2), or the odds ratios (OR) of the significant [...]