Search code examples
rlatexdata-visualizationstandard-errortufte

Tufte tables: convert quartile plots into standard error plots hacking qTable function from NMOF package


If you remember there is a nice version of table conceived by Tufte that include small quartile plots running next to the corresponding data rows:

enter image description here

There is an implementation of such solution in R using package NMOF and function qTable, which basically creates the table shown above and outputs it as a LaTeX code:

require(NMOF)
x <- rnorm(100, mean = 0, sd = 2)
y <- rnorm(100, mean = 1, sd = 2)
z <- rnorm(100, mean = 1, sd = 0.5)
X <- cbind(x, y, z)
qTable(X,filename="res.tex")#this will save qTable in tex format in current dir

This method of visualization seem to be especially useful if you have a small amount of information to present, and you don't want to waste a space for the full graph. But I would like to hack qTable a bit. Instead of displaying quantile plots, I would prefer to display standard errors of the mean. I am not great in hacking such functions, and I used brute force to do it. I replaced the line from the qTable function which computes quantiles:

A <- apply(X, 2L, quantile, c(0.25, 0.5, 0.75))

with something very brutal, that computes standard errors:

require(psych)#got 'SE' function to compute columns standard deviation     
M = colMeans(X)#column means
SE = SD(X)/sqrt(nrow(X))#standard error
SELo = M-SE#lower bound
SEHi = M+SE#upper bound
A = t(data.frame(SELo,M,SEHi))#combines it together

I know, I know it's possibly unsustainable approach, but it actually works to some extend - it plots standard errors but keeps this gap in the plot:

enter image description here

and I would like this gap to disappear.

Here is a qTable function with modification discussed above.


Solution

  • To remove the gaps, you can insert these two lines:

    B[2, ] <- B[3, ]
    B[4, ] <- B[3, ]
    

    right before the for loop that starts with

    for (cc in 1L:dim(X)[2L]) {...
    

    Why does it work? Reading the graph from left to right, the five rows of B correspond to where

    1) the left segments start
    2) the left segments ends
    3) the dots are
    4) the right segments start
    5) the right segments end
    

    so by forcing B[2, ] and B[4, ] to B[3, ] you are effectively getting rid of the gaps.