Heatmap tables with ggplot2, sort-of

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.

 


	

6 comments

  1. 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)

  2. Régis says:

    Hello,
    Thank you for your post but can you share the code?

    Best regards

  3. [...] 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). [...]

  4. [...] 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 [...]

Post a comment

Copyright © socialdatablog
Project evaluation. Reproducible research.

Built on Notes Blog Core
Powered by WordPress