Heatmap tables with ggplot2, sort-of

[This article was first published on socialdatablog » R, and kindly contributed to R-bloggers]. (You can report issue about the content on this page here)
Want to share your content on R-bloggers? click here if you have a blog, or here if you don't.

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.

 

To leave a comment for the author, please follow the link and comment on their blog: socialdatablog » R.

R-bloggers.com offers daily e-mail updates about R news and tutorials about learning R and many other topics. Click here if you're looking to post or find an R/data-science job.
Want to share your content on R-bloggers? click here if you have a blog, or here if you don't.

Never miss an update!
Subscribe to R-bloggers to receive
e-mails with the latest R posts.
(You will not see this message again.)

Click here to close (This popup will not appear again)