Heatmap tables done better, in Sweave and latex

July 10, 2011

(This article was first published on Social data blog, and kindly contributed to R-bloggers)



I wrote before about using heatmap tables to combine the strengths of tables and graphics for nominal data.

Here is a neat approach using Sweave and latex to produce an effect like in the picture.

This latex code is self-contained. Just save it as myfile.Rnw, run Sweave(myfile.Rnw) from inside R and then pdflatex myfile.tex at the command line from inside the same folder.

(Not a latex person myself so some of the commands may be unnecessary.)

The approach uses the residuals from the chisquared test to generate the “surprisingness” for each cell.

Any thoughts on the marginal totals – should they be in colour too? If so, using which residuals?
How would you improve on this?

\definecolor{blue1}{rgb}{.8, .8, 1}
\definecolor{blue2}{rgb}{.6, .6, 1}
\definecolor{blue3}{rgb}{.4, .4, 1}
\definecolor{blue4}{rgb}{.2, .2, 1}
\definecolor{blue5}{rgb}{0, 0, 1}
\definecolor{red1}{rgb}{1, .8, .8}
\definecolor{red2}{rgb}{1, .6, .6}
\definecolor{red3}{rgb}{1, .4, .4}
\definecolor{red4}{rgb}{1, .2, .2}
\definecolor{red5}{rgb}{1, 0, 0}



<<main, echo=FALSE, results=tex, strip.white=FALSE>>=
cellTex =Q

cellTex[r< -.6] <- “cellcolor{blue1}” #
cellTex[r< -1] <- “cellcolor{blue2}” #
cellTex[r< -1.5] <- “cellcolor{blue3}” #
cellTex[r< -2] <- “cellcolor{blue4}” #
cellTex[r< -2.5] <- “cellcolor{blue5}” #
cellTex[r> .6] <- “cellcolor{red1}” #
cellTex[r> 1] <- “cellcolor{red2}” #
cellTex[r> 1.5] <- “cellcolor{red3}” #
cellTex[r> 2] <- “cellcolor{red4}” #
cellTex[r> 2.5] <- “cellcolor{red5}” #
cellTex =Q
latex(Q,file = “”, cellTexCmds = cellTex,insert.bottom=
“\\footnotesize {\\textcolor{red}{Red} cells contain numbers which are surprisingly high; 
\\textcolor{blue}{blue} cells contain numbers which are surprisingly low. Stronger colours are more surprising.}”)



| Leave a comment  »

To leave a comment for the author, please follow the link and comment on their blog: Social data blog.

R-bloggers.com offers daily e-mail updates about R news and tutorials on topics such as: Data science, Big Data, R jobs, visualization (ggplot2, Boxplots, maps, animation), programming (RStudio, Sweave, LaTeX, SQL, Eclipse, git, hadoop, Web Scraping) statistics (regression, PCA, time series, trading) and more...

If you got this far, why not subscribe for updates from the site? Choose your flavor: e-mail, twitter, RSS, or facebook...

Comments are closed.

Recent popular posts


Mango solutions

RStudio homepage

Zero Inflated Models and Generalized Linear Mixed Models with R

Dommino data lab

Quantide: statistical consulting and training




CRC R books series

Six Sigma Online Training

Contact us if you wish to help support R-bloggers, and place your banner here.

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)